What does HackerNews think of mypy?

Optional static typing for Python

Language: Python

#45 in Python
#5 in TypeScript
Python now has an optional type system and if you add one of them such as mypy or pyre to your CI process and you can configure GitHub to refuse the pull request until types are added you can make it somewhat strongly typed.

If you have a preexisting codebase I believe the way you can convert it is to add the types that you know on commits and eventually you will have enough types that adding the missing ones should be easy. For the missing ones Any is a good choice.

https://pyre-check.org and https://github.com/python/mypy are popular.

The reason I didn't just create a pylint/flake8/mypy plugin is because they all use a very specific methodology, whereas I wanted the freedom to architect it the way I wanted. Also, I really needed good type information, which is why I used mypy[0] as the AST parser/type checker.

[0]: https://github.com/python/mypy

Python is statically typed though. [1][2][3][4]

These aren't just some third-party tools bolted on. The type annotation syntax is built into the language[5] and standard library[6][7].

I personally find static typing to be more trouble than it's worth most of the time. Industry typing metalanguages are not expressive enough to deal with even fairly basic real-world programs and force you to write bad code to work around the type checker's stupidity. And, of course, you still have to write tests. Maybe someday they'll catch up to Idris. Python's static typing is no better, but at least it allows you to turn it off when it's not worth it.

[1]: https://github.com/microsoft/pyright

[2]: https://github.com/python/mypy

[3]: https://github.com/google/pytype

[4]: https://github.com/facebook/pyre-check

[5]: https://peps.python.org/pep-3107/

[6]: https://peps.python.org/pep-0484/

[7]: https://docs.python.org/3/library/typing.html#module-typing

Have you look at mypyc?

https://github.com/python/mypy

Its a compiler that support subset of python and mypy's team including Guido work on it. It would be great if this kind of efforts have been done on more realistic project.

> The 2/3 mess (yes it is still a thing)

It's still a thing for the (loud) few who couldn't manage to move to 3 after 10 years. The rest just went on. The ecosystem is on 3 [0] and it has been for years now.

[0]: http://py3readiness.org/

> The insane packaging situation. Packaging a Python app is a nightmare.

The packaging story in Python is in the middle of a big transformation from legacy tools to modern npm/yarn/cargo-like tools. pipenv [1] and poetry [2] are the most popular contenders. It will take a while until the community settles on this, but both tools work already.

[1]: https://github.com/pypa/pipenv

[2]: https://github.com/sdispater/poetry

> Dynamically typed

Python 3.5 introduced type annotations. It is now possible to write statically typed code in Python via mypy [3], which from my experience works similar well as TypeScript works for JavaScript.

[3]: https://github.com/python/mypy

> I would honestly like an option to "compile" python to check for typing inconsistencies

You can use mypy for that: https://github.com/python/mypy

You can write Python like pseudo-code. Sometimes the results look almost like plain English.

For example, if I have to design software for a micro-controller that uses C/C++ I sometimes write down the algorithm in Python (with editor support: auto-formatting, etc.), then I implement it in C/C++. There is no better way to write pseudo-code that I know of.

Python is also great to create a prove of concept for something. It allows for rapid prototyping due to the simple syntax, the high-level features, the huge ecosystem and the dynamic type system. Most of the time the result is good enough for production. You can add type annotations and get statically typing via mypy [0] to harden the code base. This works really well. (See: JS->TypeScript).

Python has a well developed FFI which allows you to call C/C++/Rust, without large performance hits. I think this is one of the main reasons for Python's success. It can be used as a high level interface to a huge low level ecosystem. ML wouldn't exist in Python without that.

Python is a great language to create simple scripts as well. I use it as a bash replacement.

But beware: Python can get really ugly if you rely too heavily on OOP/Inheritance. There is a lot of ugly Python code out there. I think Python shines the most if you use it in a procedural/semi-functional style and describe data-types for your business logic (almost) entirely with (dump) dataclasses [1] and namedtuples [2].

Beware2: Python's packaging story (package manager, etc) is currently in a messy state. There are good solutions, but it's difficult to find the right tools for a newcomer. I think it will take a couple of years for the dust to clear, but you might want to check out pipenv [7] or poetry [8]

These packages will make your life a lot easier. I use them for every project:

- black [3] or yapf [4] (auto-formatter)

- mypy [0] (statically typing with editor support for VSCode)

- pylint [5] or flake8 [6] (linter)

[0] https://github.com/python/mypy

[1] https://docs.python.org/3/library/dataclasses.html#module-da...

[2] https://docs.python.org/3/library/typing.html#typing.NamedTu...

[3] https://github.com/psf/black

[4] https://github.com/google/yapf

[5] https://github.com/PyCQA/pylint/

[6] https://github.com/PyCQA/flake8

[7] https://github.com/pypa/pipenv

[8] https://github.com/sdispater/poetry

Python supports optional static typing annotations now.

There are four popular typecheckers that I'm aware of (mypy [1], pyre [2], pyright [3], pytype [4]), which is a little confusing, but any of the four will catch a lot of type issues that typical statically typed language compilers would catch. It's not as robust as true static typing - there are sometimes false positives and false negatives - but I find it helpful.

[1] https://github.com/python/mypy

[2] https://github.com/facebook/pyre-check

[3] https://github.com/microsoft/pyright

[4] https://github.com/google/pytype

> This is exactly how I felt moving from a Python codebase to Go. But you realize on the 100th or so Python stacktrace that it's worth the investment up front, if not for type safety alone.

Luckily, you don't have to choose between those two outcomes:

https://github.com/python/mypy

Two words: typing, mypy [1]

The editor support that I get from typing/mypy is fantastic! It feels like writing in a statically typed language. To me this is by far the most important feature of Python3. It's a godsend for larger/complicated code bases.

[1]: https://github.com/python/mypy