-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Should PYI041
consider bool
to be redundant with int
?
#9810
Comments
@AlexWaygood - I defer to you on this one. |
I don't think this makes sense to me, they seem more distinct than |
To me, they're very different in intended usage, but surprisingly related in actual effect, which threw me off, especially since the rule warned about the other scenario and not this one. |
I just stumbled into this rule, and I think the rule is wrong. The PEP mentioned in the rule documentation ( PEP 3141 ) is talking about the numbers ABCs, these are, numbers.Complex, numbers.Real and numbers.Integral. float is a particular class that implements numbers.Real, int is a particular class that implements. But they don't have a relationship between them. Afaik, the numerical hierarchy defined in the pep-3141 can be seen/tested using:
Am I missing something? |
The rule isn't wrong @maxyz, but you're right that it's quite misleading for the docs to mention PEP 3141. The rule is premised on the fact that type checkers do not support PEP 3141 (and never will) -- the ABCs introduced by that PEP are wholly incompatible with Python's static-typing system and impossible to annotate in a desirable way in typeshed. For more, see:
The "numeric tower" that the docs should be linking to is this one here: https://peps.python.org/pep-0484/#the-numeric-tower. (If you think this situation is confusing, you're not alone :) I'll make a docs PR to improve this situation. |
Currently, this rule only flags
int | float
unions as redundant. Coming from other languages, I didn't even realizeint
was a subtype offloat
, but I learned about it because of this rule. However, I found a bug in my code where I was convertingFalse
to0.0
- this was because in Python,bool
is actually anint
. I found that the tests for this rule very explicitly considerint | bool
to not be a violation, however this would have prevented me from writing the bug as I did.The
bool
toint
relation is in some ways stronger thanint
tofloat
sinceisinstance()
only recognizes the former:The text was updated successfully, but these errors were encountered: