python / python/mypy

Using package multipledispatch, mypy gives error name already defined

Open
#11,065 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy reports errors when using package multipledispatch for overloading functions.

To Reproduce

from multipledispatch import dispatch


@dispatch(list, str)
def concatenate(a: list, b: str):
    a.append(b)
    return a


@dispatch(str, str)
def concatenate(a: str, b: str):
    return a + b


@dispatch(str, int)
def concatenate(a: str, b: int):
    return a + str(b)


@dispatch(int, str)
def concatenate(a: int, b: str):
    return str(a) + b


print(concatenate(["a", "b"], "c"))
# ['a', 'b', 'c']
print(concatenate("Hello", "World"))
# HelloWorld
print(concatenate("a", 1))
# a1
print(concatenate(2, "b"))
# 2b

Expected Behavior

No errors

Actual Behavior

mypy multi.py

multi.py:10: error: Name "concatenate" already defined on line 4
multi.py:15: error: Name "concatenate" already defined on line 4
multi.py:20: error: Name "concatenate" already defined on line 4

(Write what happened.)

Your Environment

  • Mypy version used: 0.910
  • Python version used: 3.8.6
  • Operating system and version: macOS 11.5.2
  • Installed packages:
    flake8==3.9.2
    mccabe==0.6.1
    multipledispatch==0.6.0
    mypy==0.910
    mypy-extensions==0.4.3
    pycodestyle==2.7.0
    pyflakes==2.3.1
    six==1.16.0
    toml==0.10.2
    typing-extensions==3.10.0.2

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 running the reported mypy multi.py example with the shown multipledispatch definitions and confirm the repeated-name diagnostics. Trace the name-checking path for these repeated function definitions and compare it with existing overload handling; done means the example type-checks without those errors while ordinary duplicate definitions remain diagnosed.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.