-
Notifications
You must be signed in to change notification settings - Fork 106
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
Use sys.version_info to guard imports #1177
Conversation
if sys.version_info >= (3, 10): | ||
from typing import get_args, get_origin | ||
else: | ||
# Python 3.8 has get_origin() and get_args() but those implementations aren't | ||
# Annotated-aware. Python 3.9's versions don't support ParamSpecArgs and | ||
# ParamSpecKwargs. | ||
from typing_extensions import get_args, get_origin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old version of this code was importing Self
in the try/except block, causing it to fall back to the bespoke implementation in typing_extensions
for Python 3.9. I'm unclear if this was intentional, so retained this behaviour to avoid any potential regression. For Python 3.10, typing_extensions
imports Python's built-in version, so we can import it directly.
else: | ||
from typing_extensions import Annotated | ||
|
||
|
||
from typing_extensions import ParamSpec, get_args, get_origin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unclear if this was intentionally not guarded, so left as-is. If the intention is to ensure any future typing_extensions
behavioural changes are included, we could in a future PR change this to
if TYPE_CHECKING:
if sys.version_info >= (3, 9):
from typing import ParamSpec, get_args, get_origin
else:
from typing_extensions import ParamSpec, get_args, get_origin
else:
try:
# Prefer to use get_args/get_origin from typing_extensions to support any future behavioural
# extensions
from typing_extensions import ParamSpec, get_args, get_origin
except ImportError:
# Fall back to typing if typing_extensions is not available
from typing import ParamSpec, get_args, get_origin
2dc6f22
to
e0bcd68
Compare
Use `if sys.version_info >= (3, minor):` for Python-version-dependent imports. Unlike `try/except ImportError`, this is natively supported by typecheckers, allowing removal of typechecker error suppression and consequently better typechecking validation. Signed-off-by: Alice Purcell <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1177 +/- ##
=======================================
- Coverage 81.7% 81.5% -0.2%
=======================================
Files 54 60 +6
Lines 4208 4756 +548
Branches 889 1016 +127
=======================================
+ Hits 3439 3880 +441
- Misses 574 646 +72
- Partials 195 230 +35 ☔ View full report in Codecov by Sentry. |
e0bcd68
to
91294df
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
else: | ||
from typing_extensions import Annotated | ||
|
||
|
||
from typing_extensions import ParamSpec, get_args, get_origin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strong opinion. looks great if we update this statement as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Will leave to a separate PR to avoid risking coupling a behaviour change to a large refactor
Pull Request Checklist
Description of PR
Currently, conditional imports that depend on Python version are using
try/except ImportError
to import fromtyping_extensions
on older Python versions. This is not natively supported by typecheckers, forcing use of error suppression, and losing potential validation of downstream code. (Additionally, pyright is erroneously flagging a lot of code that usesAnnotated
with "Call expression not allowed in type expression, making it harder to spot real issues in VSCode.)This PR refactors these conditional imports to use the recommended
if sys.version_info >= (3, minor):
pattern instead.See also: mypy issue comment, pyright issue comment