-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Validate signatures of dataclass __post_init__ methods
#22730
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
crates/ty_python_semantic/resources/mdtest/dataclasses/post_init.md
This file contains hidden or 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,175 @@ | ||
| # Tests for the `__post_init__` method | ||
|
|
||
| At runtime, if a dataclass has a `__post_init__` method then all `InitVar`-annotated fields will be | ||
| passed as positional arguments to that method as the final statement in the dataclass's generated | ||
| `__init__` method. The `__post_init__` signature must therefore be compatible with the fields of the | ||
| dataclass: | ||
|
|
||
| ```py | ||
| from dataclasses import dataclass, InitVar | ||
|
|
||
| @dataclass | ||
| class Empty1: | ||
| def __post_init__(self): ... # fine | ||
|
|
||
| @dataclass | ||
| class Empty2: | ||
| def __post_init__(self) -> None: ... # fine | ||
|
|
||
| @dataclass | ||
| class Empty3: | ||
| # The returned value is discarded, | ||
| # so arbitrary return annotations are allowed | ||
| def __post_init__(self) -> int: | ||
| return 42 | ||
|
|
||
| @dataclass | ||
| class Empty4: | ||
| def __post_init__(self, *args): ... # fine | ||
|
|
||
| @dataclass | ||
| class Empty5: | ||
| def __post_init__(self, **kwargs): ... # fine | ||
|
|
||
| @dataclass | ||
| class Empty6: | ||
| def __post_init__(self, *args, **kargs): ... # fine | ||
|
|
||
| @dataclass | ||
| class Empty7: | ||
| # no arguments will be passed to this method at runtime, | ||
| # because there are no `InitVar` fields on the class, | ||
| # so this is an error: | ||
| # | ||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, required_argument: int): ... | ||
|
|
||
| @dataclass | ||
| class Empty8: | ||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, *, required_argument): ... | ||
|
|
||
| @dataclass | ||
| class Empty9: | ||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, required_argument, /): ... | ||
|
|
||
| @dataclass | ||
| class SingleField: | ||
| x: int | ||
|
|
||
| # `x` will not be passed to `__post_init__`, | ||
| # because it is not an `InitVar`, so this is an | ||
| # | ||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, x: int) -> None: ... | ||
|
|
||
| @dataclass | ||
| class SingleFieldGood: | ||
| x: int | ||
|
|
||
| # this is fine! | ||
| def __post_init__(self) -> None: ... | ||
|
|
||
| @dataclass | ||
| class HasInitVarNoParameter: | ||
| x: InitVar[int] | ||
|
|
||
| # error: [invalid-dataclass] | ||
| def __post_init__(self) -> None: ... | ||
|
|
||
| @dataclass | ||
| class HasInitVarDifferentParameterName: | ||
| x: InitVar[int] | ||
|
|
||
| # because arguments are always passed in positionally | ||
| # to `__post_init__` methods, we allow a parameter to | ||
| # have an arbitrary name as long as it is inferred has | ||
| # having a compatible type. So this is fine: | ||
| def __post_init__(self, xx) -> None: ... | ||
|
|
||
| @dataclass | ||
| class HasInitVarBadParameterType: | ||
| x: InitVar[int] | ||
|
|
||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, x: str) -> None: ... | ||
|
|
||
| @dataclass | ||
| class HasInitVarBadParameterKind: | ||
| x: InitVar[int] | ||
|
|
||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, *, x: int) -> None: ... | ||
|
|
||
| @dataclass | ||
| class HasInitVarGood: | ||
| x: InitVar[int] | ||
|
|
||
| def __post_init__(self, x: int) -> None: ... | ||
|
|
||
| @dataclass | ||
| class HasInitVarGoodPositionalOnly: | ||
| x: InitVar[int] | ||
|
|
||
| # arguments are always passed to `__post_init__` positionally at runtime, | ||
| # so this is fine | ||
| def __post_init__(self, x: int, /) -> None: ... | ||
|
|
||
| @dataclass | ||
| class LotsOfInitVarsBad: | ||
| a: int | ||
| b: InitVar[str] | ||
| c: InitVar[bytes] | ||
| d: int | ||
| e: int | ||
| f: InitVar[range] | ||
| g: int | ||
|
|
||
| # Only `InitVar` fields are passed in at runtime, so this is an | ||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, a: int, b: str, c: bytes, d: int, e: int, f: range, g: int): ... | ||
|
|
||
| @dataclass | ||
| class LotsOfInitVarsOutOfOrder: | ||
| a: int | ||
| b: InitVar[str] | ||
| c: InitVar[bytes] | ||
| d: int | ||
| e: int | ||
| f: InitVar[range] | ||
| g: int | ||
|
|
||
| # the parameters are in the wrong order, so this is an | ||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, c: bytes, b: str, f: range): ... | ||
|
|
||
| @dataclass | ||
| class LotsOfInitVarsGood: | ||
| a: int | ||
| b: InitVar[str] | ||
| c: InitVar[bytes] | ||
| d: int | ||
| e: int | ||
| f: InitVar[range] | ||
| g: int | ||
|
|
||
| def __post_init__(self, b: str, c: bytes, f: range): ... | ||
|
|
||
| @dataclass | ||
| class InitVarSubclassGood(LotsOfInitVarsGood): | ||
| h: InitVar[list[int]] | ||
| i: str | ||
| j: InitVar[bool] | ||
|
|
||
| def __post_init__(self, b: str, c: bytes, f: range, h: list[int], j: bool): ... | ||
|
|
||
| @dataclass | ||
| class InitVarSubclassBad(LotsOfInitVarsGood): | ||
| h: InitVar[list[int]] | ||
| i: str | ||
| j: InitVar[bool] | ||
|
|
||
| # error: [invalid-dataclass] | ||
| def __post_init__(self, h: list[int], j: bool, b: str, c: bytes, f: range): ... | ||
| ``` | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.