gh-140665: Substitute PEP 696 defaults referencing other parameters#151811
Open
Builder106 wants to merge 1 commit into
Open
gh-140665: Substitute PEP 696 defaults referencing other parameters#151811Builder106 wants to merge 1 commit into
Builder106 wants to merge 1 commit into
Conversation
…ters A type parameter's default may reference an earlier parameter in the same scope (e.g. class C[T, S = T], or S = list[T]), but the default was inserted unsubstituted when a trailing parameter was filled from it. Parametrizing C[int] produced C[int, T] instead of C[int, int], leaking the unbound type variable; nested defaults (S = list[T]) and chained defaults (S = T, U = S) were left unbound too. Add _resolve_parameter_defaults, called from _generic_class_getitem and _GenericAlias._determine_new_args, which substitutes the now-bound parameters into any argument supplied by a default. Detection is gated on the argument tuple growing during __typing_prepare_subst__, so explicitly-passed arguments such as C[int, T] are left untouched.
Author
|
The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A type parameter's default may reference an earlier parameter in the same scope —
class C[T, S = T], orclass C[T, S = list[T]]. When a generic is parametrized and a trailing parameter is filled from its default, the default was inserted without substituting the already-bound parameters, soC[int]producedC[int, T]instead ofC[int, int](andS = list[T]stayedlist[T], chainedS = T, U = Sstayed unbound).The fix adds
_resolve_parameter_defaults, called from both substitution paths (_generic_class_getitemand_GenericAlias._determine_new_args). Once the parameters are bound it substitutes them into any argument that was supplied by a default. Detection is gated on the argument tuple actually growing during__typing_prepare_subst__, so an explicitly-passed argument likeC[int, T]is preserved as(int, T)— only genuine defaults are resolved. Nested defaults (S = list[T]→list[int]) and chained defaults are handled.Verified on a
3.16.0a0build and against 3.14.6 runtime objects:C[int]goes from(int, T)to(int, int), and the fulltest_typingsuite passes (738 tests) with the addedtest_typevar_default_referencing_other_typevar, which covers explicit-vs-default arguments, nested and chained defaults, and a concrete-default guard.