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
{{ message }}
Repository navigation
Commit 714cd37
Browse filesBrowse the repository at this point in the historyBrowse files
Treat converter=None on a dataclass field as no converter
Summary:
attrs treats `converter=None` exactly like leaving the converter out, and its stubs declare `converter: None` as the no-converter form. Pyrefly treated it as a real converter: the synthesized `__init__` parameter became `Unknown`, so `A("asd")` was accepted for an `int` field. Pyright, basedpyright and ty treat it as no converter.
Following Rebecca's direction, `None` is handled where the field's converter is stored: a `converter=` argument whose type is `None` is never stored, so everything that reads the stored converter treats the field as having none. That fixes the `__init__` parameter and also turns on the checks that only run without a converter: the `x.default` return type against the field type, an assignment to the attribute, and the `x.converter` decorator now applies. This covers every `dataclass_transform` field specifier, not only attrs, and because it checks the argument's type, a variable set to `None` counts too.
The attrs check of `default=` and `factory=` against the field type gets the same rule separately. It runs for every call to an attrs field specifier, including in classes pyrefly doesn't treat as attrs classes, where nothing is stored, so it reads the call's own `converter=` argument. A test from the previous diff fails if it reads the stored converter instead.
A converter whose type is a union with `None` still makes the `__init__` parameter partly `Unknown`; its `bug` test is unchanged.
Not mypy_primer: on 17 vendored libraries (the 8 standard ones plus 9 that use attrs), the base and patched builds reported the same diagnostics. None of them pass `converter=None`, so this only shows that real converters are unaffected.
Fixes#5283
Reviewed By: rchen152
Differential Revision: D130289218
fbshipit-source-id: a25dca184a213c156172b34f9d3425f150232650
0 commit comments