typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

`__static_attributes__` class attribute from Python 3.13

Open lancelote opened this issue 1 year ago • 3 comments

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?

lancelote avatar Jun 13 '24 10:06 lancelote

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.

AlexWaygood avatar Jun 13 '24 10:06 AlexWaygood

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

JelleZijlstra avatar Jun 13 '24 13:06 JelleZijlstra

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.

srittau avatar Jun 14 '24 09:06 srittau

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

not-my-profile avatar Mar 22 '25 18:03 not-my-profile