DavidC
DavidC

Reputation: 1441

Mypy: no signature inference?

It looks like Mypy doesn't do anything to infer signatures. Is that correct? For example:

# types.py
def same_int(x: int) -> int:
    return x

def f(x):
    y = same_int(x)

    # This would be "Unsupported operand types for + ("int" and "str")" 
    # y + "hi"

    return y

f("hi")
f(1) + "hi"

No complaints when I do this:

mypy --check-untyped-defs types.py

Mypy will make inference about expressions within the body of f (if --check-untyped-defs is turned on). I'm wondering whether it would make sense to use that to also make and apply inferences about the signatures. (And if not, why not.)

Upvotes: 4

Views: 285

Answers (1)

Michael0x2a
Michael0x2a

Reputation: 63978

That's a deliberate design decision -- mypy was designed to allow you to mix together dynamic and typed code, mainly to make it easier to transition large and diverse codebases + allow you to selectively gain the benefits of both.

As a result, functions without type annotations are treated by default as dynamically typed functions and are implicitly given parameter and return types of Any.

Upvotes: 4

Related Questions