-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Bug Report
types.GeneratorType is documented as "The type of generator-iterator objects, created by generator functions." Therefore I would expect that a value annotated using typing.Generator would be considered an instance of GeneratorType. However, that is not the case.
It is possible that this issue originates in typeshed instead of mypy; I don't know whether mypy is drawing the wrong conclusions or is operating on flawed definitions. Even if the problem is in typeshed, perhaps mypy can output a more detailed error message, listing which method signatures it considers incompatible.
To Reproduce
Run mypy on the following code:
from types import GeneratorType
from typing import Any, Generator
def check(g: Generator[Any, Any, Any]) -> None:
if not isinstance(g, GeneratorType):
raise TypeError("Not a generator")
print("It is a generator")
def gen() -> Generator[int, None, None]:
yield 123
check(gen())
try:
check(None)
except TypeError:
print("Non-generator is rejected")
else:
print("Non-generator is accepted")Expected Behavior
Only check(None) should be flagged as an error.
Actual Behavior
$ mypy generator_type.py
generator_type.py:5: error: Subclass of "Generator[Any, Any, Any]" and "GeneratorType" cannot exist: would have incompatible method signatures [unreachable]
generator_type.py:7: error: Statement is unreachable [unreachable]
generator_type.py:15: error: Argument 1 to "check" has incompatible type "None"; expected "Generator[Any, Any, Any]" [arg-type]
Found 3 errors in 1 file (checked 1 source file)
The first error (isinstance), reported on line 5, is the one I consider a bug.
The error reported on line 7 (print) is a consequence of the one on line 5.
The error reported on line 15 (check(None)) is an actual error.
Note that the runtime behavior is as expected:
$ python generator_type.py
It is a generator
Non-generator is rejected
Your Environment
I'm using mypy 0.812 with Python 3.8.8 on openSUSE Linux.