marrow / WebCore

WebCore, the super tiny and blazingly fast modular Python web nanoframework.
MIT License
94 stars 10 forks source link

Local value referenced outside of scope. #193

Closed crosoftzach closed 3 years ago

crosoftzach commented 5 years ago

This appears to work, but for the wrong reasons (should be 'arg' not 'key'): https://github.com/marrow/WebCore/blob/76377606c3ca8f6dc259998ed13156979efbf4a9/web/ext/annotation.py#L57

amcgregor commented 3 years ago

The annotations extension is undergoing a large amount of rewrite for 3.0 with an eye towards greater capability and simplification. That particular cast closure has been rewritten to support enhanced resolution of "alias-able" types, correctly using passed-in values rather than fragile containing scope.

def cast(key, annotation, value):
    """Attempt to typecast data incoming from the web."""

    annotation = self.aliases.get(annotation, annotation)
    if isinstance(annotation, type) and isinstance(value, annotation): return value  # Nothing to do.
    annotation = self.mapper.get(annotation, annotation)

    try:
        value = annotation(value)
    except (ValueError, TypeError) as e:
        raise HTTPBadRequest(f"{e.__class__.__name__}: {e} while processing endpoint argument '{arg}'")

    return value