You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the common case of a descriptor which behaves like a normal attribute, infer:
class X:
y: int = descriptor()
to:
class X:
y: descriptor[int] = descriptor()
if descriptor is a Generic with one parameter. Extra checks should also be done to make sure that the descriptor is properly typed to behave like an attribute of that type (__get__ and __set__ expect that type on an instance).
Pitch
Type annotations apply to object instances by default, so it's strange to see y: descriptor[int] as type annotation while x.y returns an int.
You could also see a normal attribute as an invisible descriptor whose behavior is to set __dict__[name] (and raise AttributeError on the class), so this could be a way to unify these things.
Is there any way to do this without a plugin ? I'm so frustrated, seeing what descriptor is able to do, it's so disappointing to not be able to properly type the attribute.
Also my implementation of a descriptor is used as a convenient factory that gets the concrete type from the __annotations__ of the owner object.
def __set_name__(self, owner: Any, name: str) -> None:
self.public_name = name
self.private_name = "_" + name
self.type_ = owner.__annotations__[self.public_name]
So it would be nice to also have a way to just ignore the type of __get__ in that case, the annotation is just the only typing necessary.
Feature
For the common case of a descriptor which behaves like a normal attribute, infer:
to:
if descriptor is a Generic with one parameter. Extra checks should also be done to make sure that the descriptor is properly typed to behave like an attribute of that type (
__get__
and__set__
expect that type on an instance).Pitch
Type annotations apply to object instances by default, so it's strange to see
y: descriptor[int]
as type annotation whilex.y
returns an int.You could also see a normal attribute as an invisible descriptor whose behavior is to set
__dict__[name]
(and raise AttributeError on the class), so this could be a way to unify these things.It's also related to:
#7724
#5481
I implemented it partially as a plugin but only for my custom descriptors.
The text was updated successfully, but these errors were encountered: