Skip to content

Conversation

brianschubert
Copy link
Member

@brianschubert brianschubert commented Sep 1, 2025

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
Member Author

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
Member Author

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
Member Author

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
Member Author

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?

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.

TypeError when omitting a Protocol type argument with default
1 participant