-
-
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
94 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import _GenericAlias, Union | ||
try: | ||
from types import UnionType | ||
except ImportError: | ||
class UnionType: | ||
__args__: list # list[type] | ||
def __init__(self, *args): | ||
self.__args__ = args | ||
|
||
def is_type(x) -> bool: | ||
return isinstance(x, type) or \ | ||
isinstance(x, _GenericAlias) or \ | ||
isinstance(x, 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__) | ||
else: | ||
return instanceof(obj, classinfo) |
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,21 @@ | ||
assert 1 in (Int or Str) | ||
assert 1.2 notin (Int or Str) | ||
|
||
dic = {:} | ||
assert dic in {:} | ||
assert dic in {Str: Int} | ||
assert dic in {Str: Str} | ||
dic2 = {"a": 1} | ||
assert dic2 in {Str or Int: Int} | ||
assert dic2 in {Str: Int or Str} | ||
assert dic2 notin {Int: Int} | ||
|
||
tup = () | ||
assert tup in () | ||
assert tup in (Int, Int) | ||
assert tup in (Int, Str) | ||
|
||
arr = [] | ||
assert arr in [] | ||
assert arr in [Int] | ||
assert arr in [Str] |
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