Skip to content

Fix overloading with a typevar missing - #11617

Closed
A5rocks wants to merge 4 commits into
python:masterfrom
A5rocks:fix-overloading
Closed

Fix overloading with a typevar missing#11617
A5rocks wants to merge 4 commits into
python:masterfrom
A5rocks:fix-overloading

Conversation

@A5rocks

@A5rocksA5rocks commented Nov 25, 2021

Copy link
Copy Markdown
Collaborator

Description

This fixes this following program:

importtypingclassA:
passT=typing.TypeVar("T", bound=A)
@typing.overloaddeff(a: T) ->None:
...
@typing.overloaddeff(*, copy: bool=False) ->None:
...
deff(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...).

@A5rocksA5rocks changed the title Fix overloadingFix overloading with a typevar missingNov 25, 2021
@github-actions

This comment has been minimized.

@A5rocks

Copy link
Copy Markdown
CollaboratorAuthor

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
Copy Markdown
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.overloaddeff(a: T) ->None:
...
@typing.overloaddeff(*, copy: bool=False) ->None:
...
deff(a: T= ..., *, copy: bool=False) ->None:
...

Should be:

@typing.overloaddeff(a: A) ->None:
...
@typing.overloaddeff(*, copy: bool=False) ->None:
...
deff(a: A= ..., *, copy: bool=False) ->None:
...

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

@A5rocks

A5rocks commented Nov 26, 2021

Copy link
Copy Markdown
CollaboratorAuthor

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

importtypingimportcollections.abcascollectionsclassBaseSlashCommand:
...
BaseSlashCommandT=typing.TypeVar("BaseSlashCommandT", bound=BaseSlashCommand)
@typing.overloaddefwith_slash_command(command: BaseSlashCommandT, /) ->BaseSlashCommandT:
...
@typing.overloaddefwith_slash_command(
*, copy: bool=False
) ->collections.Callable[[BaseSlashCommandT], BaseSlashCommandT]:
...
defwith_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
Copy Markdown
CollaboratorAuthor

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

@github-actions

Copy link
Copy Markdown
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
Copy Markdown
CollaboratorAuthor

Remade in #15320

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

@A5rocks@sobolevn