2 ms·
Just tried it. What's the point of type signatures if the compiler won't hold you to the promises you've made? This is in Python 3.6.4: >>> def f (x:int) ->
by Jeff_Brown 8y ago
Just tried it. What's the point of type signatures if the compiler won't hold you to the promises you've made? This is in Python 3.6.4:
>>> def f (x:int) -> int: return x+1
...
>>> def g (x:str) -> str: return f(x)
...
>>> g(1)
2
I expected a complaint after the second definition: I specified that g takes and returns a string, and then defined it to take and return an int. Not only did Python not catch the error at compile time (defining g), it didn't even catch it at runtime (calling g).
- netheril96 8y agoFor some strange reasons Python developers decide to provide the syntax of type annotations but leave the actual checking to third party implementations.
- detaro 8y agoType annotations are just that: annotations. They currently have no meaning to the CPython interpreter and are purely consumed by external tooling (IDEs, there's a static typechecker called mypy, ...) or code that chooses to use them (e.g. there's web frameworks that convert and validate parameter types according to the signature of the handler function)