Change the spec for class access of Self variables#2148
Draft
davidhalter wants to merge 2 commits intopython:mainfrom
Draft
Change the spec for class access of Self variables#2148davidhalter wants to merge 2 commits intopython:mainfrom
davidhalter wants to merge 2 commits intopython:mainfrom
Conversation
26f68f4 to
88422ea
Compare
carljm
reviewed
Jan 13, 2026
Member
carljm
left a comment
There was a problem hiding this comment.
I commented more generally at https://discuss.python.org/t/disallow-access-to-class-variables-with-self/105434/8
Comment on lines
46
to
47
| ClassB.a # E: Ambigous access | ||
| ClassB.a = ClassB.method1() # E: Ambigous write |
Member
There was a problem hiding this comment.
Are these supposed to be ChildB?
Contributor
Author
There was a problem hiding this comment.
You are right, this was a typo. My bad, thanks for looking into it!
3761f07 to
d159fd3
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I have tried to create a first draft for disallowing access to variables through the class object containing Self, as discussed here.
The general reception of this proposed change was pretty positive, but I guess we are all not sure how this rule should be worded.
The by far biggest issue is that I write:
where "contains an occurrence of Self" is according to Eric a bit of a new concept. I guess I understand that this is not well defined in the glossary or anywhere else and can of course be used for other things as well. I have tried to skim the Spec and found the following usages in the spec:
Anyor another gradual form.Self. SupportingSelfThere's also two times where "in" is used:
Selfin staticmethods.Selfdoes not add muchSelfin metaclasses.Selfconsistently refers to theI have not found other language about this. I have tried to search for "depends on", "references", "mentions", "introduces" and none of them are used.
However I feel like this concept is often talked in almost implicit ways about TypeVars/Self. For example in the chapter "Type Parameters as Parameters to Generics" (talks about
T1 = TypeVar("T2", default=list[T1])):I feel like there's a couple of other places where different words are used, but it's not like this concept is new.
Please let me know what you all think. I'm happy to improve the wording.