Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix how type parameters are collected from parametrized Protocol bases #667

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

Open
brianschubert wants to merge 4 commits into python:main
base: main
Choose a base branch
Loading
from brianschubert:gh-636-backport-137281-param-order

Conversation

@brianschubert
Copy link
Contributor

@brianschubert brianschubert commented Sep 1, 2025
edited
Loading

Fixes #636, backport of python/cpython#137281

Added some inline comments about the implementation below.

Comment on lines +3274 to +3277
typevar_types = (TypeVar, typing.TypeVar, ParamSpec)
if hasattr(typing, "ParamSpec"): # Python 3.10+
typevar_types += (typing.ParamSpec,)
tvars = _collect_type_vars(cls.__orig_bases__, typevar_types)
Copy link
Contributor Author

@brianschubert brianschubert Sep 1, 2025

Choose a reason for hiding this comment

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

note: for compatibility with both 3.9 and 3.10+, c.f. python/cpython#26091

args,
*,
enforce_default_ordering=_marker,
validate_all=False,
Copy link
Contributor Author

@brianschubert brianschubert Sep 1, 2025

Choose a reason for hiding this comment

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

note: since typing only explicitly passes validate_all from inside _generic_init_subclass (which this monkey patches below), there's no need for sys._getframe hacks like what was done for enforce_default_ordering in #392.

Comment on lines +3335 to +3336
if enforce_default_ordering is _marker:
enforce_default_ordering = _has_generic_or_protocol_as_origin()
Copy link
Contributor Author

@brianschubert brianschubert Sep 1, 2025

Choose a reason for hiding this comment

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

note: using a placeholder default value for enforce_default_ordering allows the same function to work as _collect_parameters on Python <3.13 and as _collect_type_parameters on Python 3.13+. Another alternative would be to split this into separate <3.13 and 3.13+ implementations in respective version branches

Comment on lines +3310 to +3315
def _collect_parameters(
args,
*,
enforce_default_ordering=_marker,
validate_all=False,
):
Copy link
Contributor Author

@brianschubert brianschubert Sep 1, 2025

Choose a reason for hiding this comment

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

I saw that there have been previous issues where monkey patching typing internals can break future versions of typing if new parameters are added (c.f. python/cpython#118900). I think there's low risk of that here, since this only monkey patches internal functions on Python <3.15, and it seems unlikely that new parameter would be backported to 3.14 at this point. But if we wanted to help guard against that, I suppose adding an ignored **kwargs parameter might be an option?

Copy link

codecov bot commented Sep 9, 2025
edited
Loading

Codecov Report

❌ Patch coverage is 94.50549% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.30%. Comparing base (4f42e6b) to head (d748b5d).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/typing_extensions.py 92.64% 5 Missing ⚠️
@@ Coverage Diff @@
## main #667 +/- ##
==========================================
- Coverage 97.36% 97.30% -0.07% 
==========================================
 Files 3 3 
 Lines 7680 7768 +88 
==========================================
+ Hits 7478 7559 +81 
- Misses 202 209 +7 
Flag Coverage Δ
3.10 ?
3.10.4 88.65% <57.14%> (-0.34%) ⬇️
3.11 ?
3.11.0 87.13% <60.43%> (-0.33%) ⬇️
3.12 87.84% <60.43%> (-0.33%) ⬇️
3.12.0 ?
3.13 ?
3.13.0 ?
3.14 79.10% <61.53%> (+0.27%) ⬆️
3.9 89.35% <56.04%> (-0.36%) ⬇️
3.9.12 89.35% <56.04%> (-0.36%) ⬇️
pypy3.10 88.49% <57.14%> (-0.34%) ⬇️
pypy3.11 87.75% <60.43%> (-0.33%) ⬇️
pypy3.9 89.18% <56.04%> (-0.36%) ⬇️

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.36% <100.00%> (-0.03%) ⬇️
src/typing_extensions.py 93.82% <92.64%> (-0.06%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

TypeError when omitting a Protocol type argument with default

1 participant

AltStyle によって変換されたページ (->オリジナル) /