Skip to content

Doesn't correctly match overloads when calling method using conditional arguments #9264

Closed as not planned
@FFY00

Description

@FFY00
  • Are you reporting a bug, or opening a feature request?

Bug report

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import List, Optional


def example(data: Optional[List[int]] = None, size: int = 0) -> None:
    buffer = bytearray(data if data else size)
    print(buffer)


example(size=3)
example(data=[1, 2, 3])
  • What is the actual behavior/output?
error: No overload variant of "bytearray" matches argument type "object"
note: Possible overload variants:
note:     def __init__(self, ints: Iterable[int]) -> bytearray
note:     def __init__(self, length: int) -> bytearray
note:     <2 more non-matching overloads not shown>

I am forced to replace it with

if data:
    buffer = bytearray(data)
else:
    buffer = bytearray(size)
  • What is the behavior/output you expect?

I would expect mypy to pass, as I believe the arguments passed to the bytearray constructor match the overloads.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

mypy 0.782
master is the same

  • What are the mypy flags you are using? (For example --strict-optional)

mypy --strict

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions