Skip to content

Remove compatibility code for Python versions older than 3.10 - #835

Merged
AlexWaygood merged 1 commit into
mainfrom
alex/remove-pre-310-compat
Nov 30, 2026
Merged

AlexWaygood merged 1 commit into
mainfrom
alex/remove-pre-310-compat

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

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:

  • typing handles argument substitution for generics whose only type parameter is a ParamSpec. We can drop the special case for those generics in _check_generic and share one implementation across Python 3.10–3.12.
  • The ParamSpec tests now exercise cases with lists of types and ... that were commented out because they failed on 3.9.
  • Even Python 3.10.0 provides the typing helpers we need to deduplicate Literal arguments, so we can use those directly rather than backporting our own implementations.
  • Python 3.10 is also the only supported version that needs our Concatenate backport, so its entire implementation can live in that version branch.
  • The docs move objects such as get_args and get_origin into “Pure aliases” because we now re-export them from typing on every supported version.

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.44068% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.44%. Comparing base (dfd735e) to head (1bc7379).

Files with missing lines Patch % Lines
src/typing_extensions.py 71.42% 8 Missing ⚠️
@@            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     
Flag Coverage Δ
3.10 91.74% <84.74%> (+0.50%) ⬆️
3.10.4 91.74% <84.74%> (+0.50%) ⬆️
3.11 91.01% <64.40%> (+0.56%) ⬆️
3.11.0 90.23% <64.40%> (+0.55%) ⬆️
3.12 90.94% <64.40%> (+0.56%) ⬆️
3.12.0 90.93% <64.40%> (+0.56%) ⬆️
3.13 86.17% <61.01%> (+0.51%) ⬆️
3.13.0 86.90% <61.01%> (+0.52%) ⬆️
3.14 82.21% <57.62%> (+0.48%) ⬆️
3.14.0 82.96% <57.62%> (+0.49%) ⬆️
3.15 79.36% <57.62%> (+0.51%) ⬆️
3.15.0 79.36% <57.62%> (+0.51%) ⬆️
pypy3.10 91.57% <84.74%> (+0.50%) ⬆️
pypy3.11 90.86% <64.40%> (+0.56%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/test_typing_extensions.py 98.40% <100.00%> (+0.07%) ⬆️
src/typing_extensions.py 93.67% <71.42%> (+1.34%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines 5737 to +5739
def test_canonical_usage_with_variable_annotation(self):
ns = {}
exec('Alias: TypeAlias = Employee', globals(), ns)
class Namespace:
Alias: TypeAlias = Employee

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we no longer have any __class__ hacks 🎉

Comment on lines -5914 to -5918
# 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]]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ForwardRef.__owner__ was added in Python 3.14, but the below assertions still pass on 3.14+, so the comment is not relevant/accurate

Comment thread src/typing_extensions.py
Comment on lines 1558 to 1543
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').

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the part about Optional[t] is not accurate anymore (I think it dates back to an old behaviour that we removed at some point?)

Comment thread src/typing_extensions.py
# Recursively evaluate the type
if isinstance(value, ForwardRef):
if getattr(value, "__forward_module__", True) is not None:
if value.__forward_module__ is not None:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __forward_module__ now exists on every version of Python we support

@AlexWaygood
AlexWaygood marked this pull request as ready for review November 29, 2026 18:29
Comment on lines -5953 to -5960
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))

@AlexWaygood AlexWaygood Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a unit test for a helper function that this PR removes

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one optional comment below.

Comment thread src/typing_extensions.py
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__))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@AlexWaygood
AlexWaygood merged commit 295c9bc into main Nov 30, 2026
69 checks passed
@AlexWaygood
AlexWaygood deleted the alex/remove-pre-310-compat branch November 30, 2026 13:46
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

Successfully merging this pull request may close these issues.

2 participants