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

Support conditionally importing from typing_extensions #14220

Open
JukkaL opened this issue Nov 29, 2022 · 5 comments
Open

Support conditionally importing from typing_extensions #14220

JukkaL opened this issue Nov 29, 2022 · 5 comments
Labels

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 29, 2022

This code tries to import TypeAlias from typing if possible and falls back to typing_extensions if it isn't available (e.g. on Python 3.8):

try:
    from typing import TypeAlias  # type: ignore
except ImportError:
    from typing_extensions import TypeAlias

Mypy doesn't actually support this as expected in older Python versions that are missing the definition in typing. They will just get an Any value, and the except block has no significance.

Perhaps the import from typing_extensions should take precedence? Or can we at least generate a more specific error about this?

A possible workaround is to flip the order of the imports.

This probably affects any feature that requires an import of a backported definition from typing_extensions.

Related issue: #14219

@JukkaL JukkaL added the feature label Nov 29, 2022
@erictraut
Copy link

Is there a need to support this idiom? The typing_extensions library already provides a backward-compatibility abstraction and uses typing.TypeAlias if it's available, so the above code could be simplified to from typing_extensions import TypeAlias. This is not only simpler to read and maintain, but it also eliminates problems for static analysis tools.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Nov 29, 2022

Spelling things with if sys.version_info checks works well; I'm fine with a won't fix here

@AlexWaygood
Copy link
Member

the above code could be simplified to from typing_extensions import TypeAlias.

Some packages might prefer to only require the extra dependency if a user is installing the package on an older version of Python, so as to keep third-party dependencies to an absolute minimum where possible. I think this practice is relatively common.

Having said that, I agree with @hauntsaninja that the sys.version_info way of doing things is ~fine (though I do personally prefer EAFP style here).

@AlexWaygood
Copy link
Member

@gamecss, please don't post duplicate comments in multiple places. Copying and pasting the reply I left you in #16903:

Correct. You need to do this:

import sys

if sys.version_info >= (3, 10):
    from typing import ParamSpec
else:
    from typing_extensions import ParamSpec

You can't do from sys import version_info and expect mypy to understand it in the same way, I'm afraid. It doesn't have the ability to understand arbitrary constructs like this; only a small selection of special-cased constructs are supported. See the relevant section in PEP 484: https://peps.python.org/pep-0484/#version-and-platform-checking

@ghost

This comment was marked as off-topic.

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

No branches or pull requests

4 participants