Use enum trick for dataclasses.MISSING #7127
Merged
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 aField
may have a value ofMISSING
(a sentinel value). As a result, attributes of Field may be checked againstMISSING
as indefault
,default_factory
, andkw_only
are the attributes which may have a value ofMISSING
.This workaround of defining
_MISSING_TYPE
as an enum, andMISSING
as its only member, allows... | Literal[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 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.MISSING
while we wait for that PEP.