-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
8 changed files
with
104 additions
and
39 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
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,21 +1,33 @@ | ||
from typing import _GenericAlias, Union | ||
try: | ||
from types import UnionType | ||
except ImportError: | ||
class UnionType: | ||
from typing import Union | ||
|
||
class UnionType: | ||
__origin__ = Union | ||
__args__: list # list[type] | ||
def __init__(self, *args): | ||
self.__args__ = args | ||
|
||
class FakeGenericAlias: | ||
__origin__: type | ||
__args__: list # list[type] | ||
def __init__(self, origin, *args): | ||
self.__origin__ = origin | ||
self.__args__ = args | ||
try: | ||
from types import GenericAlias | ||
except ImportError: | ||
GenericAlias = FakeGenericAlias | ||
|
||
def is_type(x) -> bool: | ||
return isinstance(x, type) or \ | ||
isinstance(x, _GenericAlias) or \ | ||
isinstance(x, UnionType) | ||
return isinstance(x, (type, GenericAlias, UnionType)) | ||
|
||
instanceof = isinstance | ||
# The behavior of `builtins.isinstance` depends on the Python version. | ||
def isinstance(obj, classinfo) -> bool: | ||
if instanceof(classinfo, _GenericAlias) and classinfo.__origin__ == Union: | ||
return any(instanceof(obj, t) for t in classinfo.__args__) | ||
def _isinstance(obj, classinfo) -> bool: | ||
if isinstance(classinfo, (GenericAlias, UnionType)): | ||
if classinfo.__origin__ == Union: | ||
return any(isinstance(obj, t) for t in classinfo.__args__) | ||
else: | ||
return isinstance(obj, classinfo.__origin__) | ||
elif is_type(classinfo): | ||
return isinstance(obj, classinfo) | ||
else: | ||
return instanceof(obj, classinfo) | ||
return False |
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