Skip to content
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

PEP 681: Descriptor fields #2369

Merged
merged 8 commits into from
Mar 15, 2022
Merged

Conversation

debonte
Copy link
Contributor

@debonte debonte commented Feb 27, 2022

Improves support for classes with fields that are descriptors. Setting a new dataclass_transform parameter named transform_descriptor_types to True indicates that __init__ parameters corresponding to descriptor fields have the type of the descriptor’s __set__ value parameter rather than the descriptor type.

In the following example, type checkers would understand that the quantity_on_hand parameter of the __init__ method on InventoryItem is of type int | None rather than Descriptor[int].

@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]

This change was driven by a need in SQLAlchemy as discussed at microsoft/pyright#2958.

A reference implementation is available in Pyright 1.1.222 and Pylance 2022.2.3.

pep-0681.rst Outdated Show resolved Hide resolved
Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. A few copyediting fixes and improvements.

pep-0681.rst Outdated Show resolved Hide resolved
pep-0681.rst Outdated Show resolved Hide resolved
pep-0681.rst Outdated Show resolved Hide resolved
pep-0681.rst Outdated Show resolved Hide resolved
debonte and others added 2 commits February 28, 2022 13:04
@debonte
Copy link
Contributor Author

debonte commented Feb 28, 2022

@JelleZijlstra there was a Typing SIG email from Carl Meyer today that looks like it will result in some changes to transform_descriptor_types. Should we hold off on completing this PR?

@JelleZijlstra
Copy link
Member

Up to you. There's no harm in having more PRs later. I'm happy to merge this now if you would like me to.

Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @debonte . This is good to merge if you are happy with it; my suggestion would be that it is probably best to just merge this now that we've all approved, so they go live now as a discrete commit, and then open a followup PR with additional changes. Then, its easier for both you and us to focus on the new changes, and keeping things in scope.

@debonte
Copy link
Contributor Author

debonte commented Mar 15, 2022

@JelleZijlstra, can you please merge this? I'll submit another PR to addresss Carl Meyer's feedback as CAM-Gerlach suggested above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants