mypy
mypy copied to clipboard
Different error code when assigning the result of a function to an annotated field
Bug Report
playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=8c0d1a3a8e3f444fc12af3f803e18c59
from typing import Callable, TypeVar
_T = TypeVar("_T")
def Field(default_factory: Callable[[], _T]) -> _T: ... # type: ignore[empty]
def new_list() -> list[int]:
return []
class Model:
a: list[str] = Field(default_factory=new_list)
b: int = Field(default_factory=new_list)
The error for a is:
Argument "default_factory" to "Field" has incompatible type "Callable[[], list[int]]"; expected "Callable[[], list[str]]" [arg-type]
While the error for b is:
Incompatible types in assignment (expression has type "list[int]", variable has type "int") [assignment]
The error for b seems to be more correct/less confusing. Am I missing anything?
- Mypy version used: 1.12.0
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.12