As Chris A says, I'd be inclined to see how far we can get with
(extended) type hints before going for new syntax, though.
> def foo() -> {1, 2, 3}:
> return 2
That is, of course, valid syntax right now. I wonder what a type
checker could do with it? Similarly,
Well, the thing is that there is no way to know at compile time what Value is getting passed in -- this is really more a way to catch a ValueError than TypeError, so can't be done with static type checking.
Unless you do, as ChrisA suggests, crate a Type (and Enum) that you can then check for.
But while I like the idea of Enums, particularly for, say multi-valued flags, They require an extra name and extra typingthat I find annoying (annoying enough that I haven't used one yet in my code. That is, I prefer, so use Chris A's example:
some_function(1, 2)
...
to:
from some_module import Spam
some_function(Spam(1), 2)
...
That being said, if you want your API to be "safe" and type-chackable, then Enum is the way to go.
As for the OP, who was asking for a run-time check, if:
def fun(a, b):
if a not in {1,2,3}:
raise ValueError("a has to be one of these values: 1, 2, 3")
is too verbose, you can always write a utility function (or a callable class or closure that stores the options) to make it a simple one-liner:
def fun(a, b):
value_check(a, options= {1, 2, 3})
-CHB
--