Skip to content

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

Closed

Description

@samuelcolvin

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.

Activity

  1. srittau commented on Jun 11, 2020

    @srittau
    Contributor

    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.

  2. samuelcolvin commented on Jun 11, 2020

    @samuelcolvin
    SponsorContributorAuthor

    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

  3. hauntsaninja commented on Jun 11, 2020

    @hauntsaninja
    Collaborator

    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'
    
  4. ilevkivskyi commented on Jun 12, 2020

    @ilevkivskyi
    Member

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

  5. samuelcolvin commented on Jun 25, 2020

    @samuelcolvin
    SponsorContributorAuthor

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions