Skip to content

Use enum trick for dataclasses.MISSING #7127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions stdlib/dataclasses.pyi
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import enum
import sys
import types
from builtins import type as Type # alias to avoid name clashes with fields named "type"
from typing import Any, Callable, Generic, Iterable, Mapping, Protocol, TypeVar, overload
from typing_extensions import Literal

if sys.version_info >= (3, 9):
from types import GenericAlias

_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)

class _MISSING_TYPE: ...
# define _MISSING_TYPE as an enum within the type stubs,
# even though that is not really its type at runtime
# this allows us to use Literal[_MISSING_TYPE.MISSING]
# for background, see:
# https://github.com/python/typeshed/pull/5900#issuecomment-895513797
class _MISSING_TYPE(enum.Enum):
MISSING = enum.auto()

MISSING: _MISSING_TYPE
MISSING = _MISSING_TYPE.MISSING

if sys.version_info >= (3, 10):
class KW_ONLY: ...
Expand Down Expand Up @@ -72,15 +80,15 @@ class _DefaultFactory(Protocol[_T_co]):
class Field(Generic[_T]):
name: str
type: Type[_T]
default: _T
default_factory: _DefaultFactory[_T]
default: _T | Literal[_MISSING_TYPE.MISSING]
default_factory: _DefaultFactory[_T] | Literal[_MISSING_TYPE.MISSING]
repr: bool
hash: bool | None
init: bool
compare: bool
metadata: types.MappingProxyType[Any, Any]
if sys.version_info >= (3, 10):
kw_only: bool
kw_only: bool | Literal[_MISSING_TYPE.MISSING]
def __init__(
self,
default: _T,
Expand Down