E721: pycodestyle v2.11.0 does not raise the error to `int == type(obj)`
評価
調査の方向性
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
- 平均マージ
- 5時間 56分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
PyCQA/pycodestyle のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
PyCQA/pycodestyle#1294 · コメント 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
PyCQA/pycodestyle#1302 · コメント 3 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 42/100
PyCQA/pycodestyle#1242 · コメント 2 件 ·
-
Spurious E231 for nested format substitutions再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
PyCQA/pycodestyle#1241 · コメント 2 件 · リアクション 4 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
PyCQA/pycodestyle#1217 ·
PyCQA/pycodestyle の issue をすべて見る
似ている issue
-
Workflow.schedule stops when a cron deadline passes before sleep対応中かも @HuaTNA が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
[Bug] HiveDialect shares its colspecs dict with SQLAlchemy's DefaultDialect (python/pyhive)対応中かも @rusackas が今日担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 80/100
apache/kyuubi#7786 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
メンテナーはふだん 1 日以内に返信
-
[20.0] hr_work_entry: Duration column of Export Time Entries is truncated to whole hours対応中かも @odooaktiv が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
fix formatting of the sample database page of the docs対応中かも @Aaqibhafeezkhan が今日担当しました。 オープンdocumentation good first issue help wanted
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
chaoss/CollectOSS#544 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信