-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
bpo-42345: Fix three issues with typing.Literal parameters #23294
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
Conversation
Fidget-Spinner
left a comment
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.
Hello, thank you for your PR to cpython! This looks very good to me, just one thing about de-duplicating literals pointed out in the review below.
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 still think that Literal should de-duplicate its __args__ similar to Union, although I now see the set approach won't work. Maybe you can call set on the _value_and_type_iter function, then extract out the parameters again, something like:
def Literal:
...
parameters = [p for p, _ in set(_value_and_type_iter(parameters))]
return _LiteralGenericAlias(self, parameters)This probably needs more discussion, and Guido/Ivan's decision on what the expected behavior should be. Maybe we can ask on the bpo issue.
|
@Fidget-Spinner There is a problem with Code like this will fail because of type caching: >>> Literal[0] == Literal[False]
FalseCode below will show the root cause of this problem: >>> (Literal[0], Literal[False])
(Litera[0], Literal[0])As a solution, we can disable caching for a What do you think about that? |
One possible workaround for that is to pass The only problem is I realized that A possible workaround is to just loop through them and check the other args one by one (not particularly efficient but I don't expect Literals to be too long, and with cache it should offset some of it), something like what # more code here to keep track and store the unique elements
...
for index, i_elem in enumerate(parameters):
is_duplicate = False
for j_elem in parameters[index:]:
if i_elem == j_elem:
is_duplicate = True
break
...
# more code for logic to handle the unique elementsI think before we progress further, we should wait for a core dev to respond on the bpo. They might think just having comparisons work is sufficient, and that de-duplicating args isn't needed. So let's save our effort for now 😉 . Edit: |
|
@Fidget-Spinner I forget about I totally agree with you, let wait for a decision from Guido and Ivan regarding As for me, we can skip args de-duplication because |
|
Please change the title and news text. We don’t use the style that describes the new state, we describe the action of the PR as a change to the behavior. For examples, just look at the commit log. |
|
I have updated the title and news. Sorry for that, I didn't notice this convention. My bad. |
gvanrossum
left a comment
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.
LG, except for one nit.
You might mention in the description (not the title) and the news file that you also fixed an issue where incorrectly Literal[0] == Literal[False] -- good catch, that!
|
@gvanrossum I have seen your message regarding |
Fidget-Spinner
left a comment
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.
Getting very close :). Thank you for your patience.
Fidget-Spinner
left a comment
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.
LGTM, and sorry for the weird tangents I went off on in some places!
|
@Fidget-Spinner thanks for the review and all comments 🙂 What will be the next step regarding this PR? |
@uriyyo A core dev like Guido/Ivan will take a look, once they're satisfied and approve it, it will be merged into 3.10. And probably backported to 3.9 and 3.8 too (done automatically via bots). This may take some time depending on how busy the core devs are. I'm looking forward to see your first commit to cpython :). |
|
I will look at it some time this week. |
|
Should the docs of |
|
GH-23335 is a backport of this pull request to the 3.9 branch. |
|
@gvanrossum Looks like I did it🙂 |
@uriyyo What about updating the docs of |
@Dominik1123, not sure if this is a bug, but >>> hash(Literal[1, 2, 3])
3136223724409791019
>>> hash(Literal[3, 2, 1])
4438910888190981000 |
|
@Fidget-Spinner Looks like we should not update docs because |
|
But Literal can occur inside other types, e.g. |
|
@uriyyo yeah, sorry if I wasn't clear in my original statement - I was in support of not updating the docs. Although my reasoning is slightly different. It's not about generics, the behavior Dominik mentioned happens when Union's nested in anything. The real reason why we don't need a docs update is because the Edit: # Union with __args__ not preserving order when nested due to type caching.
>>> Tuple[Union[int, str]].__args__
(typing.Union[int, str],)
>>> Tuple[Union[str, int]].__args__
(typing.Union[int, str],)
# Literal doesn't have that issue
>>> Tuple[Literal[1, 2]].__args__
(typing.Literal[1, 2],)
>>> Tuple[Literal[2, 1]].__args__
(typing.Literal[2, 1],) |
|
@Fidget-Spinner Thanks for the explanation it's clear to me now. |
|
@uriyyo Awesome! I'm glad I was able to help :). Oops, forgot to say this: Congrats to your first commit to CPython! |
@Fidget-Spinner @uriyyo I see, I didn't notice before. But isn't this very unusual in terms of behavior? So we can have two objects that compare equal but their hashes are different. AFAIK nowhere else in Python this is the case. The closest that comes to it in terms "being equal, but not having the same hash" is To quote the
So according to that it's a bug and the hash should not be order dependent. |
|
@Dominik1123 Agree with you, looks like it's a bug related to def __hash__(self):
return hash(tuple(_value_and_type_iter(self.__args__)))to: def __hash__(self):
return hash(frozentset(_value_and_type_iter(self.__args__)))@Fidget-Spinner should we create issue regarding this bug? |
I'm not sure if
Using |
|
Whoa, this is a bug. It not allowed to have two objects that compare equal but have a different hash. So since Using set() or frozenset() here is totally fine, we already use those in |
|
@gvanrossum Should we create a separate issue or just open a new PR that will point to the same issue as this PR? |
|
Create a new PR pointing to the same issue -- I already reopened it.
|
Literal equality no longer depends on the order of arguments.
Fix issue related to
typing.Literalcaching by addingtypedparameter totyping._tp_cachefunction.Add deduplication of
typing.Literalarguments.https://bugs.python.org/issue42345
Automerge-Triggered-By: GH:gvanrossum