[dataclasses] Fix MISSING and KW_ONLY for Python 3.15+#16035
Conversation
This comment has been minimized.
This comment has been minimized.
629120c to
5a3662e
Compare
This comment has been minimized.
This comment has been minimized.
5a3662e to
54b0beb
Compare
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
| class _MISSING_TYPE(enum.Enum): | ||
| MISSING = enum.auto() | ||
| if sys.version_info >= (3, 15): | ||
| MISSING: Final[sentinel] |
There was a problem hiding this comment.
hm this might regress functionality, can't we already do MISSING = sentinel("MISSING")?
There was a problem hiding this comment.
Thanks, @JelleZijlstra, that makes perfect sense. This was my first contribution, inspired by the EuroPython sprints, where the dataclasses stubs were listed as candidates for a fix.
It looks however like the correct fix is currently blocked by a tooling constraint: typeshed pins mypy to 2.3.0, which does not yet support sentinel values as types. That work is pending in python/mypy#21647.
Should we close this PR for now and revisit it once the mypy PR is merged and typeshed updates its pin, or would you prefer to keep it open as a draft until then?
| default_factory: _DefaultFactory[_T] | Literal[_MISSING_TYPE.MISSING] | ||
|
|
||
| if sys.version_info >= (3, 15): | ||
| default: _T | sentinel |
There was a problem hiding this comment.
This is worse than before, we should hint the specific sentinel not the sentinel type.
In Python 3.15,
dataclasses.MISSINGanddataclasses.KW_ONLYwere changed from custom class instances (_MISSING_TYPE,_KW_ONLY_TYPE) tobuiltins.sentinelobjects (see: python/cpython#149086) and_MISSING_TYPEno longer exists at runtime.This PR:
MISSINGandKW_ONLYbehindsys.version_info >= (3, 15)usingbuiltins.sentinelFieldattribute types (default,default_factory,kw_only) for>= 3.15field()overloads for>= 3.15usingsentinelinstead ofLiteral[_MISSING_TYPE.MISSING]py315.txt