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

Using flake8 with multiple dispatch #17795

Open
oscarbenjamin opened this issue Oct 24, 2019 · 3 comments
Open

Using flake8 with multiple dispatch #17795

oscarbenjamin opened this issue Oct 24, 2019 · 3 comments

Comments

@oscarbenjamin
Copy link
Contributor

There are many flake8 errors in sets like:

sympy/sets/handlers/intersection.py:14:1: F811 redefinition of unused 'intersection_sets' from line 10
sympy/sets/handlers/intersection.py:18:1: F811 redefinition of unused 'intersection_sets' from line 14
sympy/sets/handlers/intersection.py:22:1: F811 redefinition of unused 'intersection_sets' from line 18
...

These are because the same function name is used repeatedly in multiple dispatch:

@dispatch(ConditionSet, ConditionSet)
def intersection_sets(a, b):
return None
@dispatch(ConditionSet, Set)
def intersection_sets(a, b):
return ConditionSet(a.sym, a.condition, Intersection(a.base_set, b))
@dispatch(Naturals, Integers)
def intersection_sets(a, b):
return a

Is there a good way to tell flake8 that these are fine?

@ethankward
Copy link
Contributor

Maybe this would help, once we're able to use type annotations?

@asmeurer
Copy link
Member

Is it possible to use different function names with multiple dispatch? IMO a cleaner API would be to name each function like intersection_sets_Naturals_Integers and so on. That would also make the individual functions available for debugging. But presumably @dispatch determines which function to add the dispatch to based on its name.

@oscarbenjamin
Copy link
Contributor Author

There is a pattern that flake8 understands which is often used for single dispatch:

from sympy.multipledispatch import Dispatcher

do_stuff = Dispatcher('do_stuff')

@do_stuff.register(int)
def _(arg):
    return -arg

@do_stuff.register(float)
def _(arg):
    return 2*arg

print(do_stuff(2))
print(do_stuff(2.0))

Here flake8 does not complain about the name _ being reused. On the other hand if you want to call the function do_stuff_float etc then you can because used this way multipledispatch does not care what the function name is.

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

No branches or pull requests

3 participants