Friedrich Gretz
Friedrich Gretz

Reputation: 535

Mypy does not detect errors regarding the use of NamedTuple inside functions

In the following code the defintions of x2 and x3 are obviously wrong, yet mypy does not complain.

from typing import NamedTuple

class X(NamedTuple):
    a: float
    b: float
    c: float

def foo():
    x1 = X(1, 2, 3)
    x2 = X(1, 2)
    x3 = X("1", 2, 3)
    

If I remove the function declaration and put the three lines into the main file scope, then mypy correctly identifies both errors

from typing import NamedTuple

class X(NamedTuple):
    a: float
    b: float
    c: float

x1 = X(1, 2, 3)
x2 = X(1, 2)
x3 = X("1", 2, 3)
    

Why is that, what can I do about it?

Im running python 3.9.5 mypy 0.931

Upvotes: 0

Views: 241

Answers (1)

hussic
hussic

Reputation: 1920

Your foo function is untyped, you must say to mypy to type it with ->:

def foo() -> None:

Upvotes: 1

Related Questions