2

If I have a following function:

def rand(int: int) -> int:
    ...

rand('2')

It is working because when I call a rand function the editor showing the error. But it is still compiling the code. And I can write something like:

def rand(num: int) -> int:
    if not isinstance(num, int):
        raise ValueError(f"{num} is not a integer")
    return 0

I want to raise a error without doing some logic in the function. Is it possible to raise a error when calling function and without checking it?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
nonsensei
  • 99
  • 5
  • 1
    From the [linked question](https://stackoverflow.com/a/44784350/68587): "If static typing were to be completely enforced in Python, then it won't be Python anymore. It's a duck-typed dynamic language, and would loose all dynamism as a result. If you really intend to use a statically-typed language, you are better off not using Python." – John Kugelman Mar 13 '20 at 13:07
  • What do you mean better not using python? So why did they create static type declaration? – nonsensei Mar 13 '20 at 13:09
  • I'm just asking, Is the other possible way of without checking in the function – nonsensei Mar 13 '20 at 13:10
  • 2
    Static type checking is better than no type checking and helps catch certain classes of errors easier and earlier than waiting for them to blow up at runtime. Enforcing typing at runtime is an entirely different ballgame and requires different programming strategies; just as an ad hoc example, passing mock objects for tests becomes more complicated if that mock object is checked for its actual type hierarchy instead of merely needing to conform to certain *behavioural* expectations. – deceze Mar 13 '20 at 13:36

0 Answers0