Skip to content
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

Introduce NothingType #1358

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/attr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

from functools import partial
from typing import Callable, Protocol
from typing import Callable, Literal, Protocol

from . import converters, exceptions, filters, setters, validators
from ._cmp import cmp_using
Expand All @@ -16,6 +16,7 @@
Attribute,
Converter,
Factory,
_Nothing,
attrib,
attrs,
fields,
Expand All @@ -36,12 +37,15 @@ class AttrsInstance(Protocol):
pass


NothingType = Literal[_Nothing.NOTHING]

__all__ = [
"Attribute",
"AttrsInstance",
"Converter",
"Factory",
"NOTHING",
"NothingType",
"asdict",
"assoc",
"astuple",
Expand Down
7 changes: 4 additions & 3 deletions src/attr/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from typing import (
Any,
Callable,
Generic,
Literal,
Mapping,
Protocol,
Sequence,
Expand Down Expand Up @@ -37,9 +38,9 @@ from attrs import (
)

if sys.version_info >= (3, 10):
from typing import TypeGuard
from typing import TypeGuard, TypeAlias
else:
from typing_extensions import TypeGuard
from typing_extensions import TypeGuard, TypeAlias

if sys.version_info >= (3, 11):
from typing import dataclass_transform
Expand Down Expand Up @@ -72,11 +73,11 @@ class _Nothing(enum.Enum):
NOTHING = enum.auto()

NOTHING = _Nothing.NOTHING
NothingType: TypeAlias = Literal[_Nothing.NOTHING]

# NOTE: Factory lies about its return type to make this possible:
# `x: List[int] # = Factory(list)`
# Work around mypy issue #4554 in the common case by using an overload.
from typing import Literal

@overload
def Factory(factory: Callable[[], _T]) -> _T: ...
Expand Down
2 changes: 2 additions & 0 deletions src/attrs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
AttrsInstance,
Converter,
Factory,
NothingType,
_make_getattr,
assoc,
cmp_using,
Expand Down Expand Up @@ -58,6 +59,7 @@
"make_class",
"mutable",
"NOTHING",
"NothingType",
"resolve_types",
"setters",
"validate",
Expand Down