Skip to content

No Generator is considered to be an instance of GeneratorType #5496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mthuurne opened this issue May 18, 2021 · 7 comments · Fixed by #5474
Closed

No Generator is considered to be an instance of GeneratorType #5496

mthuurne opened this issue May 18, 2021 · 7 comments · Fixed by #5474

Comments

@mthuurne
Copy link
Contributor

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.

@JelleZijlstra
Copy link
Member

You're in luck because I just submitted a typeshed PR that should fix this: #5474.

@mthuurne
Copy link
Contributor Author

Thanks!

I'm trying to verify that your PR fixes this, but I can't get mypy to use an external typeshed directory:

$ mypy --custom-typeshed-dir ~/src/typeshed generator_type.py
generator_type.py:1: error: No library stub file for standard library module 'types'  [import]
generator_type.py:1: note: (Stub files are from https://github.com/python/typeshed)
generator_type.py:1: error: Cannot find 'builtins' module. Typeshed appears broken!
generator_type.py:2: error: No library stub file for standard library module 'typing'  [import]
Found 3 errors in 1 file (errors prevented further checking)

Where ~/src/typeshed is the git work area where I checked out your branch.

@JelleZijlstra
Copy link
Member

You may have to also use mypy master. mypy hasn't had a release yet since we changed the typeshed directory structure a few months ago.

@mthuurne
Copy link
Contributor Author

Indeed it works with mypy master and the unexpected errors are gone then.

Should I close this issue now or wait until the fix has been merged?

@JelleZijlstra
Copy link
Member

I'll transfer the issue to typeshed and associate the PR with the issue.

@JelleZijlstra
Copy link
Member

Also, thanks for checking!

@JelleZijlstra JelleZijlstra transferred this issue from python/mypy May 18, 2021
@mthuurne
Copy link
Contributor Author

OK, and thanks for fixing this before I even reported it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants