def test_handles_nested_annotations(self): def foo(bar: typing.Optional[typing.Iterator[int]]): ... signature = reflect.resolve_signature(foo) assert signature.parameters["bar"].annotation == typing.Optional[ typing.Iterator[int]]
def test_handles_NoneType(self): def foo(bar: type(None)) -> type(None): ... signature = reflect.resolve_signature(foo) assert signature.parameters["bar"].annotation is None assert signature.return_annotation is None
def decorator( callback: event_dispatcher.CallbackT[event_dispatcher.EventT_co], ) -> event_dispatcher.CallbackT[event_dispatcher.EventT_co]: nonlocal event_type signature = reflect.resolve_signature(callback) params = signature.parameters.values() if len(params) != 1: raise TypeError( "Event listener must have exactly one parameter, the event object." ) event_param = next(iter(params)) if event_type is None: if event_param.annotation is event_param.empty: raise TypeError( "Must provide the event type in the @listen decorator or as a type hint!" ) event_type = event_param.annotation self.subscribe(event_type, callback, _nested=1) return callback
def test_handles_only_return_annotated(self): def foo(bar, bat) -> str: ... signature = reflect.resolve_signature(foo) assert signature.parameters["bar"].annotation is reflect.EMPTY assert signature.parameters["bat"].annotation is reflect.EMPTY assert signature.return_annotation is str
def test_handles_mixed_annotations(self): def foo(bar: str, bat: "int"): ... signature = reflect.resolve_signature(foo) assert signature.parameters["bar"].annotation is str assert signature.parameters["bat"].annotation is int assert signature.return_annotation is reflect.EMPTY
def test_handles_forward_annotations(self): def foo(bar: "str", bat: "int") -> "str": ... signature = reflect.resolve_signature(foo) assert signature.parameters["bar"].annotation is str assert signature.parameters["bat"].annotation is int assert signature.return_annotation is str
def test_handles_normal_no_annotations(self): def foo(bar, bat): ... signature = reflect.resolve_signature(foo) assert signature.parameters["bar"].annotation is reflect.EMPTY assert signature.parameters["bat"].annotation is reflect.EMPTY assert signature.return_annotation is reflect.EMPTY
def test_handles_normal_references(self): def foo(bar: str, bat: int) -> str: ... signature = reflect.resolve_signature(foo) assert signature.parameters["bar"].annotation is str assert signature.parameters["bat"].annotation is int assert signature.return_annotation is str