python / python/mypy

Don't report "expression contains 'Any'" if it's a `TypeOfAny.from_error`

Open
#12,016 2 comments 3 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

Update

@A5Rocks idea about not reporting expression contains Any if it's TypeOfAny.from_error is based 100. I also realized that if it's <nothing> it would still complain about attribute access etc being incorrect.

Bug Report
The reason being that when you update an overloaded signature you can get hundreds of reported "Expression contains 'Any'" errors, when there are in reality an order of magnitude less actual errors.

from typing import overload

@overload
def foo(a: int) -> int: ...
# this overload was just removed
# @overload
# def foo(a: None) -> str ...
@overload
def foo(a: str) -> str: ...
def foo(a: object) -> object: ...

a = foo(None)  # error: No overload variant of "foo" matches argument type "None"\
    # error: Expression has type "Any"

reveal_type(a)  # error: Expression has type "Any"\
  # note: Revealed type is "Any"
print(a) # error: Expression has type "Any"
bar(a) # error: Expression has type "Any"
a + "asdf" # error: Expression has type "Any"

But If TypeOfAny.from_errors are ignored:

a = foo(None)  # error: No overload variant of "foo" matches argument type "None"

reveal_type(a)  # note: Revealed type is "Any"
print(a)
bar(a)
a + "asdf"

Original Title

Invocations of overloaded functions with invalid argument types shouldn't return Any it should return <nothing>

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 minimal overloaded-call examples in the issue and trace how invalid overload resolution assigns TypeOfAny.from_error and how later expressions report that type. Done means the original overload error remains, while propagated error Any values do not produce additional "Expression has type Any" diagnostics; the issue names no files or tests to run.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.