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
Docs of match statement incorrectly claim that int(0|1) doesn't match False #96359
Comments
I originally raised this on the python discord server here: https://discord.com/channels/267624335836053506/709904092280914030/1013412267376775168 Either this is an error in the documentation or this is an error in cpython's implementation of the feature. There is also a general inconsistency in how these two cases are handled: >>> match False:
... case int(0):
... print("this is bad")
... case _:
... print("this is good")
...
this is bad >>> match 0:
... case bool(False):
... print("this is bad")
... case _:
... print("this is good")
...
this is good The semantics of the feature changes depending upon whether the So apparently |
@m-rutter No, that behavior is as expected, because |
@L3viathan why the special case for without >>> match 0.0:
... case 0:
... print("this is bad")
... case _:
... print("this is good")
...
this is bad with >>> match 0.0:
... case int(0):
... print("this is bad")
... case _:
... print("this is good")
...
this is good Why is it more likely to be a documentation error and not an implementation error? |
In the first case, TL;DR: |
Perhaps builtins should be and were intended to be a special case here. In fact, the docs themselves say they are a special case. Not to treat them as a special case introduces unnecessary footguns into the language. Not that you can ever fully correct the mistake of having bool extend int. |
What's apparent is that 0 does not match
The docs say they are a special case, but not in the respect you are describing. According to PEP 634:
That is, the special case is for how positional subpatterns are handled, not for how the instance test is done. There's nothing in the spec to suggest any other behaviour was intended for the instance test for built-in types.
If anything, the footgun is that But it's a bit late for this argument now, since PEP 634 has been accepted and what you're proposing would be a backwards-incompatible change. For what it's worth, I disagree that the additional special case you're proposing would be a good idea. |
@L3viathan Your suggested wording looks correct to me. Please submit a PR and assign it to Brandt for review. |
@kaya3 thanks for the explanation. I understand its impossible to change things like However, it is a shame that we have the tools to distinguish between |
@rhettinger It doesn't look like I'm able to assign a reviewer, but I made a PR. |
…honGH-96361) (cherry picked from commit 3d3a86e) Co-authored-by: Jonathan Oberländer <github@l3vi.de>
…honGH-96361) (cherry picked from commit 3d3a86e) Co-authored-by: Jonathan Oberländer <github@l3vi.de>
Documentation
In https://docs.python.org/3/reference/compound_stmts.html#class-patterns, the following sentence:
should be changed to
Since
bool
is a subclass ofint
,case int(0|1)
actually does matchFalse
:The text was updated successfully, but these errors were encountered: