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

Bytes formatting error gets produced during failed overload match #12665

Closed
JelleZijlstra opened this issue Apr 24, 2022 · 7 comments
Closed
Labels
bug mypy got something wrong topic-overloads topic-type-context Type context / bidirectional inference

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Apr 24, 2022

Bug Report

On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc" gets produced during bidirectional inference in a failed overload match.

To Reproduce

from typing import overload, Callable

@overload
def sub(pattern: str, repl: Callable[[str], str]) -> str: ...
@overload
def sub(pattern: bytes, repl: Callable[[bytes], bytes]) -> bytes: ...
def sub(pattern: object, repl: object) -> object:
    raise NotImplementedError

def better_snakecase(text: str) -> str:
    text = sub(r"([A-Z])([A-Z]+)([A-Z](?:[^A-Z]|$))", lambda match: f"{match}")
    return text

Expected Behavior

No errors

Actual Behavior

test_cases/stdlib/typing/pattern.py:11: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior

Your Environment

  • Mypy version used: master, 0.942
  • Mypy command-line flags: none

Found in python/typeshed#7679, the repro case minified from some code in homeassistant.

@AlexWaygood
Copy link
Member

This appears to have been fixed by a3abd36 (#12631), unless I did the git bisect wrong. I can't reproduce the error on the original example with a3abd36 checked out, but I can with the commit before it.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Nov 22, 2022

Hmm, this happened to me too with that exact same PR: #8814 (comment) . Since it's unexpected, seems worth understanding the mechanism / adding regression tests.

@AlexWaygood
Copy link
Member

Oh, looks like it's actually not entirely unexpected: Jelle mentioned in #12631 (comment) that the PR was touching logic that impacted this bug. But we should definitely add a test, agreed.

@sebwills
Copy link

sebwills commented Feb 9, 2023

I'm getting the same error mypy_test.py:6: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior [str-bytes-safe] with the following repro

import pandas as pd

df = pd.DataFrame({'A': [1]})

x = f"Hello {df.loc[0, 'A']}"

mypy: 1.0.0 (strict=True)

@sebwills
Copy link

sebwills commented Feb 9, 2023

Sorry, ignore my previous comment, it went away when I upgraded pandas-stubs

@erictraut
Copy link

This appears to have been fixed in mypy 0.960.

@AlexWaygood
Copy link
Member

We were keeping this open so that we didn't forget to add a regression test, since this was a strange bug that was fixed somewhat by accident: #12665 (comment). But it looks like we forgot to anyway :)

I filed #15867 to add a regression test.

JelleZijlstra pushed a commit that referenced this issue Aug 14, 2023
Adds a regression test for #12665, which is a strange bug that was fixed
somewhat by accident
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-type-context Type context / bidirectional inference
Projects
None yet
Development

No branches or pull requests

5 participants