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

Fix overloading with a typevar missing #11617

Closed
wants to merge 4 commits into from

Conversation

A5rocks
Copy link
Contributor

@A5rocks A5rocks commented Nov 25, 2021

Description

This fixes this following program:

import typing

class A:
    pass

T = typing.TypeVar("T", bound=A)

@typing.overload
def f(a: T) -> None:
    ...

@typing.overload
def f(*, copy: bool = False) -> None:
    ...

def f(a: T = ..., *, copy: bool = False) -> None:
    ...

This also fixes #9023.

Test Plan

I haven't added any tests yet, I plan to do that based on mypy primer output (still not sure this works...).

@A5rocks A5rocks changed the title Fix overloading Fix overloading with a typevar missing Nov 25, 2021
@github-actions

This comment has been minimized.

@A5rocks
Copy link
Contributor Author

A5rocks commented Nov 25, 2021

oh that broke terribly didn't it.

@A5rocks A5rocks marked this pull request as draft November 25, 2021 20:07
@github-actions

This comment has been minimized.

@A5rocks A5rocks marked this pull request as ready for review November 25, 2021 23:07
@sobolevn
Copy link
Member

I think that the given example is not ideal. Because typevar should not even be used in this example:

T = typing.TypeVar("T", bound=A)

@typing.overload
def f(a: T) -> None:
    ...

@typing.overload
def f(*, copy: bool = False) -> None:
    ...

def f(a: T = ..., *, copy: bool = False) -> None:
    ...

Should be:

@typing.overload
def f(a: A) -> None:
    ...

@typing.overload
def f(*, copy: bool = False) -> None:
    ...

def f(a: A = ..., *, copy: bool = False) -> None:
    ...

Maybe you should also add an example, where TypeVar is actually needed?

@A5rocks
Copy link
Contributor Author

A5rocks commented Nov 26, 2021

The given example was an oversimplification of the original, which can be simplified to:

import typing
import collections.abc as collections

class BaseSlashCommand:
    ...

BaseSlashCommandT = typing.TypeVar("BaseSlashCommandT", bound=BaseSlashCommand)

@typing.overload
def with_slash_command(command: BaseSlashCommandT, /) -> BaseSlashCommandT:
    ...

@typing.overload
def with_slash_command(
    *, copy: bool = False
) -> collections.Callable[[BaseSlashCommandT], BaseSlashCommandT]:
    ...

def with_slash_command(
    command: BaseSlashCommandT = ..., /, *, copy: bool = False
) -> typing.Union[BaseSlashCommandT, collections.Callable[[BaseSlashCommandT], BaseSlashCommandT]]:
    ...

The original can be found at https://github.com/FasterSpeeding/Tanjun/blob/7edec0fa4418718343826f9c6489bd817c4a9b47/tanjun/components.py#L590-L604 (yes I know the / is missing after self in the second overload, I have that fixed locally)

@A5rocks
Copy link
Contributor Author

A5rocks commented Nov 28, 2021

Went through some issues that seemed partially related -- it looks like #9023 gets fixed by this too!

@github-actions
Copy link
Contributor

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

Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/abc.py:3139: error: Overloaded function implementation cannot satisfy signature 2 due to inconsistencies in how they use type variables  [misc]
- tanjun/abc.py:3205: error: Overloaded function implementation cannot satisfy signature 2 due to inconsistencies in how they use type variables  [misc]

@A5rocks
Copy link
Contributor Author

A5rocks commented May 29, 2023

Remade in #15320

@A5rocks A5rocks closed this May 29, 2023
@A5rocks A5rocks deleted the fix-overloading branch May 29, 2023 07:11
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.

False positive when type checking overloads with generics
2 participants