Skip to content

Commit

Permalink
allow self type
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Dec 28, 2024
1 parent 62d43b8 commit 26618b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ def analyze_class_attribute_access(
if not node.node.is_classvar and node.node.info.self_type:
def_vars.add(node.node.info.self_type)
typ_vars = set(get_type_vars(t))
if def_vars & typ_vars:
if any(tv for tv in def_vars & typ_vars if tv.upper_bound != itype):
if node.node.is_classvar:
message = message_registry.GENERIC_CLASS_VAR_ACCESS
else:
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,22 @@ xi = C[int].x # E: Access to generic instance variables via class is ambiguous
reveal_type(xi) # N: Revealed type is "builtins.int"
[builtins fixtures/classmethod.pyi]

[case testGenericClassAttrSelfType]
from typing import TypeVar, Generic, Self, Type, Mapping

class A:
d: Mapping[str, Self]

@classmethod
def make(cls) -> Self:
return cls.d["asdf"]

def main(A_t: Type[A]) -> None:
reveal_type(A.d) # N: Revealed type is "typing.Mapping[builtins.str, __main__.A]"
reveal_type(A_t.d) # N: Revealed type is "typing.Mapping[builtins.str, __main__.A]"

[builtins fixtures/classmethod.pyi]

[case testGenericClassAttrUnboundOnSubClass]
from typing import Generic, TypeVar, Tuple
T = TypeVar('T')
Expand Down

0 comments on commit 26618b0

Please sign in to comment.