1

I don't quite understand why this is not failing:

def hello(name: str) -> int:
    ending:int = '!!!'
    return f'Hello {name} {ending}'

print(hello('John')) # Hello John !!!

And if there is already possibility to strong type python?

Meroz
  • 697
  • 1
  • 6
  • 22

2 Answers2

2

The reason is explained in PEP 484 by Guido himself:

It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.

So the answer is NO. Type hints are only hints. They help to indicate what type of data a variable or function should/may contain/returns/etc. It wasn't designed to transform Python into a statically typed language.

Antwane
  • 15,262
  • 5
  • 37
  • 71
  • Strong typing is not the same as static typing, though. Python does raise runtime type errors unlike weakly typed languages such as javascript. https://stackoverflow.com/questions/11328920/is-python-strongly-typed?rq=1 – Håken Lid Nov 05 '18 at 09:51
  • I there possiblity though that could raise those error (like parameter)? `python3.7 --strong`? – Meroz Nov 05 '18 at 10:00
  • @HåkenLid I just fixed my answer. Thanks ! – Antwane Nov 05 '18 at 10:01
  • 1
    @Meroz: No, there's no runtime enforcement of type hints. You have to use a type checker such as [mypy](http://mypy-lang.org/). It would probably be possible to create a runtime type checker which uses type hints, but that would have a performance cost. The PyCharm IDE uses the type hints for code linting, and you can also configure other editors to use mypy for the same. – Håken Lid Nov 05 '18 at 10:07
  • Performance cost only on running on development mode, on production would be without `--check-types` parameter. I use VS Code, do you know any extentions for it? – Meroz Nov 05 '18 at 10:36
  • I can `pip3 install mypy` and `mypy file.py`, and it will show me where I have errors with types. – Meroz Nov 05 '18 at 12:59
0

As I wrote in comment it is nice to use mypy myproject.py to run it before project/code run. Then you could verify data types structure and correct flow.

Meroz
  • 697
  • 1
  • 6
  • 22