-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
base: main
Are you sure you want to change the base?
gh-130827: Support typing.Self
annotations in singledispatchmethod()
#130829
Conversation
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: |
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.
Will 'typing.Self'
annotation work the same way?
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.
Good question! It should, yeah. get_type_hints
handles forward references.
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.
Should we add a test case for that, maybe?
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.
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) |
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.
This seems like it will behave incorrectly if a parameter that is not self
is annotated with Self
.
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.
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?
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.
I don't know, maybe we should unconditionally ignore the type hint on the self
parameter? Not too familiar with how singledispatchmethod works.
singledispatchmethod.register
fails withtyping.Self
annotation #130827