Skip to content

Preserve types of functions registered with singledispatch #10756

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

Merged

Conversation

pranavrajpal
Copy link
Contributor

Description

When calling a function that has been registered as an implementation for a singledispatch function, currently, mypy doesn't type check arguments to those functions when those functions are called directly (instead of through the main singledispatch function). This fixes that by preserving the type of the registered function even after the register decorator has been used.

Test Plan

I added a test that made sure that we report errors when using registered functions directly.

Add a test that makes sure that we preserve the type of registered
functions and we correctly type check errors when registered functions
are called directly.
Make mypy keep track of the declared type of registered functions, which
allows properly type checking code that calls those registered functions
directly, without going through the singledispatch function.
@github-actions

This comment has been minimized.

Instead of deciding what to return in register_function, returning
ctx.default_return_type when there's an error with the register call,
just return the function directly from the callback. That allows calls
to registered functions to be type checked properly even when registered
incorrectly, and makes the code simpler and easier to test.
@github-actions
Copy link
Contributor

github-actions bot commented Jul 3, 2021

Diff from mypy_primer, showing the effect of this PR on open source code:

edgedb (https://github.com/edgedb/edgedb.git)
- edb/ir/ast.py:759:12: error: "None" not callable

Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@msullivan msullivan merged commit e07ad3b into python:master Jul 3, 2021
@pranavrajpal pranavrajpal deleted the preserve-registered-impls-types branch July 3, 2021 02:06
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

Successfully merging this pull request may close these issues.

2 participants