8000 Do not expand root type in the mypy plugin for variables by Viicos · Pull Request #11676 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Do not expand root type in the mypy plugin for variables #11676

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

Merged
merged 1 commit into from
Apr 2, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Do not expand root type in the mypy plugin for variables
  • Loading branch information
Viicos committed Apr 2, 2025
commit 91ea2802a798016d6efa331fddb5440a36601b13
10 changes: 7 additions & 3 deletions pydantic/mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def to_argument(

strict = model_strict if self.strict is None else self.strict
if typed or strict:
type_annotation = self.expand_type(current_info, api)
type_annotation = self.expand_type(current_info, api, include_root_type=True)
else:
type_annotation = AnyType(TypeOfAny.explicit)

Expand All @@ -304,7 +304,11 @@ def to_argument(
)

def expand_type(
self, current_info: TypeInfo, api: SemanticAnalyzerPluginInterface, force_typevars_invariant: bool = False
self,
current_info: TypeInfo,
api: SemanticAnalyzerPluginInterface,
force_typevars_invariant: bool = False,
include_root_type: bool = False,
) -> Type | None:
"""Based on mypy.plugins.dataclasses.DataclassAttribute.expand_type."""
if force_typevars_invariant:
Expand Down Expand Up @@ -332,7 +336,7 @@ def expand_type(
arg.variance = INVARIANT

expanded_type = expand_type(self.type, {self.info.self_type.id: filled_with_typevars})
if isinstance(expanded_type, Instance) and is_root_model(expanded_type.type):
if include_root_type and isinstance(expanded_type, Instance) and is_root_model(expanded_type.type):
# When a root model is used as a field, Pydantic allows both an instance of the root model
# as well as instances of the `root` field type:
root_type = cast(Type, expanded_type.type['root'].type)
Expand Down
6 changes: 5 additions & 1 deletion tests/mypy/modules/root_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Generic, TypeVar

from typing_extensions import assert_type

from pydantic import BaseModel, RootModel


Expand Down Expand Up @@ -40,5 +42,7 @@ class Model(BaseModel, Generic[V]):


Model[str](m1=1, m2='dog', m3=[])
Model[str](m1=Maybe(None), m2=Maybe('dog'), m3=Maybe([]))
m = Model[str](m1=Maybe(None), m2=Maybe('dog'), m3=Maybe([]))
Model(m1=None, m2={}, m3=[])

assert_type(m.m1, Maybe[int])
6 changes: 5 additions & 1 deletion tests/mypy/outputs/1.10.1/mypy-default_ini/root_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Generic, TypeVar

from typing_extensions import assert_type

from pydantic import BaseModel, RootModel


Expand Down Expand Up @@ -45,8 +47,10 @@ class Model(BaseModel, Generic[V]):
# MYPY: error: Argument "m1" to "Model" has incompatible type "int"; expected "Maybe[int]" [arg-type]
# MYPY: error: Argument "m2" to "Model" has incompatible type "str"; expected "Maybe[str]" [arg-type]
# MYPY: error: Argument "m3" to "Model" has incompatible type "list[Never]"; expected "Maybe[Any]" [arg-type]
Model[str](m1=Maybe(None), m2=Maybe('dog'), m3=Maybe([]))
m = Model[str](m1=Maybe(None), m2=Maybe('dog'), m3=Maybe([]))
Model(m1=None, m2={}, m3=[])
# MYPY: error: Argument "m1" to "Model" has incompatible type "None"; expected "Maybe[int]" [arg-type]
# MYPY: error: Argument "m2" to "Model" has incompatible type "dict[Never, Never]"; expected "Maybe[Never]" [arg-type]
# MYPY: error: Argument "m3" to "Model" has incompatible type "list[Never]"; expected "Maybe[Any]" [arg-type]

assert_type(m.m1, Maybe[int])
6 changes: 5 additions & 1 deletion tests/mypy/outputs/1.10.1/mypy-plugin_ini/root_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Generic, TypeVar

from typing_extensions import assert_type

from pydantic import BaseModel, RootModel


Expand Down Expand Up @@ -43,5 +45,7 @@ class Model(BaseModel, Generic[V]):


Model[str](m1=1, m2='dog', m3=[])
Model[str](m1=Maybe(None), m2=Maybe('dog'), m3=Maybe([]))
m = Model[str](m1=Maybe(None), m2=Maybe('dog'), m3=Maybe([]))
Model(m1=None, m2={}, m3=[])

assert_type(m.m1, Maybe[int])
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Generic, TypeVar

from typing_extensions import assert_type

from pydantic import BaseModel, RootModel


Expand Down Expand Up @@ -45,8 +47,10 @@ class Model(BaseModel, Generic[V]):
# MYPY: error: Argument "m1" to "Model" has incompatible type "int"; expected "Maybe[int]" [arg-type]
# MYPY: error: Argument "m2" to "Model" has incompatible type "str"; expected "Maybe[str]" [arg-type]
# MYPY: error: Argument "m3" to "Model" has incompatible type "list[Never]"; expected "Maybe[Any]" [arg-type]
Model[str](m1=Maybe(None), m2=Maybe('dog'), m3=Maybe([]))
m = Model[str](m1=Maybe(None), m2=Maybe('dog'), m3=Maybe([]))
Model(m1=None, m2={}, m3=[])
# MYPY: error: Argument "m1" to "Model" has incompatible type "None"; expected "Maybe[int]" [arg-type]
# MYPY: error: Argument "m2" to "Model" has incompatible type "dict[Never, Never]"; expected "Maybe[Never]" [arg-type]
# MYPY: error: Argument "m3" to "Model" has incompatible type "list[Never]"; expected "Maybe[Any]" [arg-type]

assert_type(m.m1, Maybe[int])
Loading
0