Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gjcarneiro opened this issue Mar 30, 2024 · 3 comments
Open

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

gjcarneiro opened this issue Mar 30, 2024 · 3 comments
Labels
bug mypy got something wrong

Comments

@gjcarneiro
Copy link

gjcarneiro commented Mar 30, 2024

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
@gjcarneiro gjcarneiro added the bug mypy got something wrong label Mar 30, 2024
@erictraut
Copy link

Mypy's behavior is correct and compliant with the Python typing spec. The type list[Any] is "consistent with" list[int] (or any other list). The Any type allows for "gradual typing". You can think of Any as "any type that could conceivably satisfy the type relationship".

@gjcarneiro
Copy link
Author

And neither --disallow-any-generics or --warn-return-any triggers any kind of warning?... That's disappointing 😞

@JelleZijlstra
Copy link
Member

Eric is right in general, but I do think it's worth considering whether mypy's --warn-return-any option should have triggered here, which aims to cover this sort of issue. If you're interested, feel free to try making this change in a PR and we can see the fallout. (It might lead to an unacceptably high number of low-signal errors.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants