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

False positive when calling overloaded function in Python 2 mode only #11609

Closed
JukkaL opened this issue Nov 24, 2021 · 4 comments
Closed

False positive when calling overloaded function in Python 2 mode only #11609

JukkaL opened this issue Nov 24, 2021 · 4 comments
Labels
bug mypy got something wrong topic-overloads topic-python2 issues only applicable to Python 2

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 24, 2021

The second call at the bottom of this example generates an unexpected error, but only in Python 2 mode:

from typing import Any, Literal, overload

@overload
def f(
    x,  # type: Any
    y,  # type: Literal[True]
):
    # type: (...) -> None
    pass

@overload
def f(
    x,  # type: Any
    y,  # type: Literal[False]
):
    # type: (...) -> None
    pass

def f(x, y):
    pass

f(1, False)  # OK
f((1 if int() else 1), False)  # Error

Here's the output:

t.py:23: error: No overload variant of "f" matches argument types "int", "bool"
t.py:23: note: Possible overload variants:
t.py:23: note:     def f(x: Any, y: Literal[True]) -> None
t.py:23: note:     def f(x: Any, y: Literal[False]) -> None

This is a regression from mypy 0.910.

@JukkaL JukkaL added the bug mypy got something wrong label Nov 24, 2021
@JukkaL
Copy link
Collaborator Author

JukkaL commented Nov 24, 2021

It looks like this was caused by #10666 somehow.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Nov 24, 2021

First, it looks like #10666 is broken in Python 2 mode, because the rules for booleans are different. However, the new feature is not enabled by default, so there seems to be another bug that causes a disabled feature to affect type checking results.

@hauntsaninja
Copy link
Collaborator

Looks like something in the error reporting mechanism has some side effect, e.g. this patch passes:

diff --git a/mypy/checker.py b/mypy/checker.py
index 763176a3e..3f6d2ea25 100644
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -4266,13 +4266,15 @@ class TypeChecker(NodeVisitor[None], CheckerPluginInterface):
             else:
                 return f'Expression has type {typ}'
 
+        fail = lambda *a, **kw: None
+
         if isinstance(t, FunctionLike):
-            self.fail(message_registry.FUNCTION_ALWAYS_TRUE.format(format_type(t)), expr)
+            fail(message_registry.FUNCTION_ALWAYS_TRUE.format(format_type(t)), expr)
         elif isinstance(t, UnionType):
-            self.fail(message_registry.TYPE_ALWAYS_TRUE_UNIONTYPE.format(format_expr_type()),
+            fail(message_registry.TYPE_ALWAYS_TRUE_UNIONTYPE.format(format_expr_type()),
                       expr)
         else:
-            self.fail(message_registry.TYPE_ALWAYS_TRUE.format(format_expr_type()), expr)
+            fail(message_registry.TYPE_ALWAYS_TRUE.format(format_expr_type()), expr)

@JelleZijlstra JelleZijlstra added topic-overloads topic-python2 issues only applicable to Python 2 labels Mar 19, 2022
@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 31, 2022

Closing due to Python 2 feature freeze (#12237).

@JukkaL JukkaL closed this as completed Mar 31, 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 topic-overloads topic-python2 issues only applicable to Python 2
Projects
None yet
Development

No branches or pull requests

3 participants