Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork411
Implement pyright support via dataclass_transforms#796
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
52d769c18913734f3787cbba9d7a70e428641b41498abde8a2a4f8df822722b55a06757c21153e8a4c0d6d10e305fa3b2d59714cf68e86bf693e71be63087b7876c40File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -86,6 +86,16 @@ else: | ||
| takes_self: bool = ..., | ||
| ) -> _T: ... | ||
| # Static type inference support via __dataclass_transform__ implemented as per: | ||
| # https://github.com/microsoft/pyright/blob/1.1.135/specs/dataclass_transforms.md | ||
| # This annotation must be applied to all overloads of "define" and "attrs" | ||
| def __dataclass_transform__( | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. There should probably be an implementation of this somewhere right? Otherwise this won't be useful outside of attrs. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is a little strange, the decorator is just being used as a marker for Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Hmm. I'm a little concerned (ok not too much but it definitely feels weird) about having a ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 🤔 I've done a little test of this. Under the 1.1.135
Then a custom annotation of the form has types inferred properly. importattr@attr.__dataclass_transform__(fields=(attr.field,attr.attrib))defcustom_define(cls):returnattr.define(cls)@custom_defineclassCustom:a:int However, given that this is a very early and provisional specification and implementation in
Given how bleeding-edge this feature is I have a weakly held preference that we opt for 2 for this release to ensure that folks don't implictly rely on the Of course, happy to reconsider. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Since this is only for pyright and doesn't currently get mypy to do the right thing (i.e. it doesn't tell mypy that this is an attrs class creator) perhaps it's best if we leave this only inside the pyi file. I might add a comment on here that says that this method doesn't actually exist at runtime. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Roger, comment added. | ||
| *, | ||
| eq_default: bool = True, | ||
| order_default: bool = False, | ||
| kw_only_default: bool = False, | ||
| field_descriptors: Tuple[Union[type, Callable[..., Any]], ...] = (()), | ||
| ) -> Callable[[_T], _T]: ... | ||
| class Attribute(Generic[_T]): | ||
| name: str | ||
| @@ -276,6 +286,7 @@ def field( | ||
| on_setattr: Optional[_OnSetAttrArgType] = ..., | ||
| ) -> Any: ... | ||
| @overload | ||
| @__dataclass_transform__(order_default=True, field_descriptors=(attrib, field)) | ||
| def attrs( | ||
| maybe_cls: _C, | ||
| these: Optional[Dict[str, Any]] = ..., | ||
| @@ -301,6 +312,7 @@ def attrs( | ||
| field_transformer: Optional[_FieldTransformer] = ..., | ||
| ) -> _C: ... | ||
| @overload | ||
| @__dataclass_transform__(order_default=True, field_descriptors=(attrib, field)) | ||
| def attrs( | ||
| maybe_cls: None = ..., | ||
| these: Optional[Dict[str, Any]] = ..., | ||
| @@ -326,6 +338,7 @@ def attrs( | ||
| field_transformer: Optional[_FieldTransformer] = ..., | ||
| ) -> Callable[[_C], _C]: ... | ||
| @overload | ||
| @__dataclass_transform__(field_descriptors=(attrib, field)) | ||
| def define( | ||
| maybe_cls: _C, | ||
| *, | ||
| @@ -349,6 +362,7 @@ def define( | ||
| field_transformer: Optional[_FieldTransformer] = ..., | ||
| ) -> _C: ... | ||
| @overload | ||
| @__dataclass_transform__(field_descriptors=(attrib, field)) | ||
| def define( | ||
| maybe_cls: None = ..., | ||
| *, | ||