Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix mypy with
default_factory=list
etc. (#4471)
* fix mypy with default_factory=list etc. * fix tests * add error case
- Loading branch information
1 parent
5a2ddec
commit 02cf7f5
Showing
7 changed files
with
39 additions
and
7 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 @@ | ||
Fix mypy plugin when using bare types like `list` and `dict` as `default_factory`. |
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,21 @@ | ||
""" | ||
See https://github.com/pydantic/pydantic/issues/4457 | ||
""" | ||
|
||
from typing import Dict, List | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
def new_list() -> List[int]: | ||
return [] | ||
|
||
|
||
class Model(BaseModel): | ||
l1: List[str] = Field(default_factory=list) | ||
l2: List[int] = Field(default_factory=new_list) | ||
l3: List[str] = Field(default_factory=lambda: list()) | ||
l4: Dict[str, str] = Field(default_factory=dict) | ||
l5: int = Field(default_factory=lambda: 123) | ||
l6_error: List[str] = Field(default_factory=new_list) | ||
l7_error: int = Field(default_factory=list) |
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,2 @@ | ||
20: error: Incompatible types in assignment (expression has type "List[int]", variable has type "List[str]") [assignment] | ||
21: error: Incompatible types in assignment (expression has type "List[Any]", variable has type "int") [assignment] |
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