FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Change the spec for class access of Self variables by davidhalter · Pull Request #2148 · python/typing · GitHub

/ typing Public
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 4 additions & 2 deletions conformance/tests/generics_self_advanced.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)

ChildB.a # E: Ambigous access
ParentB.a = [ChildB.method1()] # E: Ambigous write
16 changes: 16 additions & 0 deletions docs/spec/generics.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL