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

singledispatch and typing.Tuple[x] interaction #384

Closed
Tinche opened this issue Feb 2, 2017 · 3 comments · Fixed by #386
Closed

singledispatch and typing.Tuple[x] interaction #384

Tinche opened this issue Feb 2, 2017 · 3 comments · Fixed by #386

Comments

@Tinche
Copy link

Tinche commented Feb 2, 2017

Hello,

Python 3.5, but I'm using typing.py from this repo, master branch. (Trying to port a library.)

I realize this is not really the intended use of typing.

I'm not really sure if this is an issue in typing or singledispatch, but it used to work before. Here's a small test case. Note that typing.Tuple[int] is a subclass of typing.Tuple, and typing.Tuple is a subclass of typing.Sequence.

import typing
from functools import singledispatch

s = singledispatch(lambda: 'default')
s.register(typing.Sequence, lambda: 'sequence')
s.register(typing.Tuple, lambda: 'tuple')

print(s.dispatch(typing.Tuple[int])())

If you run it a number of times, the results are either 'tuple' or 'sequence', based on the ordering of an internal dict in singledispatch. This is a bug in singledispatch, right?

@ilevkivskyi
Copy link
Member

This looks indeed like a subtle bug in singledispatch, it should raise an error in ambiguous situations.

However, while playing with this, I have found another issue: issubclass(Node[int], Node) returns False. I would like it to return True and I have a very simple fix that accidentally also fixes your case to always print 'tuple' (which is right, since it is a more narrow type, and singledispatch determines this correctly).

@ilevkivskyi
Copy link
Member

Here is the PR #386 I promised

@Tinche
Copy link
Author

Tinche commented Feb 3, 2017

Thank you!

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 a pull request may close this issue.

2 participants