Skip to content

Commit 75243de

Browse files
committed
Fix context for class variables, fixes #127
1 parent 6b65a91 commit 75243de

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

crates/zuban_python/src/file/inference.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -885,15 +885,33 @@ impl<'db, 'file> Inference<'db, 'file, '_> {
885885
if cls.is_calculating_class_infos() {
886886
return None;
887887
}
888-
cls.instance()
889-
.lookup(
890-
self.i_s,
891-
name_def.as_code(),
892-
InstanceLookupOptions::new(&|_| ())
893-
.with_skip_first_of_mro(self.i_s.db, cls),
894-
)
895-
.lookup
896-
.into_maybe_inferred()
888+
cls.lookup(
889+
self.i_s,
890+
name_def.as_code(),
891+
ClassLookupOptions::new(&|_| ()).with_ignore_self(),
892+
)
893+
.lookup
894+
.into_maybe_inferred()
895+
.and_then(|x| {
896+
if let Some(ComplexPoint::TypeInstance(Type::Class(c))) =
897+
x.maybe_complex_point(self.i_s.db)
898+
&& c.link == self.i_s.db.python_state.property_link()
899+
{
900+
// Properties can beo overwritten with the property value type as a
901+
// normal variable. Here we make sure that the context is correct.
902+
return cls
903+
.instance()
904+
.lookup(
905+
self.i_s,
906+
name_def.as_code(),
907+
InstanceLookupOptions::new(&|_| ())
908+
.with_skip_first_of_mro(self.i_s.db, cls),
909+
)
910+
.lookup
911+
.into_maybe_inferred();
912+
}
913+
Some(x)
914+
})
897915
})
898916
.or_else(|| {
899917
Some(

crates/zuban_python/src/python_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,7 @@ impl PythonState {
10911091
attribute_link!(builtins, pub frozenset_link, builtins_frozenset_index);
10921092
attribute_link!(builtins, pub notimplementederror_link, builtins_notimplementederror);
10931093
attribute_link!(builtins, pub slice_link, builtins_slice_index);
1094+
attribute_link!(builtins, pub property_link, builtins_property_index);
10941095
attribute_link!(abc, pub abc_meta_link, abc_abc_meta_index);
10951096
attribute_link!(abc, pub abstractmethod_link, abc_abstractmethod_index);
10961097
attribute_link!(abc, pub abstractproperty_link, abc_abstractproperty_index);

crates/zuban_python/tests/mypylike/tests/classes.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,3 +2096,19 @@ reveal_type(y) # N: Revealed type is "Any"
20962096
Test().y = '' # E: "Test" has no attribute "y"
20972097
Test().with_annotation1 # E: "Test" has no attribute "with_annotation1"
20982098
Test().with_annotation2 # E: "Test" has no attribute "with_annotation2"
2099+
2100+
[case context_for_class_vars_should_not_be_instance_method]
2101+
import m
2102+
2103+
def wrap[T](obj: T) -> T: ...
2104+
2105+
class A(m.Base):
2106+
y = wrap(m.Base.f)
2107+
f = wrap(m.Base.f)
2108+
2109+
A().y()
2110+
A().f()
2111+
2112+
[file m.pyi]
2113+
class Base:
2114+
def f(self) -> None: ...

0 commit comments

Comments
 (0)