Skip to content
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

python version checks don't seem to work with 0.780 #8990

Closed
samuelcolvin opened this issue Jun 11, 2020 · 5 comments
Closed

python version checks don't seem to work with 0.780 #8990

samuelcolvin opened this issue Jun 11, 2020 · 5 comments

Comments

@samuelcolvin
Copy link
Contributor

Upgrading to mypy 0.780 from 0.770, I can't find a way to get the following lines to pass with both python 3.6 and 3.7.

If I try ignoring different lines with # type: ignore I get either errors or unused 'type: ignore' comment from one version of python or the other.

if sys.version_info < (3, 7):
    from typing import _ForwardRef as ForwardRef
else:
    from typing import ForwardRef

Is this a problem with 0.780 or am I doing something wrong.

@srittau
Copy link
Contributor

srittau commented Jun 11, 2020

This is a typeshed issue. ForwardRef is only defined for Python 3.7+, since it was added then under that name. The previous _ForwardRef name is not represented. PR for typeshed welcome that adds it.

@samuelcolvin
Copy link
Contributor Author

okay, but is there any way around this using # type: ignore for both python 3.6 and 3.7?

I don't understand why the errors are different between python versions.

There seems no way to specify a "type ignore" that only applies to one python version.

Linked PR on pydantic pydantic/pydantic#1598

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jun 11, 2020

Basically a dupe of #8823 (typeshed-specific details aside)

You should be able to work around with something like:

from typing import Any, TYPE_CHECKING

if TYPE_CHECKING:
    ForwardRef: Any
else:
    from typing import ...

which isn't ideal, since mypy will now treat ForwardRef as Any.

Edit: Actually, you could do the following:

~/tmp λ cat test1.py                      
import sys
if sys.version_info >= (3, 7):
    from typing import ForwardRef
else:
    import typing
    from typing import cast, Any
    ForwardRef = cast(Any, typing)._ForwardRef

reveal_type(ForwardRef)
~/tmp λ mypy --python-version 3.8 test1.py
test1.py:9: note: Revealed type is 'def (arg: builtins.str, is_argument: builtins.bool =) -> typing.ForwardRef'
~/tmp λ mypy --python-version 3.6 test1.py
test1.py:9: note: Revealed type is 'Any'

@ilevkivskyi
Copy link
Member

It doesn't look like there is a (new) action item for mypy here, so closing.

@samuelcolvin
Copy link
Contributor Author

samuelcolvin commented Jun 25, 2020

@hauntsaninja thanks for pointing to the other issue. Unfortunately you ForwardRef = cast(Any, typing)._ForwardRef solution doesn't work - this is currently preventing pydantic from upgrading latest mypy (I've found another work around).

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

No branches or pull requests

4 participants