0

I am using a simplified example, to show the problem. I have a function, could also be a method, which should accept only int or bool
Any other type should be removed. I know how to input one type, I don't know how to input both. I have tried Sequence from the typing, but failed.See code below:

int_bool = TypeVar( "int_bool", int, bool)
def foo( a : Sequence[ int_bool ] ):
    print( a )
foo( 1.1 )
>>> 1.1

It should error out, as 1.1 is neither int nor bool

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
user1134991
  • 3,003
  • 2
  • 25
  • 35
  • 1
    Does this answer your question? [What are type hints in Python 3.5?](https://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5) – MisterMiyagi Feb 15 '22 at 11:40

1 Answers1

0

The answer is that I have made a mistake. Unlike some other languages (Cadence SKILL++ comes to mind), this functionality is for documentation, and hinting, and has no enforcement mechanism. i.e., it's doing what it is supposed to do, not what I wanted it to do, or thought that it was supposed to do.

user1134991
  • 3,003
  • 2
  • 25
  • 35
  • 1
    The closest you can get is to use a static checker like mypy. You can run it as a pre-commit or as a test or before running your app. Of course, it's a _static_ checker, so if the value is only defined during runtime, then it still won't catch the error. – Gino Mempin Feb 15 '22 at 12:00
  • The idea is a dynamic one, as in Cadence SKILL++ – user1134991 Feb 15 '22 at 13:03