Skip to content

Commit

Permalink
Introduce NothingType (#1358)
Browse files Browse the repository at this point in the history
* Introduce NothingType

* Fallback to typing_extensions

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Avoid TypeAlias at runtime

* Add changelog

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Docs

* Add import, test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix test

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Tinche and pre-commit-ci[bot] authored Dec 10, 2024
1 parent 1e07f46 commit d18763c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
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\]

0 comments on commit d18763c

Please sign in to comment.