Skip to content

Conversation

sirosen
Copy link
Contributor

@sirosen sirosen commented Feb 4, 2022

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.


I believe this is the correct resolution for python/mypy#12124

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.
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2022

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]

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Feb 4, 2022

Thanks, but we previously rejected this change in #5900 .
As discussed over there, the main issue is that type checkers will not recognise if x is dataclasses.MISSING as narrowing the type

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Feb 4, 2022

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 :-)

@sirosen
Copy link
Contributor Author

sirosen commented Feb 4, 2022

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.

@sirosen sirosen deleted the fix-dataclass-field-classattrs branch February 4, 2022 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants