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 involving re.sub and + operator introduced in mypy 0.960 #12961

Closed
DMRobertson opened this issue Jun 9, 2022 · 5 comments
Closed
Labels
bug mypy got something wrong

Comments

@DMRobertson
Copy link

Bug Report

Consider the following snippet. (This is a minified example of the problem detected in matrix-org/synapse#12996.)

import re

"#" + re.sub(
    "PATTERN",
    lambda match: match.group(0) + "ANOTHER_STRING",
    "HAYSTACK",
)

Mypy 0.950 is happy with this, but mypy 0.960 is not.

main.py:5: error: Unsupported operand types for + ("bytes" and "str")

AFAICS this is a legitimate usage of re.sub, consistent with the stubs defined in typeshed.

Additional

Strangely, removing either of the addition operators and the corresponding string literal causes mypy 0.960 to accept the snippet. That is, applying this change

- "#" + re.sub(
+ re.sub(

is accepted, and applying this change

-    lambda match: match.group(0) + "ANOTHER_STRING",
+    lambda match: match.group(0),

to the vanilla snippet above is also accepted.

I'm not sure how to get mypy to reveal the type of an expression in a lambda body, but I'd guess that in the error cases it deduces match: Match[bytes] rather than match: Match[str]?

@DMRobertson DMRobertson added the bug mypy got something wrong label Jun 9, 2022
@DMRobertson
Copy link
Author

DMRobertson commented Jun 9, 2022

Bisection shows that this was introduced by 7fbf4de ("Sync typeshed"). I'm not sure if this indicates s typeshed bug or just something that a typeshed change happened to expose?

@DMRobertson
Copy link
Author

DMRobertson commented Jun 9, 2022

Ahh, digging into the typeshed change (python/typeshed#7679) this looks like this is probably related to (duplicate of?) #12665

@AlexWaygood
Copy link
Member

Ahh, digging into the typeshed change (python/typeshed#7679) this looks like this is probably related to (duplicate of?) #12665

Or #12773 (cc. @JukkaL, @JelleZijlstra)

@hauntsaninja
Copy link
Collaborator

Yup, duplicate of #12666 / 12665

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jun 9, 2022
@hauntsaninja
Copy link
Collaborator

The issue is probably something like we're not correctly silencing errors in the earlier passes for the multipass lambda logic

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