Skip to content

Comments

Prohibit access via class for instance-only attributes#20855

Merged
ilevkivskyi merged 1 commit intopython:masterfrom
ilevkivskyi:prohibit-class-access-on-instance-var
Feb 21, 2026
Merged

Prohibit access via class for instance-only attributes#20855
ilevkivskyi merged 1 commit intopython:masterfrom
ilevkivskyi:prohibit-class-access-on-instance-var

Conversation

@ilevkivskyi
Copy link
Member

Fixes #240 (yes, you read that right, three digits)

I am taking a conservative approach and flag only cases that would definitely fail at runtime. Note we already have a more strict check for this for generics, but it is tricky to avoid double errors, so I think it is fine to have two errors sometimes.

I also update an outdated comment that confused me for a while.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

setuptools (https://github.com/pypa/setuptools)
+ setuptools/config/_apply_pyprojecttoml.py:328: error: Cannot access instance-only attribute "global_options" on class object  [misc]

@ilevkivskyi
Copy link
Member Author

setuptools error is correct. There is just too much monkey-patching for mypy to figure out where the base class with the attribute comes from.

@ilevkivskyi ilevkivskyi merged commit 87e9425 into python:master Feb 21, 2026
24 checks passed
@ilevkivskyi ilevkivskyi deleted the prohibit-class-access-on-instance-var branch February 21, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disallow access to instance variable via class

2 participants