Is it possible to add a new compile option for static type checking or provide an official type checker?

Advice:

  1. Add a new compile option for static type checking.
  2. Provide an official type checking tool,or use some off-the-shelf type checking tools as recommended tools.

I’m doing some research on Python type annotations recently.
I found that, some dynamic typing related bugs can be effectively avoided in the early stages of code development
by adding type annotations to Python code and applying some type checking tools (such as MyPy, PyType, Pyright, and Pyre, etc.).

Is it possible to add an optional compile option to Python?
In this optional compilation mode, the compiler will strictly check the type of each target.
Once the type check fails, the program will not be able to run.
Such a strict compilation mode may be beneficial for some safety-critical areas to write safer code.

Moreover, different tools discover, classify and report defects in different ways,
and there is little overlap in the defects they reported, which may confuse users.
Is it possible to have a type checker officially released by Python?

It is certainly possible; most things are possible. Whether it’s something that the core developers wish to do or not is a different question. Right now the type-checking ecosystem has multiple tools as you noted, and that allows users to choose the one that provides the features they want.

Also, Python doesn’t really have a ‘compile’ operation (in spite of the .pyc files). I doubt many users would want to run the type-checking process every time they ran their application, or have to install all of the type-checking dependencies in order to run their application, and I don’t know how the mode you have proposed would work for a library which can’t be run at all.

Are there specific workflow problems you think this would solve?

1 Like

Thanks!Your answer inspired me a lot.

I have another question:
Different tools discover, classify and report defects in different ways.
For the same code segment, some type checking tools may report defects while others do not.
These may confuse Python developers who use type annotations and diffenrent checking tools.

Does Python plan to provide a new official type checking tool,
or list an off-the-shelf type checking tool as the recommended and official one?

I hope my idea is helpful for you.

You never give any examples, so I’m just guessing here, but presumably you’re talking about the differences between tools like mypy (the original Python static checker, which was co-developed with PEP 484), Pyre (Facebook’s competitor), pytype (Google’s offering, which has a different goal), and pyright (the basis of strict type checking in VS Code). Or perhaps you’ve only ever experienced two of these.

It is indeed true that these tools sometimes give different results – each has different goals and audiences in mind, different limitations, a different implementation, and so on. In addition, the original PEP 484 is not very strictly specified (we had little experience in the matter at the time) and often leaves room open fr interpretation.

We’re not planning to start a new project to develop yet another static type checker. We just don’t have the resources, and it would be folly – there are already enough offerings on the “market” (in quotes, because they’re all open source and free to use).

I’m not sure what you expect from “off-the-shelf” (which I mostly associate with commercial software), but the above type checkers are all mature and get the job done. To be honest, I am personally partial to mypy, which I helped co-develop for many years, but I know that the others are all very good at what they do as well. That’s about as much of a recommendation you’ll get out of me – or out of anyone in an official position. Our users have diverse needs, and one size does not fit all. Good luck with your quest!

11 Likes

The core developers are not in the business of alienating part of the community by choosing winners. We sometime recognize a community-chosen winner by referring to it in the docs.

Pvm, your best solution is to write an OS-Shell or Python script that invokes the tools you like in the order you want, with the command-line arguments you want, revising it after you have more experience.

4 Likes