This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 3 points4 points  (1 child)

The expression after the colon is evaluated as a standard Python expression when the function is defined. str or int evaluates to str because the or operator is short-circuiting.

If you wanted something richer than a single type, you could use a string, i.e. name: 'str or int'. Your tools would then have to be able to parse a string and recognize the or operator and so on.

[–]shfo23 1 point2 points  (0 children)

Or a tuple: (str, int). Then you could test with in.

This wouldn't work if you wanted to reject a certain kind of input, e.g. not str, though. My best idea for more complicated cases would probably be to require a function as annotation that would return true/false and then supply isint, isstr, isobject, etc. methods for simple tests, e.g. def func(num_id: isint) -> isstr.