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

Name '_' already defined for "@register" function registration pattern #6752

Closed
jonathanslenders opened this issue May 1, 2019 · 2 comments
Labels

Comments

@jonathanslenders
Copy link

jonathanslenders commented May 1, 2019

I'm adding type annotations to the prompt_toolkit library.
The code for handling the key bindings looks something like this:

    @handle('up')
    def _(event: E) -> None:
        event.current_buffer.auto_up(count=event.arg)

    @handle('down')
    def _(event: E) -> None:
        event.current_buffer.auto_down(count=event.arg)

    @handle('delete', filter=has_selection)
    def _(event: E) -> None:
        data = event.current_buffer.cut_selection()
        event.app.clipboard.set_data(data)

The @handle decorator is registering the function as a callback somewhere. The name is irrelevant, because it's never referred to in the scope where it's defined, hence the name _.

Mypy however, doesn't like it:

prompt_toolkit/key_binding/bindings/basic.py:179: error: Name '_' already defined on line 160
prompt_toolkit/key_binding/bindings/basic.py:183: error: Name '_' already defined on line 160
prompt_toolkit/key_binding/bindings/basic.py:187: error: Name '_' already defined on line 160
prompt_toolkit/key_binding/bindings/basic.py:194: error: Name '_' already defined on line 160
prompt_toolkit/key_binding/bindings/basic.py:207: error: Name '_' already defined on line 160

I would hate to give all 214 occurrences of key bindings a name, if it doesn't matter.

 $ grep 'def _(' -R prompt_toolkit/ | wc -l
214

Would it be possible to add an exception to mypy, which allows function names to be rebound in the same scope, if they are never called by their name binding?
If so, that would be great, and I think not just for prompt_toolkit and its users.

(Btw, I'm at PyCon right now.)

Thanks a lot!

@ethanhs
Copy link
Collaborator

ethanhs commented May 1, 2019

@jonathanslenders You may be interested in the discussion in #2904. Unfortunately, there isn't a good method of making an exception to this pattern that I am aware of, so a plugin seems the optimal solution.

Also if you are at PyCon you may be interested in the typing summit https://mail.python.org/archives/list/typing-sig@python.org/thread/P36XOWSKFZRHW7SOJTGVJUOHAZRDAHVQ/

(I unfortunately couldn't make it to PyCon this year, but the rest of the mypy team is there).

@jonathanslenders
Copy link
Author

Thank you @ethanhs for the quick reply,
That discussion covers the problem indeed.
I'll close this issue.

Otherwise, I'm really happy with everything mypy does.

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

No branches or pull requests

2 participants