Skip to content

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

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

Open
KotlinIsland opened this issue Jan 19, 2022 · 2 comments
Open
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Jan 19, 2022

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>

@KotlinIsland KotlinIsland added the bug mypy got something wrong label Jan 19, 2022
@A5rocks
Copy link
Collaborator

A5rocks commented Jan 19, 2022

I think it would be a better idea to not complain about Any if it's from an error (TypeOfAny.from_error), due the amount of code change necessary and because the type system is not perfect.

I can see the argument for returning a bottom type on type errors instead of Any due to type safety (Any is super duper unsafe, bottom types less so although both can be casted to whatever), though. (Bottom type meaning something akin to NoReturn, which... Is <nothing> a bottom type?)

@KotlinIsland
Copy link
Contributor Author

KotlinIsland commented Jan 19, 2022

@A5rocks

Is <nothing> a bottom type?

a: NoReturn
reveal_type(a)  # <nothing>

In Kotlin(and Scala) the bottom type is called Nothing

@KotlinIsland KotlinIsland changed the title Invocations of overloaded functions with invalid argument types shouldn't return Any it should return <nothing> Don't report "expression contains 'Any'" if it's a TypeOfAny.from_error Jan 19, 2022
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

2 participants