Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using vtypes with a run-time type checker #2

Closed
piccolbo opened this issue Jun 10, 2022 · 2 comments
Closed

using vtypes with a run-time type checker #2

piccolbo opened this issue Jun 10, 2022 · 2 comments

Comments

@piccolbo
Copy link

piccolbo commented Jun 10, 2022

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!

@smarie
Copy link
Owner

smarie commented Jun 13, 2022

Good catch @piccolbo ! Indeed this is a known issue in pytypes, that I reported some time ago : Stewori/pytypes#86

@piccolbo
Copy link
Author

OK will follow there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants