24

I was recently checking about type hinting and after reading some theory I tried a simple example:

def myfun(num1: int, num2: int) -> int:
    return str(num1) + num2


a = myfun(1, 'abc')
print(a)
# output -> 1abc

Here you can see that I have defined num1 and num2 of type int and even after passing num2 value as string it is not generating any error.

Also the function should expect to return int type value but it's not complaining on returning string type value.

Can someone please explain what's going wrong here?

3
  • 5
    Python doesn't check any of the type annotations. If you just run the file normally, the hints are just for readers (and you can also access type hints within code, e.g. with typing.get_type_hints). To really use the type hints, use a tool like MyPy which is a static analyzer. Commented May 6, 2021 at 5:22
  • This question is similar to: What are type hints in Python 3.5?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 26, 2024 at 19:53
  • This question is similar to: Python 3 type check not works with use typing module?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jan 20 at 21:19

1 Answer 1

37

It's called type hinting for a reason: you're giving a hint about what a variable should be to the IDE or to anyone else reading your code. At runtime, the type hints don't actually mean anything - they exist for documentation and usability by other developers. If you go against the type hints, python won't stop you (it assumes you know what you're doing), but it's poor practice.

Note that this differs from statically-typed languages like Java, where trying to pass an argument that's incompatible with the function's definition will produce a compile-time error, and if you pass a differently-typed but compatible argument (e.g. passing a float to a function that expects an int) it will be automatically typecast.

Note that the code you've given will encounter a TypeError if the programmer uses it like they're supposed to, because int cannot be concatenated to a str. Your IDE or linter should be able to see this and give you a warning on that line, which it does based on the type hints. That's what the type hints are for - informing the behavior of the IDE and documentation, and providing a red flag that you might not be using a function in the intended way - not anything at runtime.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there any module that I can install or any option I can set somewhere that would cause Python to raise errors at runtime if the type hints are violated?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.