If I use a descriptor as at the end of this description, there are two problems with how the generated documentation for Example.count
looks like:
__get__
method.class-atribute
and instance-atribute
which are both somewhat wrong. Technically it is a class attribute, but so are methods and property
s (that are also implemented as descriptors) and they don't get such a label either. Technically it's not an instance attribute, but from the usage perspective it acts like one. I believe a proper label would be data-descriptor
, but property
would probably be more familiar for normal users and the functionality is the same.You can see how the example is rendered here in a demo site that I've used for MkDocs experiments.
A variation of this is using a descriptor as a method decorator like the setter we use extensively with the Robot Framework project to avoid using normal propertys with dummy getters. Such attributes are currently shown as methods without any type information. You can see an example here in a site where we are creating our new Manual that will also incorporate API docs.
from typing import overload, Self
class PositiveInteger[T]:
def __set_name__(self, owner: T, name: str):
self.name = '_' + name
@overload
def __get__(self, instance: None, owner: type[T]) -> Self:
...
@overload
def __get__(self, instance: T, owner: type[T]) -> int:
...
def __get__(self, instance, owner) -> int | Self:
if instance is None:
return self
return getattr(instance, self.name)
def __set__(self, instance: T, value: int):
if value <= 0:
raise ValueError(f'{value} is not positive')
setattr(instance, self.name, value)
class Example:
"""Example using descriptor."""
count = PositiveInteger()
"""Count as a positive integer."""
def __init__(self, count: int = 1):
self.count = count
I'm not sure should this have been submitted to griffe instead or should I have submitted separate issues about different problems. Feel free to move or split as needed.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too