Skip to content

Commit

Permalink
Don't try to wrap generators that are untyped. (#118)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Hall <alex.mojaki@gmail.com>
  • Loading branch information
prescod and alexmojaki committed Jun 2, 2020
1 parent 6b8c22c commit f666c1e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 10 additions & 0 deletions tests/test_typeguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,16 @@ def generate(a: int):
gen = generate(1)
next(gen)

def test_wrapped_generator_no_return_type_annotation(self):
"""Test that return type checking works in a generator function too."""
@typechecked
def generate(a: int):
yield a
yield a + 1

gen = generate(1)
next(gen)

def test_varargs(self):
def foo(*args: int):
assert check_argument_types()
Expand Down
11 changes: 6 additions & 5 deletions typeguard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,12 @@ def wrapper(*args, **kwargs):
# If a generator is returned, wrap it if its yield/send/return types can be checked
if inspect.isgenerator(retval) or isasyncgen(retval):
return_type = memo.type_hints.get('return')
origin = getattr(return_type, '__origin__')
if origin in generator_origin_types:
return TypeCheckedGenerator(retval, memo)
elif origin is not None and origin in asyncgen_origin_types:
return TypeCheckedAsyncGenerator(retval, memo)
if return_type:
origin = getattr(return_type, '__origin__', None)
if origin in generator_origin_types:
return TypeCheckedGenerator(retval, memo)
elif origin is not None and origin in asyncgen_origin_types:
return TypeCheckedAsyncGenerator(retval, memo)

return retval

Expand Down

0 comments on commit f666c1e

Please sign in to comment.