Stewori / pytypes

Typing-toolbox for Python 3 _and_ 2.7 w.r.t. PEP 484.
Apache License 2.0
200 stars 20 forks source link

Iterator and Generator annotations are not processed correctly #72

Open jolaf opened 5 years ago

jolaf commented 5 years ago

The following code:

from typing import Iterator

from pytypes import TypeChecker

def f() -> Iterator[str]:
    yield 'abc'

with TypeChecker():
    x = tuple(c for c in f())
    print("OK")

fails as follows:

$ python3 Test.py
Traceback (most recent call last):
  File "Test.py", line 9, in <module>
    x = tuple(c for c in f())
  File "Test.py", line 9, in <genexpr>
    x = tuple(c for c in f())
  File "Test.py", line 6, in f
    yield 'abc'
pytypes.exceptions.ReturnTypeError: 
  __main__.f
  returned incompatible type:
Expected: Iterator[str]
Received: str

Also a simple call to f(), not wrapped in generator expression and tuple, fails in a slightly different manner:

$ python3 Test.py
Exception ignored in: <generator object f at 0x7fe74e4b7db0>
Traceback (most recent call last):
  File "Test.py", line 5, in f
    def f() -> Iterator[str]:
  File "/usr/local/lib/python3.6/dist-packages/pytypes-1.0b5.post23-py3.6.egg/pytypes/type_util.py", line 2580, in __call__
    _check_caller_type(True, None, arg, caller_level=self._caller_level_shift+1)
  File "/usr/local/lib/python3.6/dist-packages/pytypes-1.0b5.post23-py3.6.egg/pytypes/type_util.py", line 2431, in _check_caller_type
    prop_getter, force_exception=True)
  File "/usr/local/lib/python3.6/dist-packages/pytypes-1.0b5.post23-py3.6.egg/pytypes/typechecker.py", line 718, in _checkfuncresult
    _raise_typecheck_error(msg, True, check_val, tpch, resSig, func)
  File "/usr/local/lib/python3.6/dist-packages/pytypes-1.0b5.post23-py3.6.egg/pytypes/type_util.py", line 2336, in _raise_typecheck_error
    raise pytypes.ReturnTypeError(msg)
pytypes.exceptions.ReturnTypeError: 
  __main__.f
  returned incompatible type:
Expected: Iterator[str]
Received: NoneType
OK

Similar stacks occur if Iterator[str] is replaced with Generator[str, None, None].