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

Prefer unions over overloads #245

Closed
twoertwein opened this issue Jul 12, 2022 · 6 comments
Closed

Prefer unions over overloads #245

twoertwein opened this issue Jul 12, 2022 · 6 comments

Comments

@twoertwein
Copy link
Contributor

twoertwein commented Jul 12, 2022

overloads are sometimes used in places where a union would be a "better" annotation.

Here is an example from the work-in-progress pandas-stubs:

    @overload
    def agg(self, arg: str, *args, **kwargs) -> DataFrame: ...
    @overload
    def agg(self, arg: Dict, *args, **kwargs) -> DataFrame: ...
    @overload
    def agg(self, arg: FuncType, *args, **kwargs) -> DataFrame: ...

which could be re-written as

    def agg(self, arg: str | Dict | FuncType, *args, **kwargs) -> DataFrame: ...

Not only is this much shorter but it also covers the case where the first argument is a union type (not covered by the overloads).

@AlexWaygood
Copy link
Collaborator

AlexWaygood commented Jul 12, 2022

Hmm, it's a nice idea, but it might be tricky to implement. You'd probably need to "collect" FunctionDef nodes decorated with @overload while visiting the AST, and store information about whether they're inside sys.version_info or sys.platform branches. Then, after the tree has been visited, you could go through the collected overloads for each function by a certain name within each sys.version_info/sys.platform branch, and determine whether any overloads are redundant.

So it might be possible, but also might be a little complicated. Are you interested in working on it?

@twoertwein
Copy link
Contributor Author

Are you interested in working on it?

I'm sorry, that would be too complicated for me.

@AlexWaygood
Copy link
Collaborator

You've already nerd-sniped me into thinking through a possible design, so I'm not saying "no" ;)

@twoertwein
Copy link
Contributor Author

unrelated: some of the flake8-pyi rules are also applicable to py files, for example, this overload-union case. It would be great if flake8-pyi can work on pyi files and on py files with a reduced subset of checks.

@AlexWaygood
Copy link
Collaborator

unrelated: some of the flake8-pyi rules are also applicable to py files, for example, this overload-union case. It would be great if flake8-pyi can work on pyi files and on py files with a reduced subset of checks.

Yeah, we're aware, this has come up a few times:

The core dev team for this plugin is the same as the core dev team for typeshed, though, so this always ends up being somewhat low priority for us to fix :) PRs welcome!

@AlexWaygood
Copy link
Collaborator

I had a look at implementing this, but I think it would just be unreasonably complicated for us to do here, sorry. The machinery we'd have to build to properly analyse overloaded functions (and to distinguish between different overloads inside different sys.version_info and sys.platform checks) feels like it would excessively duplicate work that type checkers already do when analysing a file.

Mypy or pyright might be interested -- you could always try filing a feature request with one of them.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jul 15, 2022
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

2 participants