You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While this type checks correctly for some common use cases, we get errors when the Field function is assigned to a union of types, or when the default factory signature isn't trivial (e.g. contains overloads). Here are some examples failing:
cvar=ContextVar[None]("session_id", default=None)
classClass:
# error: Argument "default_factory" to "Field" has incompatible type "type[list[Any]]"; expected "Callable[[], Never] | Callable[[dict[str, Any]], Never]" [arg-type]:a: list|None=Field(default_factory=list)
# error: Argument "default_factory" to "Field" has incompatible type overloaded function; expected "Callable[[], Never] | Callable[[dict[str, Any]], Never]" [arg-type]:b: None=Field(default_factory=cvar.get) # note that `cvar.get` is overloaded. The first overload (`() -> None`) should match.# fine:fine: list=Field(default_factory=list)
Note that (at least for a), this worked as expected with the old type inference.
I first wanted to know if this can be considered a mypy limitation, or is the code really unsafe in some way? Or maybe is it because the typing spec does not provide any info regarding these use cases?
In comparison, pyright and pyre accepts the provided examples. We will currently recommend users to add a type: ignore comment and redirect to this issue. However, feel free to close if there's already an issue tracking this.
Thanks in advance
Your Environment
Mypy version used: 1.13
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): None
Python version used: 3.13
The text was updated successfully, but these errors were encountered:
Bug Report
Apologies if this was raised already. The only related issue I could find is #15887.
In Pydantic 2.10, we changed the definition of our field function to be:
While this type checks correctly for some common use cases, we get errors when the
Field
function is assigned to a union of types, or when the default factory signature isn't trivial (e.g. contains overloads). Here are some examples failing:(playground)
Note that (at least for
a
), this worked as expected with the old type inference.I first wanted to know if this can be considered a mypy limitation, or is the code really unsafe in some way? Or maybe is it because the typing spec does not provide any info regarding these use cases?
In comparison, pyright and pyre accepts the provided examples. We will currently recommend users to add a
type: ignore
comment and redirect to this issue. However, feel free to close if there's already an issue tracking this.Thanks in advance
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: