E721: pycodestyle v2.11.0 does not raise the error to `int == type(obj)`
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 48/100
Direzione di ricerca
Inizia in pycodestyle.py con COMPARE_TYPE_REGEX e comparison_type, quindi riproduci entrambe le forme di confronto usando i comandi mostrati nell’issue. Modifica la gestione in modo che int == type(obj) generi E721, mentre il comportamento esistente di type(obj) == int rimanga corretto, e verifica entrambi i casi con pycodestyle.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Python
- Stelle
- 5.2k
- Fork
- 754
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di PyCQA/pycodestyle
-
DOC: E252 is not documentedAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
PyCQA/pycodestyle#1294 · 1 commento ·
-
Typing and formattingAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
PyCQA/pycodestyle#1302 · 3 commenti ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 42/100
PyCQA/pycodestyle#1242 · 2 commenti ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
PyCQA/pycodestyle#1241 · 2 commenti · 4 reazioni ·
-
`exclude` is applied to `.`Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
PyCQA/pycodestyle#1217 ·
Tutte le issue di PyCQA/pycodestyle
Issue simili
-
bug server
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
sportsdataverse/sportsdataverse-py#641 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
googleapis/google-cloud-python#18532 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno