Skip to content

Commit 9649de7

Browse files
committed
Copy over binding context from old typevar
1 parent 2e8bc65 commit 9649de7

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

crates/ty_python_semantic/resources/mdtest/named_tuple.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ 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
273+
reveal_type(Person._replace) # revealed: def _replace(self, **kwargs: Any) -> Self@_replace
274274

275275
# TODO: should be `Person` once we support implicit type of `self`
276276
reveal_type(Person._make(("Alice", 42))) # revealed: Unknown
@@ -373,7 +373,7 @@ class Point(NamedTuple):
373373

374374
reveal_type(Point._make) # revealed: bound method <class 'Point'>._make(iterable: Iterable[Any]) -> Point
375375
reveal_type(Point._asdict) # revealed: def _asdict(self) -> dict[str, Any]
376-
reveal_type(Point._replace) # revealed: def _replace(self, **kwargs: Any) -> Self
376+
reveal_type(Point._replace) # revealed: def _replace(self, **kwargs: Any) -> Self@_replace
377377

378378
static_assert(is_assignable_to(Point, NamedTuple))
379379

crates/ty_python_semantic/src/types.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5973,7 +5973,13 @@ impl<'db> Type<'db> {
59735973
}
59745974
TypeMapping::ReplaceSelf { new_upper_bound } => {
59755975
if bound_typevar.typevar(db).is_self(db) {
5976-
Type::TypeVar(BoundTypeVarInstance::synthetic_self(db, *new_upper_bound))
5976+
Type::TypeVar(
5977+
BoundTypeVarInstance::synthetic_self(
5978+
db,
5979+
*new_upper_bound,
5980+
bound_typevar.binding_context(db)
5981+
)
5982+
)
59775983
} else {
59785984
self
59795985
}
@@ -7690,7 +7696,11 @@ impl<'db> BoundTypeVarInstance<'db> {
76907696
}
76917697

76927698
/// Create a new synthetic `Self` type variable with the given upper bound.
7693-
pub(crate) fn synthetic_self(db: &'db dyn Db, upper_bound: Type<'db>) -> Self {
7699+
pub(crate) fn synthetic_self(
7700+
db: &'db dyn Db,
7701+
upper_bound: Type<'db>,
7702+
binding_context: BindingContext<'db>,
7703+
) -> Self {
76947704
Self::new(
76957705
db,
76967706
TypeVarInstance::new(
@@ -7702,7 +7712,7 @@ impl<'db> BoundTypeVarInstance<'db> {
77027712
None,
77037713
TypeVarKind::TypingSelf,
77047714
),
7705-
BindingContext::Synthetic,
7715+
binding_context,
77067716
)
77077717
}
77087718

crates/ty_python_semantic/src/types/signatures.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::types::generics::{GenericContext, walk_generic_context};
2222
use crate::types::{
2323
ApplyTypeMappingVisitor, BindingContext, BoundTypeVarInstance, FindLegacyTypeVarsVisitor,
2424
HasRelationToVisitor, IsEquivalentVisitor, KnownClass, MaterializationKind, NormalizedVisitor,
25-
TypeMapping, TypeRelation, TypeVarKind, VarianceInferable, todo_type,
25+
TypeMapping, TypeRelation, VarianceInferable, todo_type,
2626
};
2727
use crate::{Db, FxOrderSet};
2828
use ruff_python_ast::{self as ast, name::Name};
@@ -474,11 +474,15 @@ impl<'db> Signature<'db> {
474474
self.generic_context.map(|context| {
475475
GenericContext::from_typevar_instances(
476476
db,
477-
context.variables(db).iter().map(|var| {
478-
if var.typevar(db).kind(db) == TypeVarKind::TypingSelf {
479-
BoundTypeVarInstance::synthetic_self(db, *new_upper_bound)
477+
context.variables(db).iter().map(|typevar| {
478+
if typevar.typevar(db).is_self(db) {
479+
BoundTypeVarInstance::synthetic_self(
480+
db,
481+
*new_upper_bound,
482+
typevar.binding_context(db),
483+
)
480484
} else {
481-
*var
485+
*typevar
482486
}
483487
}),
484488
)

0 commit comments

Comments
 (0)