Skip to content

Commit fbfc61f

Browse files
committed
[ty] Patch (implicit) Self on fallback types
1 parent 2b51ec6 commit fbfc61f

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

crates/ty_python_semantic/resources/mdtest/named_tuple.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,14 @@ reveal_type(Person._field_defaults) # revealed: dict[str, Any]
270270
reveal_type(Person._fields) # revealed: tuple[str, ...]
271271
reveal_type(Person._make) # revealed: bound method <class 'Person'>._make(iterable: Iterable[Any]) -> Person
272272
reveal_type(Person._asdict) # revealed: def _asdict(self) -> dict[str, Any]
273-
reveal_type(Person._replace) # revealed: def _replace(self, **kwargs: Any) -> Self@_replace
273+
reveal_type(Person._replace) # revealed: def _replace(self, **kwargs: Any) -> Person
274274

275-
# TODO: should be `Person` once we support `Self`
276-
reveal_type(Person._make(("Alice", 42))) # revealed: Unknown
275+
reveal_type(Person._make(("Alice", 42))) # revealed: Person
277276

278277
person = Person("Alice", 42)
279278

280279
reveal_type(person._asdict()) # revealed: dict[str, Any]
281-
# TODO: should be `Person` once we support `Self`
282-
reveal_type(person._replace(name="Bob")) # revealed: Unknown
280+
reveal_type(person._replace(name="Bob")) # revealed: Person
283281
```
284282

285283
## `collections.namedtuple`
@@ -355,7 +353,7 @@ class Point(NamedTuple):
355353

356354
reveal_type(Point._make) # revealed: bound method <class 'Point'>._make(iterable: Iterable[Any]) -> Point
357355
reveal_type(Point._asdict) # revealed: def _asdict(self) -> dict[str, Any]
358-
reveal_type(Point._replace) # revealed: def _replace(self, **kwargs: Any) -> Self@_replace
356+
reveal_type(Point._replace) # revealed: def _replace(self, **kwargs: Any) -> Point
359357

360358
static_assert(is_assignable_to(Point, NamedTuple))
361359

crates/ty_python_semantic/src/types/class.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ use crate::types::signatures::{CallableSignature, Parameter, Parameters, Signatu
2525
use crate::types::tuple::{TupleSpec, TupleType};
2626
use crate::types::typed_dict::typed_dict_params_from_class_def;
2727
use crate::types::{
28-
ApplyTypeMappingVisitor, Binding, BoundSuperError, BoundSuperType, CallableType,
29-
DataclassParams, DeprecatedInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor,
30-
IsEquivalentVisitor, KnownInstanceType, ManualPEP695TypeAliasType, MaterializationKind,
31-
NormalizedVisitor, PropertyInstanceType, StringLiteralType, TypeAliasType, TypeMapping,
32-
TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind, TypedDictParams,
33-
UnionBuilder, VarianceInferable, declaration_type, infer_definition_types,
28+
ApplyTypeMappingVisitor, Binding, BindingContext, BoundSuperError, BoundSuperType,
29+
CallableType, DataclassParams, DeprecatedInstance, FindLegacyTypeVarsVisitor,
30+
HasRelationToVisitor, IsEquivalentVisitor, KnownInstanceType, ManualPEP695TypeAliasType,
31+
MaterializationKind, NormalizedVisitor, PropertyInstanceType, StringLiteralType, TypeAliasType,
32+
TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind,
33+
TypedDictParams, UnionBuilder, VarianceInferable, declaration_type, infer_definition_types,
3434
};
3535
use crate::{
3636
Db, FxIndexMap, FxOrderSet, Program,
@@ -2257,6 +2257,15 @@ impl<'db> ClassLiteral<'db> {
22572257
.own_class_member(db, self.generic_context(db), None, name)
22582258
.place
22592259
.ignore_possibly_unbound()
2260+
.map(|ty| {
2261+
ty.apply_type_mapping(
2262+
db,
2263+
&TypeMapping::BindSelf(Type::instance(
2264+
db,
2265+
self.unknown_specialization(db),
2266+
)),
2267+
)
2268+
})
22602269
}
22612270
(CodeGeneratorKind::DataclassLike, "__replace__")
22622271
if Program::get(db).python_version(db) >= PythonVersion::PY313 =>

0 commit comments

Comments
 (0)