python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.35k stars 2.81k forks source link

Within Annotated[T, x], type errors in x are completely ignored #16094

Open andersk opened 1 year ago

andersk commented 1 year ago

Bug Report

mypy ignores the x expression in Annotated[T, x] without type-checking it. Although x has no effect on the typing interpretation of T, the expression x is still code that’s going to be executed at runtime*, so mypy should type-check x (as a separate object) before ignoring it.

(* Although it might not be executed immediately if from __future__ import annotations is in effect, it will be executed when typing.get_type_hints is used, so it’s still important for mypy to check it.)

An example of a real subtle bug that was missed because of this is zulip/zulip#26710.

To Reproduce

from typing import Annotated

def f(p: Annotated[int, 2 + "oops"]) -> None:
    pass

mypy Playground, Pyright playground

Expected Behavior

I expect a type error at 2 + "oops". (Pyright agrees.)

Actual Behavior

mypy finds no errors.

Your Environment

The-Compiler commented 8 months ago

This also affects typer - when e.g. accidentally calling a dynamic default value instead of passing the function:

import typer
from typing import Annotated

app = typer.Typer()

def get_year() -> int:
    return 2023

@app.command()
def cmd_old(year: int = typer.Option(default_factory=get_year())) -> None:
    pass

@app.command()
def cmd_new(year: Annotated[int, typer.Option(default_factory=get_year())]) -> None:
    pass

mypy detects the wrong usage in the old (discouraged) API, but happily accepts the newer variant.