python / python/mypy

False positive "Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader" when using Unions vs TypeVars

Open
#15,683 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report
When overloading a function to specify it can return bytes if given only bytes objects, str if given only str objects, or either if given a mix of both, mypy incorrectly complains that AnyStr is the same or broader than Union[str, bytes].

Despite the warning, mypy does correctly match the Union[str, bytes] signature when appropriate

To Reproduce

Very simple test case:

from typing import overload, TypeAlias, Union, AnyStr, reveal_type
from collections.abc import Sequence
from random import choice

AllStr: TypeAlias = Union[bytes, str]

# Below produces "error: Overloaded function signature 2 will never be
# matched: signature 1's parameter type(s) are the same or broader
# [misc]"
@overload
def pick_three(choices: Sequence[AnyStr]) -> AnyStr: ...
@overload
def pick_three(choices: Sequence[AllStr]) -> AllStr: ...
def pick_three(choices: Sequence[AllStr]) -> AllStr:
    return choice(choices)

strings: tuple[str, ...] = ('abc', 'def')
byte_strings: tuple[bytes, ...] = (b'abc', b'def')
mixed_strings: tuple[AllStr, ...] = ('abc', b'def')

reveal_type(pick_three(strings))  # str
reveal_type(pick_three(byte_strings))  # bytes
reveal_type(pick_three(mixed_strings))  # Union[bytes, str], despite the warning

Some more example code on mypy Playground, with other related cases that work as expected.

Expected Behavior

Mypy does not warn that signature 2 cannot match, given it clearly is able to match based on the reveal_type(pick_three(mixed_strings)) output

Actual Behavior

main.py:39: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader  [misc]
main.py:43: note: Revealed type is "builtins.str"
main.py:44: note: Revealed type is "builtins.bytes"
main.py:45: note: Revealed type is "Union[builtins.bytes, builtins.str]"

Your Environment

Seen on Debian with Python 3.11 and mypy 1.0.1 using --strict, and in mypy Playground with Python 3.11 and mypy 1.4.1 with default options.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in main.py with Python 3.11 and mypy 1.4.1 or later, using the overloads and reveal_type calls shown in the report. The fix is done when the mixed-string call still reveals Union[bytes, str] while mypy no longer reports that overload signature 2 will never be matched.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.