Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mypy plugin: More precisely detect when fields are required. (#4086)
The mypy plugin would previously incorrectly determine that a field was not required in a few scenarios where the field really is required. This specifically affected cases when the `Field()` function is used, where the plugin assumed that the first argument would always be `default`. This changes the code to examine each argument more closely, and it now properly handles several more scenarios where the default is explicitly named or when the default_factory named argument is used.
- Loading branch information
1 parent
da8f0c0
commit 02a076d
Showing
5 changed files
with
33 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve mypy plugin's ability to detect required fields. |
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,17 @@ | ||
from pydantic import BaseModel, Field | ||
|
||
|
||
class Model(BaseModel): | ||
# Required | ||
undefined_default_no_args: int = Field() | ||
undefined_default: int = Field(description='my desc') | ||
positional_ellipsis_default: int = Field(...) | ||
named_ellipsis_default: int = Field(default=...) | ||
|
||
# Not required | ||
positional_default: int = Field(1) | ||
named_default: int = Field(default=2) | ||
named_default_factory: int = Field(default_factory=lambda: 3) | ||
|
||
|
||
Model() |
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,4 @@ | ||
17: error: Missing named argument "undefined_default_no_args" for "Model" [call-arg] | ||
17: error: Missing named argument "undefined_default" for "Model" [call-arg] | ||
17: error: Missing named argument "positional_ellipsis_default" for "Model" [call-arg] | ||
17: error: Missing named argument "named_ellipsis_default" for "Model" [call-arg] |
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