-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dataclass_transform] minimal implementation of dataclass_transform (#…
…14523) This is a very simple first step to implementing [PEP 0681](https://peps.python.org/pep-0681/#decorator-function-example), which will allow MyPy to recognize user-defined types that behave similarly to dataclasses. This initial implementation is very limited: we only support decorator-style use of `typing.dataclass_transform` and do not support passing additional options to the transform (such as `freeze` or `init`). Within MyPy, we add a new `is_dataclass_transform` field to `FuncBase` which is populated during semantic analysis. When we check for plugin hooks later, we add new special cases to use the existing dataclasses plugin if a class decorator is marked with `is_dataclass_transform`. Ideally we would use a proper plugin API; the hacky special case here can be replaced in subsequent iterations. Co-authored-by: Wesley Wright <wesleyw@dropbox.com>
- Loading branch information
1 parent
6442b02
commit bac9e77
Showing
10 changed files
with
106 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
[case testDataclassTransformReusesDataclassLogic] | ||
# flags: --python-version 3.7 | ||
from typing import dataclass_transform, Type | ||
|
||
@dataclass_transform() | ||
def my_dataclass(cls: Type) -> Type: | ||
return cls | ||
|
||
@my_dataclass | ||
class Person: | ||
name: str | ||
age: int | ||
|
||
def summary(self): | ||
return "%s is %d years old." % (self.name, self.age) | ||
|
||
reveal_type(Person) # N: Revealed type is "def (name: builtins.str, age: builtins.int) -> __main__.Person" | ||
Person('John', 32) | ||
Person('Jonh', 21, None) # E: Too many arguments for "Person" | ||
|
||
[typing fixtures/typing-medium.pyi] | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
[case testDataclassTransformIsFoundInTypingExtensions] | ||
# flags: --python-version 3.7 | ||
from typing import Type | ||
from typing_extensions import dataclass_transform | ||
|
||
@dataclass_transform() | ||
def my_dataclass(cls: Type) -> Type: | ||
return cls | ||
|
||
@my_dataclass | ||
class Person: | ||
name: str | ||
age: int | ||
|
||
def summary(self): | ||
return "%s is %d years old." % (self.name, self.age) | ||
|
||
reveal_type(Person) # N: Revealed type is "def (name: builtins.str, age: builtins.int) -> __main__.Person" | ||
Person('John', 32) | ||
Person('Jonh', 21, None) # E: Too many arguments for "Person" | ||
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters