Skip to content

Commit a5e2ad7

Browse files
committed
allow self type
1 parent 62d43b8 commit a5e2ad7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

mypy/checkmember.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ def analyze_class_attribute_access(
10861086
if not node.node.is_classvar and node.node.info.self_type:
10871087
def_vars.add(node.node.info.self_type)
10881088
typ_vars = set(get_type_vars(t))
1089-
if def_vars & typ_vars:
1089+
if any(tv for tv in def_vars & typ_vars if tv.upper_bound != itype):
10901090
if node.node.is_classvar:
10911091
message = message_registry.GENERIC_CLASS_VAR_ACCESS
10921092
else:

test-data/unit/check-generics.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,22 @@ xi = C[int].x # E: Access to generic instance variables via class is ambiguous
22332233
reveal_type(xi) # N: Revealed type is "builtins.int"
22342234
[builtins fixtures/classmethod.pyi]
22352235

2236+
[case testGenericClassAttrSelfType]
2237+
from typing import TypeVar, Generic, Self, Type, Mapping
2238+
2239+
class A:
2240+
d: Mapping[str, Self]
2241+
2242+
@classmethod
2243+
def make(cls) -> Self:
2244+
return cls.d["asdf"] # type: ignore
2245+
2246+
def main(A_t: Type[A]) -> None:
2247+
reveal_type(A.d) # N: Revealed type is "typing.Mapping[builtins.str, __main__.A]"
2248+
reveal_type(A_t.d) # N: Revealed type is "typing.Mapping[builtins.str, __main__.A]"
2249+
2250+
[builtins fixtures/classmethod.pyi]
2251+
22362252
[case testGenericClassAttrUnboundOnSubClass]
22372253
from typing import Generic, TypeVar, Tuple
22382254
T = TypeVar('T')

0 commit comments

Comments
 (0)