Skip to content

Fix dataclasses.Field to use _MISSING_TYPE - #7126

Closed
sirosen wants to merge 1 commit into
python:masterfrom
sirosen:fix-dataclass-field-classattrs
Closed

sirosen wants to merge 1 commit into
python:masterfrom
sirosen:fix-dataclass-field-classattrs

Conversation

@sirosen

@sirosen sirosen commented Feb 4, 2022

Copy link
Copy Markdown
Contributor

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

github-actions Bot commented Feb 4, 2022

Copy link
Copy Markdown
Contributor

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

hauntsaninja commented Feb 4, 2022 •

Copy link
Copy Markdown
Collaborator

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

hauntsaninja commented Feb 4, 2022 •

Copy link
Copy Markdown
Collaborator

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

sirosen commented Feb 4, 2022

Copy link
Copy Markdown
Contributor Author

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