8000 mypy incorrectly identifies argument of method within a model_serializer as incorrect · Issue #10707 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content
mypy incorrectly identifies argument of method within a model_serializer as incorrect #10707
@dsayling

Description

@dsayling

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Given a function with a model_serializer decorator, and that function calls an instance method on the model, when I attempt to run mypy with the pydantic plugin enabled, then I incorrectly see two errors from mypy regarding the types to the call of the instance method.

demo.py:21: error: Missing positional argument "value" in call to "_calculate_is_hot" of "TemperatureModel"  [call-arg]
demo.py:21: error: Argument 1 to "_calculate_is_hot" of "TemperatureModel" has incompatible type "int"; expected "TemperatureModel"  [arg-type]
demo.py:26: error: Missing positional argument "value" in call to "_calculate_is_hot" of "TemperatureModel"  [call-arg]
demo.py:26: error: Argument 1 to "_calculate_is_hot" of "TemperatureModel" has incompatible type "int"; expected "TemperatureModel"  [arg-type]

Given the same as above, when I run mypy without the pydantic plugin enabled, mypy evaluates the example correctly with no issues.

Example Code

from typing import Literal
from pydantic import BaseModel, model_serializer


class TemperatureModel(BaseModel):
    unit: Literal["C", "F"]
    value: int

    def _calculate_is_hot(self, value: int) -> bool:
        if self.unit == "F":
            return value > 100
        return value > 38

    @model_serializer(when_used="json")
    def serialize_model(self) -> dict[str, int | str | bool]:
        var: int = self.value
        if self.unit == "F":
            return {
                "unit": "C",
                "value": int((self.value - 32) / 1.8),
                "is_hot": self._calculate_is_hot(var),
            }
        return {
            "unit": self.unit,
            "value": self.value,
            "is_hot": self._calculate_is_hot(var),
        }


temperature = TemperatureModel(unit="F", value=212)
print(temperature.model_dump_json())

Python, Pydantic & OS Version

pydantic version: 2.9.2
        pydantic-core version: 2.23.4
          pydantic-core build: profile=release pgo=false
                 install path: /Users/drewayling/Workspace/apps.autosar.authoring-tool/.venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.6 (main, Sep  6 2024, 19:03:47) [Clang 15.0.0 (clang-1500.3.9.4)]
                     platform: macOS-15.0.1-x86_64-i386-64bit
             related packages: pydantic-extra-types-2.9.0 typing_extensions-4.12.2 mypy-1.11.1
                       commit: unknown

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2topic-mypy pluginRelated to the mypy plugin

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0