Skip to content

Spurious warning in situation involving bytes, overload, lambda #12666

Closed
@hauntsaninja

Description

@hauntsaninja

Discovered as part of python/typeshed#7679

Given the following:

from typing import Any, Callable, overload

@overload
def sublike(pattern: str, repl: Callable[[str], str]) -> str: ...
@overload
def sublike(pattern: bytes, repl: Callable[[bytes], str]) -> bytes: ...
def sublike(*args, **kwargs) -> Any: ...

def minv1() -> str:
    return sublike("asdf", lambda m: f"{m}")

mypy produces:

test.py:10: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
Found 1 error in 1 file (checked 1 source file)

If you change to return sublike("asdf", reveal_type(lambda m: f"{m}")), you get:

asdf.py:10: note: Revealed type is "def (m: Any) -> builtins.str"
asdf.py:10: note: Revealed type is "def (builtins.str) -> builtins.str"
asdf.py:10: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
Found 1 error in 1 file (checked 1 source file)

So possibly related to the multipass logic for lambdas.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions