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

Mistaken 'Name already defined' for decorated function #4117

Closed
Zac-HD opened this issue Oct 13, 2017 · 6 comments
Closed

Mistaken 'Name already defined' for decorated function #4117

Zac-HD opened this issue Oct 13, 2017 · 6 comments
Labels
bug mypy got something wrong

Comments

@Zac-HD
Copy link
Contributor

Zac-HD commented Oct 13, 2017

While working on HypothesisWorks/hypothesis#858, I've found that decorated functions are mis-detected as already defined in another branch, if either both functions or only the second are decorated. I've simplified this to the following example:

def decor(func):
    return func

if True: # or False, or any other condition
    def f(arg):
        yield
else:
    @decor  # not detected as duplicate if this decorator is removed
    def f(arg):
        yield
$ python -m mypy example.py
a.py:8: error: Name 'f' already defined

This bug is still present as of mypy 0.610

@gvanrossum
Copy link
Member

This check is trying to find true duplicates like

def f(): pass
def f(): pass

but I agree when there are different definitions in two branches it's annoying. However, what would you want to happen if the two definitions are not compatible?

@Zac-HD
Copy link
Contributor Author

Zac-HD commented Oct 14, 2017

The check actually handles a function with the same name being defined in different branches of a conditional - the problem only occurs if the second definition is decorated. I've added a comment above to reflect this. The full motivating example is this code.

So "all that's needed" for compatible functions is handling decorated functions in the existing heuristic.

There are reasonable use-cases for differently-typed functions defined in branches (eg Py2/3 compatible str/bytes handling), so the 'correct enough' behavior would be to take the union of these function types where more than one could be in scope. Currently, Mypy reveals the type of f to be whatever it was first defined as, and ignores later functions - should I open a new issue for this as a separate bug?

@gvanrossum gvanrossum added the bug mypy got something wrong label Oct 15, 2017
@gvanrossum
Copy link
Member

I guess we should keep this bug for the case where at least one of the branches is decorated (assuming the decorator doesn't change the type).

I'm not sure what should be done (regardless decorators) if the types don't match -- in some cases you'd like this to infer the intersection of the two types, but if the duplication is accidental that could cause really confusing error messages for code elsewhere that's calling the function. In any case I believe we already have some issues about that. (If you feel the need to further discuss this, please search for relevant issues first and cross-link if you find one.)

@berislavlopac
Copy link

I have a problem with this error popping up on redefining decorated functions for functools.singledispatch.

@ethanhs
Copy link
Collaborator

ethanhs commented Nov 7, 2018

@berislavlopac there is some discussion of what to do with the case of singledispatch here: #2904. At the moment a plugin handling the issues with singledispatch seem to the path forward.

@Zac-HD
Copy link
Contributor Author

Zac-HD commented Feb 28, 2020

Seems to be fixed as of 0.761 😄

chanedwin added a commit to chanedwin/pandas-profiling that referenced this issue Sep 20, 2020
chanedwin added a commit to chanedwin/pandas-profiling that referenced this issue Oct 11, 2020
enable categorical profiling for spark string types

reduce toPandas calls in describe_numeric_spark by aggregating calls

black reformatting

add mypy test requirement to fix known mypy bug in python/mypy#4117

upgrade mypy to hopefully fix _ errors

fix linting with isort and black

add spark correlations for pearson and spearman, and new method in spark dataframes to get numeric spark types

optimise kendall, spearman and pearson for spark

add categorical support in dataframe wrapper

finish cramer's computation using pure spark

update pytest stuff

start spark testing

fix spark tests, fix correlations handling 1 column or less
chanedwin added a commit to chanedwin/pandas-profiling that referenced this issue Oct 11, 2020
chanedwin added a commit to chanedwin/pandas-profiling that referenced this issue Oct 11, 2020
chanedwin added a commit to chanedwin/pandas-profiling that referenced this issue Oct 11, 2020
chanedwin added a commit to chanedwin/pandas-profiling that referenced this issue Oct 11, 2020
chanedwin added a commit to chanedwin/pandas-profiling that referenced this issue Oct 11, 2020
chanedwin added a commit to chanedwin/pandas-profiling that referenced this issue Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

5 participants