-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
TypeVar's assumed to always be of different types #5464
Comments
(see below) |
@Michael0x2a Thanks for replying so quickly. Grouping a list of ints using a string key should be perfectly legal, this is intentional. There are plenty of ways to group a list of ints using a string key, I'm not sure what you're trying to say by that example. Just consider grouping integers by their first digit (or sign): igroupby([123, 12, -32], key=lambda x: str(x)[0]) Don't ask me why this would be useful, but it seems like a bad idea to constrain the type signature of the
For my desired behavior, it seems like it would be safe for all possible types Regardless, if this annotation is not correct, how can it be correctly annotated given my desired behavior? |
A similar annotation is used in typeshed for |
@ErikBjare -- oh wow, I badly misread your example. My bad -- please disregard my previous post. I vaguely remember reading about a similar issue before, but I don't seem to be able to find the relevant issue. In the meantime, you could perhaps take a page from how typeshed types |
I think this is a duplicate of #3737. See that issue for suggestions of workarounds. |
That did indeed work, see this commit: https://github.com/ErikBjare/QSlang/blob/01b12fdd5183b41779e27478c2317730ee2ea11b/igroupby.py Thanks @Michael0x2a and @ilevkivskyi! |
Background
I was annoyed with the default behavior of
itertools.groupby
(requiring input to be sorted to work correctly, and returning iterators) so I implemented a helper function that presorts and computes result immediately.It works fine, but something unexpected happened when I wanted to give a shot at annotating it with
TypeVar
.Code
Modified version of igroupby.py in my QSlang repo
What is the actual behavior/output?
What is the behavior/output you expect?
I expect it to not complain about the default key function, since
T
andK
could be the same type.Perhaps current behavior is expected behavior, but if it is then there's a gap in my knowledge I'd like to fill.
What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
Yes.
What are the mypy flags you are using? (For example --strict-optional)
None
The text was updated successfully, but these errors were encountered: