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

Hints for using typing_extensions with mypy #316

Closed
tgamblin opened this issue Dec 19, 2023 · 7 comments
Closed

Hints for using typing_extensions with mypy #316

tgamblin opened this issue Dec 19, 2023 · 7 comments

Comments

@tgamblin
Copy link

Is there a rough set of recommendations for how to configure mypy with different typing_extension versions?

For example, it looks like the last version of typing_extensions to support Python 3.6 was 4.1.1. If I use typing_extensions 4.1.1 with python 3.6, should I set mypy's python_version to 3.8? 3.9? Something else?

@tgamblin tgamblin changed the title Hints for using typing-extensions with mypy Hints for using typing_extensions with mypy Dec 19, 2023
@JelleZijlstra
Copy link
Member

I'm not sure there's much to be said from the typing-extensions side. If you want mypy to check Python 3.6, you should set mypy's python_version to 3.6, and pick an appropriate version of typing-extensions.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 2, 2024

+1 to everything Jelle said. Set mypy's --python-version to the oldest version of Python you'd like to support.

Additionally, note that because typing-extensions is highly special cased, type checkers are not typically aware of differences between versions of typing-extensions and usually pretend that typing-extensions works the way the latest release of typing-extensions did at the time that the version of your type checker was released.

This typically isn't a big deal. If your type checker is much newer than your typing-extensions you might get a false negative which will usually result in an import time error. If your typing-extensions is much newer than your type checker you might get a somewhat false positive, but new symbols in typing-extensions often require special case support in type checkers anyway so it's arguable how false such false positives would be.

Closing, but feel free to ask if you have follow-up questions!

@tgamblin
Copy link
Author

tgamblin commented Jan 2, 2024

I’m still confused by the answer here. I want my code to support python 3.6, but I want to use typing features from newer python versions. Isn’t that the point of typing extensions? Isn’t setting mypy’s Python version to 3.6 going to prevent me from using the new features in typing extensions?

@hauntsaninja
Copy link
Collaborator

Isn’t setting mypy’s Python version to 3.6 going to prevent me from using the new features in typing extensions?

No

@tgamblin
Copy link
Author

tgamblin commented Jan 2, 2024

Thanks!

@hauntsaninja
Copy link
Collaborator

One last note:

mypy dropped support for targeting Python 3.6 in mypy 1.6 (see https://github.com/python/mypy/blob/master/CHANGELOG.md#remove-support-for-targeting-python-36-and-earlier ).

So if you need bleeding edge type system features that only mypy 1.6 and newer support, then you need to choose between a) not using those features, b) upgrading mypy and potentially getting false negatives on Python 3.6

@tgamblin
Copy link
Author

tgamblin commented Jan 2, 2024

Thanks! My example was somewhat contrived. We support running on Python 3.6-3.12, but we’re currently setting --python-version to 3.7, and haven’t found that mypy admits any annotations that are source-incompatible with 3.6 (I’m not familiar enough to say if there are any).

Most devs are on newer Python versions (3.11, 3.12) and we don’t require that mypy itself be run from Python 3.6.

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

3 participants