Repository navigation
Remove compatibility code for Python versions older than 3.10 - #835
Conversation
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## main #835 +/- ##
==========================================
+ Coverage 97.07% 97.44% +0.36%
==========================================
Files 3 3
Lines 7588 7510 -78
==========================================
- Hits 7366 7318 -48
+ Misses 222 192 -30
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
| def test_canonical_usage_with_variable_annotation(self): | ||
| ns = {} | ||
| exec('Alias: TypeAlias = Employee', globals(), ns) | ||
| class Namespace: | ||
| Alias: TypeAlias = Employee |
There was a problem hiding this comment.
this test was originally added in python/typing@37eb9da, at a point when we still supported Python 3.5 and Python 2. There's no longer any reason for it to use exec().
| self.assertEqual(C4.__parameters__, (P, T)) | ||
|
|
||
| # ParamSpec instances should also have args and kwargs attributes. | ||
| # Note: not in dir(P) because of __class__ hacks |
There was a problem hiding this comment.
we no longer have any __class__ hacks 🎉
| # The following are some valid uses cases in PEP 612 that don't work: | ||
| # These do not work in 3.9, _type_check blocks the list and ellipsis. | ||
| # G3 = X[int, [int, bool]] | ||
| # G4 = X[int, ...] | ||
| # G5 = Z[[int, str, bool]] |
There was a problem hiding this comment.
these commented-out cases now all pass, so I've added them as proper tests
| Gen = ns["Gen"] | ||
|
|
||
| # owner=None, type_params=None | ||
| # NOTE: The behavior of owner=None might change in the future when ForwardRef.__owner__ is available |
There was a problem hiding this comment.
ForwardRef.__owner__ was added in Python 3.14, but the below assertions still pass on 3.14+, so the comment is not relevant/accurate
| This is often the same as obj.__annotations__, but it handles | ||
| forward references encoded as string literals, adds Optional[t] if a | ||
| default value equal to None is set and recursively replaces all | ||
| 'Annotated[T, ...]', 'Required[T]' or 'NotRequired[T]' with 'T' | ||
| forward references encoded as string literals and recursively replaces | ||
| 'Annotated[T, ...]', 'Required[T]', 'NotRequired[T]' and 'ReadOnly[T]' with 'T' | ||
| (unless 'include_extras=True'). |
There was a problem hiding this comment.
the part about Optional[t] is not accurate anymore (I think it dates back to an old behaviour that we removed at some point?)
| # Recursively evaluate the type | ||
| if isinstance(value, ForwardRef): | ||
| if getattr(value, "__forward_module__", True) is not None: | ||
| if value.__forward_module__ is not None: |
There was a problem hiding this comment.
The __forward_module__ now exists on every version of Python we support
| def test_is_param_expr(self): | ||
| P = ParamSpec("P") | ||
| P_typing = typing.ParamSpec("P_typing") | ||
| self.assertTrue(typing_extensions._is_param_expr(P)) | ||
| self.assertTrue(typing_extensions._is_param_expr(P_typing)) | ||
| if hasattr(typing, "_is_param_expr"): | ||
| self.assertTrue(typing._is_param_expr(P)) | ||
| self.assertTrue(typing._is_param_expr(P_typing)) |
There was a problem hiding this comment.
a unit test for a helper function that this PR removes
srittau
left a comment
There was a problem hiding this comment.
Just one optional comment below.
| return NotImplemented | ||
| these_args_deduped = set(_value_and_type_iter(self.__args__)) | ||
| other_args_deduped = set(_value_and_type_iter(other.__args__)) | ||
| these_args_deduped = set(typing._value_and_type_iter(self.__args__)) |
There was a problem hiding this comment.
Optional: Considering the very simple implementation, wouldn't it be safer to keep our custom version than to access typing internals (which in theory may change at any time)?
There was a problem hiding this comment.
I'd agree, except that this branch is only active on 3.10.0 (on Python >=3.10.1, we use typing.Literal). It seems very unlikely that the implementation of typing._value_and_type_iter will change on 3.10.0 at this point
This follows up on #831 by removing more workarounds for Python 3.9 and earlier now that 3.10 is our minimum supported version.
In particular:
typinghandles argument substitution for generics whose only type parameter is aParamSpec. We can drop the special case for those generics in_check_genericand share one implementation across Python 3.10–3.12.ParamSpectests now exercise cases with lists of types and...that were commented out because they failed on 3.9.typinghelpers we need to deduplicateLiteralarguments, so we can use those directly rather than backporting our own implementations.Concatenatebackport, so its entire implementation can live in that version branch.get_argsandget_origininto “Pure aliases” because we now re-export them fromtypingon every supported version.