Skip to content

Commit

Permalink
Fix bug with mypy plugin and no_strict_optional = True (#8666)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Jan 30, 2024
1 parent c1dff15 commit 0404872
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pydantic/mypy.py
Expand Up @@ -475,9 +475,9 @@ def transform(self) -> bool:
return False

is_settings = any(base.fullname == BASESETTINGS_FULLNAME for base in info.mro[:-1])
self.add_initializer(fields, config, is_settings, is_root_model)
self.add_model_construct_method(fields, config, is_settings)
try:
self.add_initializer(fields, config, is_settings, is_root_model)
self.add_model_construct_method(fields, config, is_settings)
self.set_frozen(fields, frozen=config.frozen is True)
except _DeferAnalysis:
if not self._api.final_iteration:
Expand Down
36 changes: 36 additions & 0 deletions tests/mypy/configs/pyproject-plugin-no-strict-optional.toml
@@ -0,0 +1,36 @@
[build-system]
requires = ["poetry>=0.12"]
build_backend = "poetry.masonry.api"

[tool.poetry]
name = "test"
version = "0.0.1"
readme = "README.md"
authors = [
"author@example.com"
]

[tool.poetry.dependencies]
python = "*"

[tool.pytest.ini_options]
addopts = "-v -p no:warnings"

[tool.mypy]
plugins = [
"pydantic.mypy"
]
follow_imports = "silent"
no_strict_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
disallow_any_generics = true
check_untyped_defs = true
no_implicit_reexport = true
disallow_untyped_defs = true

[[tool.mypy.overrides]]
module = [
'pydantic_core.*',
]
follow_imports = "skip"
7 changes: 7 additions & 0 deletions tests/mypy/modules/no_strict_optional.py
@@ -0,0 +1,7 @@
from typing import Union

from pydantic import BaseModel


class MongoSettings(BaseModel):
MONGO_PASSWORD: Union[str, None]
@@ -0,0 +1,7 @@
from typing import Union

from pydantic import BaseModel


class MongoSettings(BaseModel):
MONGO_PASSWORD: Union[str, None]
1 change: 1 addition & 0 deletions tests/mypy/test_mypy.py
Expand Up @@ -111,6 +111,7 @@ def build(self) -> List[Union[Tuple[str, str], Any]]:
('mypy-plugin-very-strict.ini', 'metaclass_args.py'),
('pyproject-default.toml', 'computed_fields.py'),
('pyproject-default.toml', 'with_config_decorator.py'),
('pyproject-plugin-no-strict-optional.toml', 'no_strict_optional.py'),
]
)

Expand Down

0 comments on commit 0404872

Please sign in to comment.