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

misleading E721 Do not compare types, use isinstance() for bool #10948

Closed
Borda opened this issue Apr 15, 2024 · 3 comments
Closed

misleading E721 Do not compare types, use isinstance() for bool #10948

Borda opened this issue Apr 15, 2024 · 3 comments
Labels
rule Implementing or modifying a lint rule

Comments

@Borda
Copy link

Borda commented Apr 15, 2024

just crossed this case where for type(component_wise) is bool ruff suggest generic isinstance(component_wise, bool) which is not the same as this comparison takes int as bool:

>>> isinstance(5, bool)
True
>>> type(5) is bool
False

see why this si happening in https://stackoverflow.com/a/37888668/4521646

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Apr 15, 2024
@mscheifer
Copy link

I think your example is backwards. bools are ints but not the other way around.

>>> isinstance(5, bool)
False
>>> isinstance(True, int)
True

@dhruvmanila
Copy link
Member

Yeah, I think the behavior is correct?

>>> isinstance(5, bool)
False
>>> type(5) is bool
False

As per the linked stackoverflow question, I think the problem would be when you compare against an int where the behavior is different:

>>> isinstance(5, int)
True
>>> isinstance(True, int)
True
>>> type(5) is int
True
>>> type(True) is int
False

Also, this is already resolved in --preview, refer #7905. It should available generally in the next minor bump.

@charliermarsh
Copy link
Member

Closing based on @dhruvmanila's analysis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

5 participants