![](https://secure.gravatar.com/avatar/57da4d2e2a527026baaaab35e6872fa5.jpg?s=120&d=mm&r=g)
El mar, 22 feb 2022 a las 11:34, Erik De Bonte (<Erik.DeBonte@microsoft.com>) escribió:
We’re considering an addition to PEP 681 (dataclass_transform) [1] to better support classes with fields that are descriptors. Setting a new dataclass_transform parameter named “transform_descriptor_types” to True would indicate that __init__ parameters corresponding to descriptor fields have the type of the descriptor’s setter value parameter rather than the descriptor type.
Here’s an example:
@dataclass_transform(transform_descriptor_types=True)
def decorator() -> Callable[[Type[T]], Type[T]]: ...
class Descriptor(Generic[_T]):
def __get__(self, instance: object, owner: Any) -> Any: ...
def __set__(self, instance: object, value: T | None): ...
@decorator
class InventoryItem:
quantity_on_hand: Descriptor[int]
In this case, type checkers would understand that the quantity_on_hand parameter of InventoryItem’s __init__ method is of type “int | None” rather than “Descriptor[int]”.
This change was driven by a need in SQLAlchemy, and we’d appreciate any thoughts on broader applicability.
Feedback is welcome either via email or on GitHub! [2] A reference implementation is available in Pyright 1.1.222 and Pylance 2022.2.3.
I think this is a useful change and I support including it in PEP 681. I like how it integrates nicely with the descriptor protocol, so it's potentially useful for libraries other than SQLAlchemy too.