1

The Mypy docs state:

If a directory contains both a .py and a .pyi file for the same module, the .pyi file takes precedence. This way you can easily add annotations for a module even if you don’t want to modify the source code.

That behavior basically means that if you have stub files, you will miss all type errors in the respective source files.

I expected Mypy to check and compare both the source and the stub files. Is there a workable solution to this?

5
  • 1
    You can still run type checkers on implementation files. That stub files override implementation files is one of their key purposes. There's also stubtest, but you might be better off using inline type hints. Is there a specific reason you can't do so? Commented Nov 9 at 8:27
  • thanks for the reply. No there is no reason not to use inline hints, I just feel like stub files could also be a way to move overload clutter from source files into stub files. But apparently this is not a feasible use case for stub files. Commented Nov 9 at 9:02
  • 1
    I don't know about other type-checkers, but mypy can't do this natively, because it operates on type information per module (and both my_module.py and my_module.pyi have the same module name). You might be able to craft a solution using --shadow-file and running mypy twice. (don't forget, mypy can read arguments from a file rather than all from the CLI, because its argument parser specifies fromfile_prefix_chars). Commented Nov 9 at 9:21
  • 2
    I've occasionally had both .pyis and inline-typed .pys in the same directory, usually when the quantity of type annotations and overloads are far far greater than the actual runtime code. Commented Nov 9 at 9:23
  • 1
    But apparently this is not a feasible use case for stub files - no, quite a good one. You do not have to type check every line of python you write, it is perfectly fine to mix&match multiple approaches. Just accept that the corresponding .py file is opaque to the typechecker, there's nothing wrong with that. stubtest can check the interface compatibility. Commented Nov 9 at 14:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.