Skip to content

Commit 50259d7

Browse files
dmontagusydney-runkle
authored andcommitted
Fix bug with mypy plugin and no_strict_optional = True (#8666)
1 parent afd0aa8 commit 50259d7

5 files changed

Lines changed: 53 additions & 2 deletions

File tree

pydantic/mypy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,9 @@ def transform(self) -> bool:
475475
return False
476476

477477
is_settings = any(base.fullname == BASESETTINGS_FULLNAME for base in info.mro[:-1])
478-
self.add_initializer(fields, config, is_settings, is_root_model)
479-
self.add_model_construct_method(fields, config, is_settings)
480478
try:
479+
self.add_initializer(fields, config, is_settings, is_root_model)
480+
self.add_model_construct_method(fields, config, is_settings)
481481
self.set_frozen(fields, frozen=config.frozen is True)
482482
except _DeferAnalysis:
483483
if not self._api.final_iteration:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[build-system]
2+
requires = ["poetry>=0.12"]
3+
build_backend = "poetry.masonry.api"
4+
5+
[tool.poetry]
6+
name = "test"
7+
version = "0.0.1"
8+
readme = "README.md"
9+
authors = [
10+
"author@example.com"
11+
]
12+
13+
[tool.poetry.dependencies]
14+
python = "*"
15+
16+
[tool.pytest.ini_options]
17+
addopts = "-v -p no:warnings"
18+
19+
[tool.mypy]
20+
plugins = [
21+
"pydantic.mypy"
22+
]
23+
follow_imports = "silent"
24+
no_strict_optional = true
25+
warn_redundant_casts = true
26+
warn_unused_ignores = true
27+
disallow_any_generics = true
28+
check_untyped_defs = true
29+
no_implicit_reexport = true
30+
disallow_untyped_defs = true
31+
32+
[[tool.mypy.overrides]]
33+
module = [
34+
'pydantic_core.*',
35+
]
36+
follow_imports = "skip"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import Union
2+
3+
from pydantic import BaseModel
4+
5+
6+
class MongoSettings(BaseModel):
7+
MONGO_PASSWORD: Union[str, None]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import Union
2+
3+
from pydantic import BaseModel
4+
5+
6+
class MongoSettings(BaseModel):
7+
MONGO_PASSWORD: Union[str, None]

tests/mypy/test_mypy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def build(self) -> List[Union[Tuple[str, str], Any]]:
110110
('mypy-plugin-strict-no-any.ini', 'dataclass_no_any.py'),
111111
('mypy-plugin-very-strict.ini', 'metaclass_args.py'),
112112
('pyproject-default.toml', 'computed_fields.py'),
113+
('pyproject-plugin-no-strict-optional.toml', 'no_strict_optional.py'),
113114
]
114115
)
115116

0 commit comments

Comments
 (0)