mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:15:33 +00:00
[ty] Add generic inference for dataclasses (#18443)
## Summary An issue seen here https://github.com/astral-sh/ty/issues/500 The `__init__` method of dataclasses had no inherited generic context, so we could not infer the type of an instance from a constructor call with generics ## Test Plan Add tests to classes.md` in generics folder
This commit is contained in:
parent
71d8a5da2a
commit
e8ea40012a
4 changed files with 30 additions and 4 deletions
|
@ -379,6 +379,21 @@ C[None](b"bytes") # error: [no-matching-overload]
|
||||||
C[None](12)
|
C[None](12)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Synthesized methods with dataclasses
|
||||||
|
|
||||||
|
```py
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class A(Generic[T]):
|
||||||
|
x: T
|
||||||
|
|
||||||
|
reveal_type(A(x=1)) # revealed: A[int]
|
||||||
|
```
|
||||||
|
|
||||||
## Generic subclass
|
## Generic subclass
|
||||||
|
|
||||||
When a generic subclass fills its superclass's type parameter with one of its own, the actual types
|
When a generic subclass fills its superclass's type parameter with one of its own, the actual types
|
||||||
|
|
|
@ -354,6 +354,18 @@ C[None](b"bytes") # error: [no-matching-overload]
|
||||||
C[None](12)
|
C[None](12)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Synthesized methods with dataclasses
|
||||||
|
|
||||||
|
```py
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class A[T]:
|
||||||
|
x: T
|
||||||
|
|
||||||
|
reveal_type(A(x=1)) # revealed: A[int]
|
||||||
|
```
|
||||||
|
|
||||||
## Generic subclass
|
## Generic subclass
|
||||||
|
|
||||||
When a generic subclass fills its superclass's type parameter with one of its own, the actual types
|
When a generic subclass fills its superclass's type parameter with one of its own, the actual types
|
||||||
|
|
|
@ -134,9 +134,7 @@ class Property[T](NamedTuple):
|
||||||
name: str
|
name: str
|
||||||
value: T
|
value: T
|
||||||
|
|
||||||
# TODO: this should be supported (no error, revealed type of `Property[float]`)
|
reveal_type(Property("height", 3.4)) # revealed: Property[float]
|
||||||
# error: [invalid-argument-type]
|
|
||||||
reveal_type(Property("height", 3.4)) # revealed: Property[Unknown]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Attributes on `NamedTuple`
|
## Attributes on `NamedTuple`
|
||||||
|
|
|
@ -1372,7 +1372,8 @@ impl<'db> ClassLiteral<'db> {
|
||||||
parameters.push(parameter);
|
parameters.push(parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
let signature = Signature::new(Parameters::new(parameters), Some(Type::none(db)));
|
let mut signature = Signature::new(Parameters::new(parameters), Some(Type::none(db)));
|
||||||
|
signature.inherited_generic_context = self.generic_context(db);
|
||||||
Some(CallableType::function_like(db, signature))
|
Some(CallableType::function_like(db, signature))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue