python / python/mypy

`--warn-return-any` not applied to generics

Open
#10,487 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

When a function annotated to return tuple[T] returns tuple[Any] no error is raised. Same with list.

To Reproduce

from __future__ import annotations
from typing import Any

def passes() -> tuple[str]:
    a: Any
    return (a,)

def fails_1() -> tuple[str]:
    a: int
    return (a,)
    # error: Incompatible return value type (got "Tuple[int]", expected "Tuple[str]")

def fails_2() -> str:
    a: Any
    return a
    # error: Returning Any from function declared to return "str"

Expected Behavior

I'd expect passes() to fail with error: Returning Tuple[Any] from function declared to return "Tuple[str]" when using mypy with --strict

Actual Behavior

Tuple[Any] is inferred to Tuple[str]

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
# setup.cfg
[mypy]
python_version = 3.8
strict = true
warn_unreachable = true
implicit_reexport = true
ignore_missing_imports = true
  • Python version used: 3.9.1
  • Operating system and version: macOS 10.13.6

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 with the supplied Python reproducer containing tuple[str], tuple[Any], list, and Any returns, and run it with the reported strict configuration. Trace how generic return types are inferred and checked, then add coverage for the tuple case and confirm that the expected Returning Tuple[Any] diagnostic is produced.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.