-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
279 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" | |
[project] | ||
name = "haiway" | ||
description = "Framework for dependency injection and state management within structured concurrency model." | ||
version = "0.9.1" | ||
version = "0.10.0" | ||
readme = "README.md" | ||
maintainers = [ | ||
{ name = "Kacper Kaliński", email = "[email protected]" }, | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from collections.abc import Callable | ||
from typing import Any, cast, final, overload | ||
|
||
from haiway.types.missing import MISSING, Missing, not_missing | ||
from haiway.utils.always import always | ||
|
||
__all__ = [ | ||
"Default", | ||
"DefaultValue", | ||
] | ||
|
||
|
||
@final | ||
class DefaultValue[Value]: | ||
@overload | ||
def __init__( | ||
self, | ||
value: Value, | ||
/, | ||
) -> None: ... | ||
|
||
@overload | ||
def __init__( | ||
self, | ||
/, | ||
*, | ||
factory: Callable[[], Value], | ||
) -> None: ... | ||
|
||
@overload | ||
def __init__( | ||
self, | ||
value: Value | Missing, | ||
/, | ||
*, | ||
factory: Callable[[], Value] | Missing, | ||
) -> None: ... | ||
|
||
def __init__( | ||
self, | ||
value: Value | Missing = MISSING, | ||
/, | ||
*, | ||
factory: Callable[[], Value] | Missing = MISSING, | ||
) -> None: | ||
assert ( # nosec: B101 | ||
value is MISSING or factory is MISSING | ||
), "Can't specify both default value and factory" | ||
|
||
self._value: Callable[[], Value | Missing] | ||
if not_missing(factory): | ||
object.__setattr__( | ||
self, | ||
"_value", | ||
factory, | ||
) | ||
|
||
else: | ||
object.__setattr__( | ||
self, | ||
"_value", | ||
always(value), | ||
) | ||
|
||
def __call__(self) -> Value | Missing: | ||
return self._value() | ||
|
||
def __setattr__( | ||
self, | ||
__name: str, | ||
__value: Any, | ||
) -> None: | ||
raise AttributeError("Missing can't be modified") | ||
|
||
def __delattr__( | ||
self, | ||
__name: str, | ||
) -> None: | ||
raise AttributeError("Missing can't be modified") | ||
|
||
|
||
@overload | ||
def Default[Value]( | ||
value: Value, | ||
/, | ||
) -> Value: ... | ||
|
||
|
||
@overload | ||
def Default[Value]( | ||
*, | ||
factory: Callable[[], Value], | ||
) -> Value: ... | ||
|
||
|
||
def Default[Value]( | ||
value: Value | Missing = MISSING, | ||
/, | ||
*, | ||
factory: Callable[[], Value] | Missing = MISSING, | ||
) -> Value: # it is actually a DefaultValue, but type checker has to be fooled most some cases | ||
return cast( | ||
Value, | ||
DefaultValue( | ||
value, | ||
factory=factory, | ||
), | ||
) |
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
Oops, something went wrong.