GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-40464: fix return annotation being used as the arg annotation in singledispatch by FFY00 · Pull Request #19871 · python/cpython

Conversation

@FFY00

@FFY00 FFY00 commented May 2, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

https://bugs.python.org/issue40464

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@FFY00
FFY00 requested a review from rhettinger as a code owner May 2, 2020 23:44
@the-knights-who-say-ni

the-knights-who-say-ni commented May 2, 2020

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@FFY00

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@the-knights-who-say-ni the-knights-who-say-ni added the CLA not signed label May 2, 2020
@bedevere-bot bedevere-bot added the awaiting review label May 2, 2020
@FFY00
FFY00 force-pushed the bpo-40464 branch 3 times, most recently from adff4b2 to 5f0a324 Compare May 3, 2020 18:28
@FFY00

FFY00 commented May 3, 2020

Copy link
Copy Markdown
Member Author

@rhettinger next, when we call get_type_hints we rely on an undocumented behavior of order of __annotations__/get_type_hints. Is this correct or am I missing anything? Should I open a bug?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@the-knights-who-say-ni the-knights-who-say-ni added CLA signed and removed CLA not signed labels May 4, 2020
@FFY00

FFY00 commented Sep 21, 2020

Copy link
Copy Markdown
Member Author

Friendly ping, it has been 5 months.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@taleinat

taleinat commented Oct 18, 2020

Copy link
Copy Markdown
Contributor

@FFY00, have you considered using the inspect module to find the name of the first argument, and then checking for that in __annotations__?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@FFY00

FFY00 commented Oct 18, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@taleinat thanks for the attention!

I have, as pointed out in bpo. The issue is getting the correct first argument. This decorator operates before the method is registered in the class, so we don't know if we should skip the first argument (self) or not.

This PR only fixes the issue of return annotation being used in the dispatch signature, it only mitigates the main problem. I am not sure how to properly address the root issue here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@taleinat

taleinat commented Oct 19, 2020

Copy link
Copy Markdown
Contributor

@FFY00

The issue is getting the correct first argument. This decorator operates before the method is registered in the class, so we don't know if we should skip the first argument (self) or not.

To my understanding, we have separate singledispatch and singledispatchmethod decorators, and singledispatchmethod "magically" takes care of the "self" arg. I haven't dug too deep into the intricacies of that code, though.

Have you tried this with singledispatchmethod and seen a problem with the above mentioned approach? If so, please provide some details.

This PR only fixes the issue of return annotation being used in the dispatch signature, it only mitigates the main problem. I am not sure how to properly address the root issue here.

We really should solve the root issue. I don't think avoiding only using the return annotation, while still possibly using the annotation for another argument, is a reasonable fix, unless we've made a significant attempt at a thorough fix and have come to the conclusion that it would be unreasonably hard to do, or that it would break backwards-compatibility too harshly.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@taleinat

taleinat commented Oct 19, 2020

Copy link
Copy Markdown
Contributor

@FFY00

@taleinat thanks for the attention!

Thank you for working on this, and sorry for the great delay in giving this any attention!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@FFY00

FFY00 commented Oct 19, 2020

Copy link
Copy Markdown
Member Author

To my understanding, we have separate singledispatch and singledispatchmethod decorators, and singledispatchmethod "magically" takes care of the "self" arg. I haven't dug too deep into the intricacies of that code, though.

Ah, sorry. I forgot about this 🙃

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

FFY00 added 2 commits October 19, 2020 14:19
@FFY00
bpo-40464: fix return annotation being used as the arg annotation in …
73433f7
…singledispatch

Signed-off-by: Filipe Laíns <[email protected]>
@FFY00
test: functools: add test for bpo-40464
d8305d4
Signed-off-by: Filipe Laíns <[email protected]>
@FFY00
FFY00 force-pushed the bpo-40464 branch from 5f0a324 to d8305d4 Compare October 19, 2020 13:20
@FFY00

FFY00 commented Oct 19, 2020

Copy link
Copy Markdown
Member Author

To solve this issue I added a _arg_index argument to register, which defaults to 0 and we set it to 1 when called via singledispatchmethod. I am not too happy about this, as it exposes a new argument. Let me know if you can think of a better way 😊

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@FFY00

FFY00 commented Nov 29, 2020

Copy link
Copy Markdown
Member Author

Friendly ping.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@FFY00

FFY00 commented Nov 29, 2020

Copy link
Copy Markdown
Member Author

just an extra comment: Unfortunately, we can't rely on isinstance(method, types.MethodType), which would have more cleanly solved the issue.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

akulakov
akulakov reviewed Nov 25, 2021
View reviewed changes
Comment thread Lib/functools.py
import inspect
try:
args = list(inspect.signature(cls).parameters.keys())
except TypeError:

@akulakov akulakov Nov 25, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

signature() can also raise a ValueError if no signature can be provided.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

FFY00 and ambv reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@taleinat

taleinat commented Nov 25, 2021

Copy link
Copy Markdown
Contributor

My apologies, @FFY00, I simply haven't been able to find the time to follow this up.

@ambv, could you perhaps take a look at this?

FFY00 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger
rhettinger removed their request for review May 5, 2022 04:17
@ezio-melotti ezio-melotti removed the CLA signed label Jul 13, 2022
@serhiy-storchaka
Merge branch 'main' into bpo-40464
9559bc5
@serhiy-storchaka

serhiy-storchaka commented Feb 16, 2024

Copy link
Copy Markdown
Member

I synchronized the branch with main, but it seems that this solution is not compatible with other features added in recent years, so some existing tests now fail.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@FFY00

FFY00 commented Nov 17, 2024

Copy link
Copy Markdown
Member Author

Closing in face of that. I'll leave it for someone else to take up.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@FFY00 FFY00 closed this Nov 17, 2024
@johnslavik

johnslavik commented Oct 16, 2025

Copy link
Copy Markdown
Member

To my understanding, we have separate singledispatch and singledispatchmethod decorators, and singledispatchmethod "magically" takes care of the "self" arg. I haven't dug too deep into the intricacies of that code, though.

It doesn't: #130827

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@johnslavik johnslavik mentioned this pull request Oct 17, 2025
gh-84644: Don't single-dispatch on return types #140255
Closed
@bedevere-app bedevere-app Bot mentioned this pull request Oct 17, 2025
functools.singledispatch doesn't verify annotation is on FIRST parameter #84644
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@akulakov akulakov akulakov left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@FFY00 @the-knights-who-say-ni @taleinat @serhiy-storchaka @johnslavik @akulakov @ezio-melotti @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.