Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple dispatch not recognized #11888

Closed
oskarpfeffer opened this issue Jun 16, 2024 · 1 comment
Closed

Multiple dispatch not recognized #11888

oskarpfeffer opened this issue Jun 16, 2024 · 1 comment

Comments

@oskarpfeffer
Copy link

oskarpfeffer commented Jun 16, 2024

Ruff does not recognize multiple dispatch methods.

Example:

from plum import dispatch

@dispatch
def f(x: str):
    return "This is a string!"

@dispatch
def f(x: int):
    return "This is an integer!"
$ ruff check test.py
test.py:10:5: F811 Redefinition of unused `f` from line 5
Found 1 error.
$ ruff --version           
ruff 0.4.9

Is there a way to include it as a rule to ignore the redefinition when there is @dispatch before the function?

@MichaReiser
Copy link
Member

Hi

This is an interesting use of type annotations. I don't think it's feasible for us to add an exception for plum. Plum bends python semantics in a way that are difficult to understand for static analysis tools and we rather avoid coding framework specific behavior into ruff, unless it is a very popular framework.

I just tried your example with Pyright and it also reports error when type checking the code

  /home/micha/astral/test/dispatch.py:4:5 - error: Function declaration "f" is obscured by a declaration of the same name (reportRedeclaration)
  /home/micha/astral/test/dispatch.py:12:3 - error: Argument of type "Literal['test']" cannot be assigned to parameter "x" of type "int" in function "f"
    "Literal['test']" is incompatible with "int" (reportArgumentType)
2 errors, 0 warnings, 0 information 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants