Skip to content

E721: Fix false negative for reversed type comparisons (e.g. int == type(obj))#1321

Closed
fazalpsinfo-cmyk wants to merge 1 commit into
PyCQA:mainfrom
fazalpsinfo-cmyk:fix/e721-reversed-comparison
Closed

E721: Fix false negative for reversed type comparisons (e.g. int == type(obj))#1321
fazalpsinfo-cmyk wants to merge 1 commit into
PyCQA:mainfrom
fazalpsinfo-cmyk:fix/e721-reversed-comparison

Conversation

@fazalpsinfo-cmyk

Copy link
Copy Markdown

Summary

Fixes #1187.

The E721 check was missing reversed comparisons where type() appears on the right side of the comparison operator. For example, int == type(obj) was not flagged even though it's equivalent to type(obj) == int.

Root Cause

The COMPARE_TYPE_REGEX has two alternatives — one for type() == X and one for X == type(). The bypass logic that allowed comparing types with == when the inner argument was an identifier only worked correctly when type() was on the left. When type() was on the right, the bypass logic incorrectly used the captured identifier as the comparison value.

Change

  • Removed the bypass logic from comparison_type() that allowed type(foo) == type(bar) to pass without an E721 warning. Per PEP 8, comparing types directly with == should always use isinstance().
  • This fix also correctly handles type(None) == type(False) style comparisons that were previously allowed.

If you found this useful, consider buying me a coffee

@asottile asottile closed this Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

E721: pycodestyle v2.11.0 does not raise the error to int == type(obj)

2 participants