0

Scalar type checking does not work in Python (v 3.8.6).

def test(x: int) -> int:
    print(type(x))
    return x

a = test('a')

It doesn't matter at all that there is no int as an input parameter. It also does not indicate that int is not an output parameter.

Output:

<class 'str'>

I would expect ValueError!

Jarda
  • 89
  • 5
  • See https://stackoverflow.com/questions/11328920/is-python-strongly-typed – azro Jun 13 '21 at 08:08
  • Python is dynamiclly typed, typing is only HINT not rules – azro Jun 13 '21 at 08:09
  • See https://stackoverflow.com/questions/41356784/how-to-use-type-hints-in-python-3-6 – azro Jun 13 '21 at 08:09
  • That is not how Python type hinting works. Doing `x: int` does not *require* `x` to be an `int`. What does is provide a hint to an IDE that can warn you if the way you are calling the function disagrees with the type hint. If you only feel comfortable programming in a strongly-typed language then maybe Python is not for you. – BoarGules Jun 13 '21 at 08:11

1 Answers1

0

What you are talking about is Function Annotations.

def test(x: int) -> int.

This means that the function is expected to take and int argument and is also expected to have an int return value. These are optional and are not enforced by Python but they are useful to static type analysis tools, and aid IDEs with code completion and refactoring.

Read more here: https://docs.python.org/3/glossary.html#term-function-annotation

Ram
  • 4,724
  • 2
  • 14
  • 22