Optional rule to check Pydantic constructor arguments against declared field types
Summary
ty models Pydantic constructors with lax coercion, so both of these pass:
import pydantic
class Model(pydantic.BaseModel):
count: int
Model(count='123') # ty: ok — coerces to 123 at runtime
Model(count='abc') # ty: ok — but raises ValidationError at runtimeThe first is correct by the book. The second is the problem: ty accepts a call that always fails at runtime, because str is a valid lax input for int in general but this particular str is not.
pydantic.mypy with init_typed = True rejects both:
error: Argument "count" to "Model" has incompatible type "str"; expected "int" [arg-type]Teams that set init_typed = True have deliberately opted out of relying on coercion — they want declared field types enforced at construction. There is currently no way to express that in ty, which blocks migrating any codebase using this setting.
Request: an opt-in rule, off by default, that checks constructor arguments against the declared field types rather than the lax input types.
This is the same shape as #3966, where ty correctly honored extra=ignore but teams wanted the stricter check regardless, and the resolution was a rule they could turn off. The default would be reversed here: off by default, enabled by teams coming from init_typed = True.
Version
ty 0.0.81
Source: astral-sh/ty