Thanks to visit codestin.com
Credit goes to github.com

Skip to content

__static_attributes__ class attribute from Python 3.13 #12136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lancelote opened this issue Jun 13, 2024 · 4 comments
Open

__static_attributes__ class attribute from Python 3.13 #12136

lancelote opened this issue Jun 13, 2024 · 4 comments

Comments

@lancelote
Copy link
Contributor

Python 3.13 introduced __static_attributes__ special attribute for classes. The trick is - it is not defined on object ...

AttributeError: type object 'object' has no attribute '__static_attributes__'

What is the best way to declare it in typeshed? As object attribute anyway? Or there is no way atm?

@AlexWaygood
Copy link
Member

I believe it's only defined on pure-Python classes, which isn't expressible in the type system. Probably the only safe way of accessing this attribute is by doing something like getattr(obj, "__static_attributes__", ()), which I think means we maybe shouldn't add it to typeshed. On the mypy master branch, stubtest already knows not to emit an error if the attribute's not there, and we're using the mypy master branch for our stubtest checks in CI at the moment.

There's a similar situation with the new __firstlineno__ class attribute, also added in py313.

@JelleZijlstra
Copy link
Member

The same thing is true for type.__abstractmethods__, which is not in the stub.

@srittau
Copy link
Collaborator

srittau commented Jun 14, 2024

We usually tend to add sometimes-defined attributes (with a comment) on the policy that false negatives are preferable to false positives. That said, maybe it's time to turn python/typing#601 into a PEP to formalize this behavior.

@not-my-profile
Copy link
Contributor

Could Python define __static_attributes__ as an empty tuple for C extension classes? Then we could easily type it.

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

No branches or pull requests

5 participants