Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#1,187 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

  • #1317 来自 @apoorvdarshan —— 已关闭,未合并
  • #1319 来自 @alexchen-sys —— 已关闭,未合并

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
tooling

调研方向

从 pycodestyle.py 中的 COMPARE_TYPE_REGEX 和 comparison_type 开始,然后使用 issue 中所示的命令重现两种比较形式。调整处理方式,使 int == type(obj) 触发 E721,同时保持现有的 type(obj) == int 行为正确,并使用 pycodestyle 验证这两种情况。

由索引模型根据 Issue 内容生成。

描述

Environment
% python -V
Python 3.11.4
% pycodestyle --version
2.11.0

% pip list
Package     Version
----------- -------
pip         23.2.1
pycodestyle 2.11.0
setuptools  68.0.0
Normal behavior
% cat example.py
if type(obj) == int:
    pass
% pycodestyle example.py
example.py:1:4: E721 do not compare types, for exact checks use `is` / `is not`, for instance checks use `isinstance()`

Great!

Strange behavior

Change type(obj) == int to int == type(obj) (swap the left and right sides).

Expected

Raise E721.
This code is still comparing the return value of type.

Actual

Do NOT raise E721.

% cat example.py
if int == type(obj):
    pass
% pycodestyle example.py
% echo $?
0
Investigation
>>> from pycodestyle import comparison_type
>>> list(comparison_type("if type(obj) == int:", noqa=False))
[(3, 'E721 do not compare types, for exact checks use `is` / `is not`, for instance checks use `isinstance()`')]
>>> list(comparison_type("if int == type(obj):", noqa=False))
[]

In v2.11.0 (21abd9b6dcbfa38635bc85a2c2327ec11ad91ffc), COMPARE_TYPE_REGEX has 2 captures.
https://github.com/PyCQA/pycodestyle/blob/21abd9b6dcbfa38635bc85a2c2327ec11ad91ffc/pycodestyle.py#L128-L131

In comparison_type, the first captured substring is only referred (inst = match.group(1)).
https://github.com/PyCQA/pycodestyle/blob/21abd9b6dcbfa38635bc85a2c2327ec11ad91ffc/pycodestyle.py#L1451-L1452
https://github.com/PyCQA/pycodestyle/blob/21abd9b6dcbfa38635bc85a2c2327ec11ad91ffc/pycodestyle.py#L1461-L1470

>>> from pycodestyle import COMPARE_TYPE_REGEX
>>> from pycodestyle import SINGLETONS

>>> match = COMPARE_TYPE_REGEX.search("if type(obj) == int:")
>>> match
<re.Match object; span=(3, 15), match='type(obj) =='>
>>> match.group(1)  # None; Go yield (L1466)
>>> match.group(2)
'obj'

>>> match = COMPARE_TYPE_REGEX.search("if int == type(obj):")
>>> match
<re.Match object; span=(7, 19), match='== type(obj)'>
>>> match.group(1)
'obj'
>>> match.group(2)
>>> inst = match.group(1)
>>> inst and inst.isidentifier() and inst not in SINGLETONS  # Go return (L1465) not yield
True

If you need to use captured substrings, you might need to do something like inst = match.group(1) or match.group(2).
However, in that case, inst and inst.isidentifier() and inst not in SINGLETONS would return True, and E721 would no longer be raised.
Therefore, I believe that removing the return statement here would resolve the reported issue.

One concern I have is that it seems inst = match.group(1) has not been changed along with the last pull request.
https://github.com/PyCQA/pycodestyle/pull/1086/files
Depending on the reason for not changing this, we might need to consider a different solution.

主要语言
Python
星标
5.2k
派生
760
平均合并
8 小时 11 分钟
30 天内合并 PR
4

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

PyCQA/pycodestyle 的其他 Issue

查看 PyCQA/pycodestyle 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。