Skip to content

Allow errors for unsound Self definitions on non-final classes - #2221

Merged
JelleZijlstra merged 9 commits into
python:mainfrom
davidhalter:allow-unsound-error-self-classvar
Apr 2, 2026
Merged

Allow errors for unsound Self definitions on non-final classes#2221
JelleZijlstra merged 9 commits into
python:mainfrom
davidhalter:allow-unsound-error-self-classvar

Conversation

@davidhalter

Copy link
Copy Markdown
Collaborator

This comes after #2148 which wants to make this a mandatory error.

But for now it should be totally fine to allow type checkers to error here, since it can clearly be shown to be unsound, see also:

https://discuss.python.org/t/disallow-access-to-class-variables-with-self/105434

I'm happy to revisit the original pull request, but for now I hope this should suffice. I will rerun the automated tests if and once no changes are required.

@srittausrittau added the topic: conformance tests Issues with the conformance test suite label Mar 17, 2026
@davidhalter

Copy link
Copy Markdown
CollaboratorAuthor

@JelleZijlstra Would you mind reviewing this? IMO it's a very uncontroversial change.

The problem is (quoting from discuss):

from typing import Self, assert_type
class ParentB:
a: list[Self]
@classmethod
def method4(cls) -> None:
cls.a = [cls()]
class ChildB(ParentB):
@classmethod
def method3(cls) -> None:
assert_type(cls, type[Self])
assert_type(cls.a, list[Self]) # This can be a different type, but is not in conformance tests
assert_type(cls.a[0], Self) # Same
print(cls.a)
ParentB.method4()
ChildB.method3() # Prints [<__main__.ParentB object>]

There is currently no part of the spec that describes this.

Comment threadconformance/tests/generics_self_advanced.py
@davidhalter
davidhalterforce-pushed the allow-unsound-error-self-classvar branch from 2f2b8d1 to ac4622fCompareMarch 30, 2026 19:47
TypeForm[Any] is not considered equivalent to itself.
"""
conformance_automated = "Fail"
conformance_automated = "Pass"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd prefer to keep this in a separate PR now that we allow fixing type checker versions, I can prepare a PR now to update it though.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Makes sense, I'll try to revert it.

@davidhalterdavidhalterMar 31, 2026

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Zuban is now at the same version as before and the results are much more understandable

@JelleZijlstra
JelleZijlstra merged commit 859d3d3 into python:mainApr 2, 2026
5 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: conformance testsIssues with the conformance test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@davidhalter@carljm@JelleZijlstra@srittau