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

Merged
merged 10 commits into from
Dec 10, 2024
Merged
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
1 change: 1 addition & 0 deletions changelog.d/1358.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduce `attrs.NothingType`, for annotating types consistent with `attrs.NOTHING`.
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__ = [
"NOTHING",
"Attribute",
"AttrsInstance",
"Converter",
"Factory",
"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/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def __bool__(self):
NOTHING = _Nothing.NOTHING
"""
Sentinel to indicate the lack of a value when `None` is ambiguous.

When using in 3rd party code, use `attrs.NothingType` for type annotations.
"""


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 All @@ -32,6 +33,7 @@
"AttrsInstance",
"Converter",
"Factory",
"NothingType",
"__author__",
"__copyright__",
"__description__",
Expand Down
1 change: 1 addition & 0 deletions src/attrs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ from attr import setters as setters
from attr import validate as validate
from attr import validators as validators
from attr import attrib, asdict as asdict, astuple as astuple
from attr import NothingType as NothingType

if sys.version_info >= (3, 11):
from typing import dataclass_transform
Expand Down
13 changes: 13 additions & 0 deletions tests/test_mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1472,3 +1472,16 @@
reveal_type(A) # N: Revealed type is "def () -> main.A"
if has(A):
reveal_type(A) # N: Revealed type is "type[attr.AttrsInstance]"

- case: testNothingType
regex: true
main: |
from typing import Optional
from attrs import NOTHING, NothingType

def takes_nothing(arg: Optional[NothingType]) -> None:
return None

takes_nothing(NOTHING)
takes_nothing(None)
takes_nothing(1) # E: Argument 1 to "takes_nothing" has incompatible type "Literal\[1\]"; expected "(Optional\[Literal\[_Nothing.NOTHING\]\]|Literal\[_Nothing.NOTHING\] \| None)" \[arg-type\]
Loading