Repository navigation
Use enum trick for dataclasses.MISSING - #7127
Merged
Merged
Conversation
sirosen
force-pushed
the
dataclass-missing-use-enum
branch
from
February 4, 2022 06:42
1ac0d00 to
c82309a
Compare
This comment has been minimized.
This comment has been minimized.
sirosen
force-pushed
the
dataclass-missing-use-enum
branch
from
February 4, 2022 06:44
c82309a to
397786b
Compare
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
hauntsaninja
approved these changes
Feb 4, 2022
hauntsaninja
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the fix, looks great to me! I'll leave open for a little bit in case anyone else chimes in.
sobolevn
reviewed
Feb 4, 2022
The goal of this change is to fix the behavior of
`dataclasses.Field`. Several attributes of a `Field` may have a value
of `MISSING` (a sentinel value). As a result, attributes of Field may
be checked against `MISSING` as in
[f for f in fields(obj) if f.default is MISSING]
`default`, `default_factory`, and `kw_only` are the attributes
which may have a value of `MISSING`.
This workaround of defining `_MISSING_TYPE` as an enum, and `MISSING`
as its only member, allows `... | Literal[_MISSING_TYPE.MISSING]` to
be used in type annotations for these attributes. This is very
slightly inaccurate -- primarily in that `_MISSING_TYPE` isn't really
an enum -- but this allows for use in `Literal`.
After PEP 661 (Sentinel Values), there may be a more accurate way of
writing this, but for now this works.
This adjustment is only applied to the attributes of Field, not the
arguments to functions and methods.
sirosen
force-pushed
the
dataclass-missing-use-enum
branch
from
February 4, 2022 17:00
397786b to
550ea0c
Compare
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The goal of this change is to fix the behavior of
dataclasses.Field. Several attributes of aFieldmay have a value ofMISSING(a sentinel value). As a result, attributes of Field may be checked againstMISSINGas indefault,default_factory, andkw_onlyare the attributes which may have a value ofMISSING.This workaround of defining
_MISSING_TYPEas an enum, andMISSINGas its only member, allows... | Literal[MISSING]to be used in type annotations for these attributes. This is very slightly inaccurate -- primarily in that_MISSING_TYPEisn't really an enum -- but this allows for use inLiteral. After PEP 661 (Sentinel Values), there may be a more accurate way of writing this, but for now this works.This adjustment is only applied to the attributes of Field, not the arguments to functions and methods.
There have been two previous attempts to handle this, #5900 and #7126.
Per those PRs, PEP 661 (Sentinel Values) will provide the best solution for this in the future. The enum+Literal workaround used here was suggested as a way to handle
dataclasses.MISSINGwhile we wait for that PEP.