Skip to content

Commit a3240d7

Browse files
committed
Reject GenericAlias parameters added after caching
Match the CPython 3.14.8 backport when an alias argument gains __typing_subst__ after __parameters__ was cached. Keep the regression reference on 3.14.8. Assisted-by: OpenAI Codex:model identifier unavailable
1 parent 4018e31 commit a3240d7

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎crates/vm/src/builtins/genericalias.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,10 @@ pub(crate) fn subs_parameters(
558558
if let Some(iparam) = tuple_index(parameters.as_slice(), arg) {
559559
subst.call((arg_items[iparam].clone(),), vm)?
560560
} else {
561-
subs_tvars(arg.clone(), parameters, &arg_items, vm)?
561+
return Err(vm.new_type_error(format!(
562+
"argument {} with __typing_subst__ was not found in __parameters__",
563+
arg.repr(vm)?
564+
)));
562565
}
563566
} else {
564567
subs_tvars(arg.clone(), parameters, &arg_items, vm)?

‎extra_tests/snippets/stdlib_typing.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,25 @@ def __module__(self):
9090
assert ParamSpec("Unbounded").__bound__ is None
9191
for bound in (None, "int", "not valid Python!", (int, str), object()):
9292
assert ParamSpec("BoundSpec", bound=bound).__bound__ is bound
93+
94+
95+
# GenericAlias rejects parameters added after its parameter tuple was cached.
96+
from types import GenericAlias
97+
98+
99+
class LateParameter:
100+
def __repr__(self):
101+
return "late_parameter"
102+
103+
104+
first = LateParameter()
105+
first.__typing_subst__ = lambda value: value
106+
late = LateParameter()
107+
alias = GenericAlias(dict, (first, late))
108+
assert alias.__parameters__ == (first,)
109+
late.__typing_subst__ = lambda value: value
110+
with assert_raises(TypeError) as caught:
111+
alias[0]
112+
assert str(caught.exception) == (
113+
"argument late_parameter with __typing_subst__ was not found in __parameters__"
114+
)

0 commit comments

Comments
 (0)