Repository navigation
Conversation
default, default_factory, and kw_only are all attributes of Field
objects which may have a value of MISSING. This is likely to arise
when examining Field objects and checking attributes.
For example,
[f for f in fields(obj) if f.default is MISSING]
The types for the attributes should therefore be `... | _MISSING_TYPE`
This adjustment is only applied to the attributes of Field, not the
arguments to functions and methods.
|
Diff from mypy_primer, showing the effect of this PR on open source code: pydantic (https://github.com/samuelcolvin/pydantic)
+ pydantic/dataclasses.py:173: error: Incompatible types in assignment (expression has type "Union[_DefaultFactory[Any], _MISSING_TYPE]", variable has type "Optional[Callable[[], Any]]") [assignment]
|
|
Thanks, but we previously rejected this change in #5900 . |
|
Actually, given that this has come up a couple times, I would accept a PR that uses the enum trick as described in: #5900 (comment) , if you'd be willing to make one :-) |
Sorry for not seeing that there had been an earlier PR! Yes, I'll read a bit more and then try that workaround in a PR. The enum trick seems good to me. It will make type narrowing work, and is valid for Literal. |
default, default_factory, and kw_only are all attributes of Field objects which may have a value of MISSING. This is likely to arise when examining Field objects and checking attributes.
For example,
The types for the attributes should therefore be
... | _MISSING_TYPEThis adjustment is only applied to the attributes of Field, not the arguments to functions and methods.
I believe this is the correct resolution for python/mypy#12124