-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix forwardref by making ProvideMultiple generic
- Loading branch information
Showing
5 changed files
with
39 additions
and
28 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,20 +1,30 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Sequence | ||
from typing import TYPE_CHECKING, Any | ||
import sys | ||
import threading | ||
from typing import TYPE_CHECKING, Generic, TypeVar | ||
|
||
__all__ = ["AnyOf", "ProvideMultiple"] | ||
|
||
if TYPE_CHECKING: | ||
from typing import Union as AnyOf | ||
|
||
if sys.version_info >= (3, 11): | ||
from typing import TypeVarTuple, Unpack | ||
|
||
Variants = TypeVarTuple("Variants") | ||
class ProvideMultiple(Generic[Unpack[Variants]]): | ||
pass | ||
else: | ||
class AnyOf: | ||
def __class_getitem__(cls, item: Any) -> Any: | ||
if isinstance(item, tuple): | ||
return ProvideMultiple(item) | ||
return item | ||
Variants = TypeVar("Variants") | ||
provides_lock = threading.Lock() | ||
|
||
class ProvideMultiple(Generic[Variants]): | ||
def __class_getitem__(cls, item): | ||
with provides_lock: | ||
cls.__parameters__ = [Variants]*len(item) | ||
return super().__class_getitem__(item) | ||
|
||
class ProvideMultiple: | ||
def __init__(self, items: Sequence[Any]) -> None: | ||
self.items = items | ||
|
||
if TYPE_CHECKING: | ||
from typing import Union as AnyOf | ||
else: | ||
AnyOf = ProvideMultiple |
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