mypy don't recognizes a typing error when it happens in a function
11:10 21 Sep 2024

When I have code like this:

class C:
    pass

def f1(c: C):
    pass

f1(100)

def f2(c: C = None):
    pass

f2(100)

then mypy claims errors like this:

$ mypy 002_1.py 
002_1.py:11: error: Argument 1 to "f1" has incompatible type "int"; expected "C"
002_1.py:15: error: Argument 1 to "f2" has incompatible type "int"; expected "Optional[C]"
Found 2 errors in 1 file (checked 1 source file)

which is expected.

But when I have code like this:

class C:
    pass

def f1(c: C):
    pass

def f2(c: C = None):
    pass

def test001():
    f1(100)
    f2(100)

test001()

then mypy don't see the error.

I am using mypy 0.812 and Python 3.9.2.

What can I do to see the typing error?

python python-typing mypy