python / python/mypy

Cannot deduce correct type for varargs and overloads involving unions

Open
#14,189 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-join-v-union
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

I'm not really sure if this is strictly a bug or not, but as I understand it now, it seems to be.

Mypy fails to deduce the correct type for overloads involving unions, but I'm not sure why.

Please refer to the code snippet. If I'm wrong in my understanding, or if I should type the coalesce function in a better/different way, please let me know!

To Reproduce

MyPy Playground link: https://mypy-play.net/?mypy=latest&python=3.11&gist=5efc4f884ef3a447bf050a2eab005f70

from typing_extensions import overload
from  typing import TypeVar


_T = TypeVar("_T")


@overload
def coalesce(
    *values: _T | None,
) -> _T | None:
    ...


@overload
def coalesce(
    *values: _T | None,
    default: _T,
) -> _T:
    ...


def coalesce(
    *values: _T | None,
    default: _T | None = None,
) -> _T | None:
    """Resolves to the first non-null value, or to `default` if no value different than `None` could be found.
    """
    for value in values:
        if value is not None:
            return value
    return default


source: str | bool | None = "a"
default: str | bool = False
output: str | bool = coalesce(source, default=default)

# main.py:35: error: Incompatible types in assignment (expression has type "object", variable has type "Union[str, bool]")  [assignment]
# Found 1 error in 1 file (checked 1 source file)

Expected Behavior

It should work.

Actual Behavior

Mypy emits the following error, for the line where output is assigned:

main.py:35: error: Incompatible types in assignment (expression has type "object", variable has type "Union[str, bool]") [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files): (all defaults)
  • Python version used: CPython 3.7

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 with the linked MyPy Playground reproduction and confirm the reported error for the varargs overloads using unions. Trace the overload and type-inference behavior responsible for producing object, then add a focused regression test; done means the example infers str | bool for output without an assignment error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.