-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow sentinel values to be used as types rather than instances.
- Loading branch information
1 parent
8230d6e
commit 3d7bd48
Showing
4 changed files
with
43 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Dict | ||
|
||
from typing_extensions import Self | ||
class _MissingType(type): | ||
""" | ||
This metaclass is used to create singleton falsey classes for use as missing | ||
and/or sentinel placeholder values. | ||
""" | ||
|
||
def __repr__(cls): | ||
return cls.__name__ | ||
|
||
class _MissingType: | ||
__instance__ = None | ||
|
||
def __new__(cls) -> _MissingType: | ||
if cls.__instance__ is None: | ||
cls.__instance__ = super(_MissingType, cls).__new__(cls) | ||
return cls.__instance__ | ||
|
||
def __bool__(self) -> bool: | ||
def __bool__(cls): | ||
return False | ||
|
||
def __repr__(self) -> str: | ||
return "MISSING" | ||
|
||
def __copy__(self) -> Self: | ||
return self | ||
|
||
def __deepcopy__(self, memo: Dict) -> Self: | ||
return self | ||
def __call__(cls): | ||
return cls | ||
|
||
|
||
MISSING = _MissingType() | ||
class MISSING(metaclass=_MissingType): | ||
""" | ||
Used to represent attributes that have not yet been assigned a value. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters