You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was wondering if it would be possible to use vtypes as type hints and have them checked at run-time. I tried in combination with pytypes and it doesn't work, meaning that one can't pass an int > 0 as a PositiveInt. It will still fail.
from vtypes import vtype
from pytypes import typechecked
PositiveInt = vtype('PositiveInt', int, {'should be positive': lambda x: x >= 0})
assert isinstance(2, PositiveInt)
@typechecked
def f(x: PositiveInt):
return x
print(f(2))
output:
Traceback (most recent call last):
File "/Users/antonio/Projects/own/ascetiprogrammer/snippets/sketch.py", line 11, in <module>
print(f(2))
pytypes.exceptions.InputTypeError:
__main__.f
called with incompatible types:
Expected: Tuple[PositiveInt]
Received: Tuple[int]
Hence the assertion passes, but pytpes doesn't accept 2 as a PositiveInt. I looked at the code and it is using an internal _isinstance rather than the built in isistance.
The goal would be to be able to write something like
@stricttypes # fantasy decorator
def sqrt(x: PositiveInt): #or PositiveNumber
....
Thanks!
The text was updated successfully, but these errors were encountered:
I was wondering if it would be possible to use vtypes as type hints and have them checked at run-time. I tried in combination with pytypes and it doesn't work, meaning that one can't pass an int > 0 as a PositiveInt. It will still fail.
output:
Hence the assertion passes, but pytpes doesn't accept 2 as a PositiveInt. I looked at the code and it is using an internal _isinstance rather than the built in isistance.
The goal would be to be able to write something like
Thanks!
The text was updated successfully, but these errors were encountered: