-
-
Save llllllllll/271f407090070e012e03 to your computer and use it in GitHub Desktop.
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
""" | |
Example | |
------- | |
In [1]: from b import _t | |
In [2]: _t[1 ::] | |
File "<string>", line unknown | |
SyntaxError: type assertions must use 'a :: b' syntax | |
In [3]: _t[1 :: int] | |
Out[3]: 1 | |
In [4]: _t[1 :: float] | |
--------------------------------------------------------------------------- | |
AssertionError Traceback (most recent call last) | |
<ipython-input-4-0cd05df31e34> in <module>() | |
----> 1 _t[1 :: float] | |
/home/yui/projects/tmp/b.py in __getitem__(key) | |
14 assert isinstance(val, tp), '%r is not an instance of %r' % ( | |
15 val, | |
---> 16 tp.__name__, | |
17 ) | |
18 return val | |
AssertionError: 1 is not an instance of 'float' | |
""" | |
@object.__new__ | |
class _t: | |
@staticmethod | |
def __getitem__(key): | |
if (not isinstance(key, slice) or | |
key.stop is not None or | |
key.step is None): | |
raise SyntaxError("type assertions must use 'a :: b' syntax") | |
val, tp = key.start, key.step | |
assert isinstance(val, tp), '%r is not an instance of %r' % ( | |
val, | |
tp.__name__, | |
) | |
return val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment