Using pytest, I have test_all.py module. If I put this at the root of the module:
len(1)
If I run:
mypy tests
I get the expected:
tests/test_all.py:1: error: Argument 1 to "len" has incompatible type "int"; expected "Sized"
But, if I change the code of my module to do:
def test_import():
len(1)
And run mypy again, mypy doesn't warn me anymore. I have no idea why putting the code in this function suddenly seems to cloak it from mypy.
I'm on Ubuntu 16.04, using mypy version 0.4.5 and Python 3.5.
Mypy, by design, will ignore the contents of any functions that don't have type annotations -- this feature is intended to make it easier for people to slowly transition over a non-typechecked codebase to being typechecked without having to deal with a bunch of spurious errors.
You can fix this by either adding annotations to your function:
def test_import() -> None:
len(1)
...or by calling mypy with the --check-untyped-defs flag from the command line. If that flag is specified, Mypy will assume all untyped functions have parameters and return values of type Any and will typecheck your code accordingly.
See http://mypy.readthedocs.io/en/latest/common_issues.html#no-errors-reported-for-obviously-wrong-code for a few more details, and possibly http://mypy.readthedocs.io/en/latest/command_line.html#additional-command-line-flags.
Thanks.
Most helpful comment
Mypy, by design, will ignore the contents of any functions that don't have type annotations -- this feature is intended to make it easier for people to slowly transition over a non-typechecked codebase to being typechecked without having to deal with a bunch of spurious errors.
You can fix this by either adding annotations to your function:
...or by calling mypy with the
--check-untyped-defsflag from the command line. If that flag is specified, Mypy will assume all untyped functions have parameters and return values of typeAnyand will typecheck your code accordingly.See http://mypy.readthedocs.io/en/latest/common_issues.html#no-errors-reported-for-obviously-wrong-code for a few more details, and possibly http://mypy.readthedocs.io/en/latest/command_line.html#additional-command-line-flags.