Skip to content

Commit 193e605

Browse files
committed
more
1 parent 26618b0 commit 193e605

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

mypy/checkmember.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,13 +1085,23 @@ def analyze_class_attribute_access(
10851085
def_vars = set(node.node.info.defn.type_vars)
10861086
if not node.node.is_classvar and node.node.info.self_type:
10871087
def_vars.add(node.node.info.self_type)
1088+
10881089
typ_vars = set(get_type_vars(t))
1089-
if any(tv for tv in def_vars & typ_vars if tv.upper_bound != itype):
1090+
for tv in def_vars & typ_vars:
1091+
if (
1092+
# Forgive access to variables generic over Self
1093+
isinstance(tv.upper_bound, Instance)
1094+
and super_info
1095+
and tv.upper_bound.type.fullname == super_info.fullname
1096+
):
1097+
continue
10901098
if node.node.is_classvar:
10911099
message = message_registry.GENERIC_CLASS_VAR_ACCESS
10921100
else:
10931101
message = message_registry.GENERIC_INSTANCE_VAR_CLASS_ACCESS
10941102
mx.msg.fail(message, mx.context)
1103+
break
1104+
10951105
t = expand_self_type_if_needed(t, mx, node.node, itype, is_class=True)
10961106
# Erase non-mapped variables, but keep mapped ones, even if there is an error.
10971107
# In the above example this means that we infer following types:

test-data/unit/check-generics.test

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2243,10 +2243,16 @@ class A:
22432243
def make(cls) -> Self:
22442244
return cls.d["asdf"]
22452245

2246-
def main(A_t: Type[A]) -> None:
2246+
class B(A): ...
2247+
2248+
def check_A(A_t: Type[A]) -> None:
22472249
reveal_type(A.d) # N: Revealed type is "typing.Mapping[builtins.str, __main__.A]"
22482250
reveal_type(A_t.d) # N: Revealed type is "typing.Mapping[builtins.str, __main__.A]"
22492251

2252+
def check_B(B_t: Type[B]) -> None:
2253+
reveal_type(B.d) # N: Revealed type is "typing.Mapping[builtins.str, __main__.B]"
2254+
reveal_type(B_t.d) # N: Revealed type is "typing.Mapping[builtins.str, __main__.B]"
2255+
22502256
[builtins fixtures/classmethod.pyi]
22512257

22522258
[case testGenericClassAttrUnboundOnSubClass]

test-data/unit/check-selftype.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ class C:
14851485
class D(C): ...
14861486

14871487
reveal_type(C.meth) # N: Revealed type is "def [Self <: __main__.C] (self: Self`1) -> builtins.list[Self`1]"
1488-
C.attr # E: Access to generic instance variables via class is ambiguous
1488+
C.attr
14891489
reveal_type(D().meth()) # N: Revealed type is "builtins.list[__main__.D]"
14901490
reveal_type(D().attr) # N: Revealed type is "builtins.list[__main__.D]"
14911491

0 commit comments

Comments
 (0)