Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyrefly/lib/alt/class/class_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
})
}

fn get_class_member_with_defining_class(
pub(in crate::alt::class) fn get_class_member_with_defining_class(
&self,
cls: &Class,
name: &Name,
Expand Down
31 changes: 27 additions & 4 deletions pyrefly/lib/alt/class/typed_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,34 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
name: &Name,
is_total: bool,
) -> Option<TypedDictField> {
self.get_class_member(class, name).and_then(|field| {
Arc::unwrap_or_clone(field)
// Get the field along with the class that defines it
let member_with_defining_class = self.get_class_member_with_defining_class(class, name)?;
let defining_class = &member_with_defining_class.defining_class;
let field = Arc::unwrap_or_clone(member_with_defining_class.value);

// If the field is defined in the current class, just apply the substitution directly
if defining_class == class {
return field
.as_typed_dict_field_info(is_total)
.map(|field| field.substitute_with(substitution))
})
.map(|field| field.substitute_with(substitution));
}

// If the field is inherited, we need to compose substitutions:
// 1. Find the defining class in the MRO to get its type arguments
// 2. Apply the child's substitution to get the defining class's concrete type arguments
// 3. Use those type arguments to substitute into the field type
let mro = self.get_mro_for_class(class);
let defining_class_type = mro
.ancestors(self.stdlib)
.find(|ancestor| ancestor.class_object() == defining_class)?;

// Apply child's substitution to the defining class's type arguments
let defining_class_substituted = defining_class_type.substitute_with(substitution);

// Now apply the defining class's substitution to the field
field
.as_typed_dict_field_info(is_total)
.map(|field| field.substitute_with(&defining_class_substituted.substitution()))
}

pub fn typed_dict_fields(&self, typed_dict: &TypedDict) -> SmallMap<Name, TypedDictField> {
Expand Down
24 changes: 24 additions & 0 deletions pyrefly/lib/test/typed_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2091,3 +2091,27 @@ x = TD({"x": 1}, y="2")
x = TD({"x": 1})
"#,
);

testcase!(
test_generic_typed_dict_inheritance_with_unpack,
r#"
from typing import TypedDict, Type, Generic
from typing_extensions import TypeVar, Unpack

_T = TypeVar("_T", default=str)

class Base(TypedDict, Generic[_T], total=False):
default: _T | None

T = TypeVar('T')

class Child(Base[T], total=False):
other: Type[T]

def test(**kwargs: Unpack[Child[int]]) -> None:
pass

# Should accept default=5 because Child[int] should have default: int | None
test(other=int, default=5)
"#,
);