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

gh-130827: Support typing.Self annotations in singledispatchmethod() #130829

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ZeroIntensity
Copy link
Member

@ZeroIntensity ZeroIntensity commented Mar 4, 2025

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
# But, it shouldn't work on singledispatch()
with self.assertRaises(TypeError):
@test.register
def silly(self: typing.Self, arg: int | str) -> int | str:
Copy link
Member

Choose a reason for hiding this comment

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

Will 'typing.Self' annotation work the same way?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question! It should, yeah. get_type_hints handles forward references.

Copy link
Member

Choose a reason for hiding this comment

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

Should we add a test case for that, maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

Possibly, but as long as get_type_hints is working it's probably fine.

Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
"typing.Self can only be used with singledispatchmethod()"
)
else:
argname, cls = next(hints_iter)
Copy link
Member

Choose a reason for hiding this comment

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

This seems like it will behave incorrectly if a parameter that is not self is annotated with Self.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry for dropping the ball here--good catch. What's the best way to deal with that? Just some extra sanity checks to somehow make sure that a Self is the first parameter?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know, maybe we should unconditionally ignore the type hint on the self parameter? Not too familiar with how singledispatchmethod works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants