Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,40 @@ class WrappedIntAndExtraData[T](Wrap[int]):
reveal_type(WrappedIntAndExtraData[bytes].__init__)
```

### Non-dataclass inheriting from generic dataclass

This is a regression test for <https://github.com/astral-sh/ty/issues/1427>.

When a non-dataclass inherits from a generic dataclass, the generic type parameters should still be
properly inferred when calling the inherited `__init__` method.

```py
from dataclasses import dataclass

@dataclass
class ParentDataclass[T]:
value: T

# Non-dataclass inheriting from generic dataclass
class ChildOfParentDataclass[T](ParentDataclass[T]): ...

def uses_dataclass[T](x: T) -> ChildOfParentDataclass[T]:
return ChildOfParentDataclass(x)

# TODO: ParentDataclass.__init__ should show generic types, not Unknown
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for highlighting this. I opened astral-sh/ty#1461 to track this.

# revealed: (self: ParentDataclass[Unknown], value: Unknown) -> None
reveal_type(ParentDataclass.__init__)

# revealed: (self: ParentDataclass[T@ChildOfParentDataclass], value: T@ChildOfParentDataclass) -> None
reveal_type(ChildOfParentDataclass.__init__)

result_int = uses_dataclass(42)
reveal_type(result_int) # revealed: ChildOfParentDataclass[Literal[42]]

result_str = uses_dataclass("hello")
reveal_type(result_str) # revealed: ChildOfParentDataclass[Literal["hello"]]
```

## Descriptor-typed fields

### Same type in `__get__` and `__set__`
Expand Down
8 changes: 5 additions & 3 deletions crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,8 @@ impl<'db> ClassLiteral<'db> {
});

if member.is_undefined() {
if let Some(synthesized_member) = self.own_synthesized_member(db, specialization, name)
if let Some(synthesized_member) =
self.own_synthesized_member(db, specialization, inherited_generic_context, name)
{
return Member::definitely_declared(synthesized_member);
}
Expand All @@ -2192,6 +2193,7 @@ impl<'db> ClassLiteral<'db> {
self,
db: &'db dyn Db,
specialization: Option<Specialization<'db>>,
inherited_generic_context: Option<GenericContext<'db>>,
name: &str,
) -> Option<Type<'db>> {
let dataclass_params = self.dataclass_params(db);
Expand Down Expand Up @@ -2320,7 +2322,7 @@ impl<'db> ClassLiteral<'db> {

let signature = match name {
"__new__" | "__init__" => Signature::new_generic(
self.inherited_generic_context(db),
inherited_generic_context.or_else(|| self.inherited_generic_context(db)),
Parameters::new(parameters),
return_ty,
),
Expand Down Expand Up @@ -2702,7 +2704,7 @@ impl<'db> ClassLiteral<'db> {
name: &str,
policy: MemberLookupPolicy,
) -> PlaceAndQualifiers<'db> {
if let Some(member) = self.own_synthesized_member(db, specialization, name) {
if let Some(member) = self.own_synthesized_member(db, specialization, None, name) {
Place::bound(member).into()
} else {
KnownClass::TypedDictFallback
Expand Down
Loading