⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions conformance/tests/generics_self_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def method2(self) -> None:
@classmethod
def method3(cls) -> None:
assert_type(cls, type[Self])
assert_type(cls.a, list[Self])
assert_type(cls.a[0], Self)
cls.a # E: Access to generic instance variables via class is ambiguous
assert_type(cls.method1(), Self)

ClassB.a # E: Ambigous access
ClassB.a = ClassB.method1() # E: Ambigous write
Comment on lines +46 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these supposed to be ChildB?

16 changes: 16 additions & 0 deletions docs/spec/generics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2393,6 +2393,22 @@ containing a ``Self`` type as a ``property`` that returns that type:
def ordinal_value(self) -> str:
return str(self.value)


Accessing a variable through the class object that has a type annotation where
the annotation contains an occurrence of Self is not allowed, because Self can
be different depending on the class in the inheritance hierarchy:

::

class C:
others: list[Self]

class D(C): ...

C.others = [C()]
print(D.others) # This is not list[Self], but list[C]


Use in Generic Classes
^^^^^^^^^^^^^^^^^^^^^^

Expand Down