python / python/mypy

Function declared to return list[int] actually returning list[Any]

Open
#17,076 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

from typing import Any


def foo() -> list[int]:
    x: list[Any] = ["xxx"]
    return x

https://mypy-play.net/?mypy=latest&python=3.12&flags=strict%2Cdisallow-any-generics%2Cwarn-return-any&gist=35cfbe4d01eed0422eb72869846095c1

Expected Behavior

I would expect an error on line 6 return x. The function is declared to return list[int] but is returning list[Any].

Actual Behavior

No error.

$ mypy --strict --disallow-any-generics --warn-return-any  test.py 
Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 1.9.0
  • Mypy command-line flags: --strict --disallow-any-generics --warn-return-any
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.10.12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python example with mypy 1.9.0 and the listed strictness flags, then trace the return-type checking path for list[Any] versus list[int]. Done means the example reports an error at the return statement without breaking related type-checking behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.