Skip to content
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

Fix bug with mypy plugin and no_strict_optional = True #8666

Merged
merged 3 commits into from Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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