Since static typing is available in Python 3.6, is it possible to force static typing for a Python project or set of Python files?
Asked
Active
Viewed 2.0k times
25
-
4Static typing is **not** available in python yet. Type hints or type annotations are for static analyzers like mypy which is not the same as static typing. – phd Jun 27 '17 at 15:21
-
1@Drako The TypeError is from normal Python operation. i.e., if you removed the hints, it would still raise an error. (`'this is ' +
` raises it). `def f(s: str) -> str: return 0` and then calling `f(0)` does not raise a TypeError for me (Python 3.6.1) – Artyer Jun 27 '17 at 15:32 -
Relevant if not dupes: http://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5/32558710#32558710 and http://stackoverflow.com/questions/39971929/what-are-variable-annotations-in-python-3-6?noredirect=1&lq=1? – Dimitris Fasarakis Hilliard Jun 27 '17 at 18:15
-
1@Drako: there is no type-checking at runtime as per PEP484: https://www.python.org/dev/peps/pep-0484/ – Ando Jurai May 23 '18 at 13:16
-
Have you considered using type_enforced to force your static typing hints? See: https://github.com/connor-makowski/type_enforced – conmak Oct 24 '22 at 13:36
2 Answers
15
You can use annotations
in Python3, which might help you get some benefits of static typing.
However 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.

hspandher
- 15,934
- 2
- 32
- 45
-
4I like its dynamicness, but Python's been evolving as well. When a project grows beyond a certain threshold, type checking begins to matter a lot, exponentially. I think there's a good chance the language eventually would allow optional type enforcement. – j4hangir May 15 '20 at 19:20
-
@j4hangir that's where well written tests come into picture and you can get the best of both worlds. Static typing or not, automated tests are indispensable anyway. – hspandher May 15 '20 at 19:23
-
2I'd opt for optional enforcement, similar to mypy. Personally I find myself type hinting nearly 80% of all my code, as the payoff is always great using a good IDE. – j4hangir May 17 '20 at 21:28
-
@j4hangir but then you will start expecting others to do the same as well. That's where it starts going south. – hspandher May 18 '20 at 11:48
-
Yes you're right, I'm certainly against making it mandatory too but it would be pretty nice to have builtin tools to say, check the type of parameters `when` they're optionally annotated. We don't need `int` for the `i`, but passing wrong parameters, wrongly ordered, etc. has been a src of headache for me. – j4hangir May 18 '20 at 19:47
-
1I'm not denying the benefits of static typing. I'm saying it has both pros and cons. And there are tools available like kite and anaconda that will provide somewhat of the IDE functionality you want. It won't be as effective of course – hspandher May 18 '20 at 19:54
14
I think you cannot force static typing but you can use a checker as mypy
.
According to line 2 of The Zen of Python by Tim Peters, you have "Explicit is better than implicit." Static typing is a good thing, but "Simple is better than complex." ...
$ python3.6
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

John Kugelman
- 349,597
- 67
- 533
- 578

glegoux
- 3,505
- 15
- 32
-
Static is (usually) better than Dynamic typing! It is not about efficiency, it is about readability. E.g. is "User" the name, the number, the system structure, the database row, or something else? The big idea Java got from Lisp and gave to C# is that there is no need for a dichotomy between scripting languages and efficient languages. It is possible to have both. C# in particular can produce efficient code that is also easy to work with. – Tuntable Aug 27 '23 at 08:28