Search code examples
pythonpep8flake8pycodestyle

flake8 & pycodestyle/pep8 not showing E721 errors


Versions
λ python --version
Python 3.10.6

λ flake8 --version
5.0.4 (mccabe: 0.7.0, pycodestyle: 2.9.1, pyflakes: 2.5.0) CPython 3.10.6 on Linux 
# and on Windows

## Edit: after update
λ flake8 --version
6.0.0 (mccabe: 0.7.0, pycodestyle: 2.10.0, pyflakes: 3.0.1) CPython 3.10.6 on Linux
Error to Catch
Example (test.py)
test = []
if type(test) == list:
    print('test is a list')
else:
    print('test not a list')

Both flake8 test.py & pycodestyle test.py commands, in terminal, do not show any errors. Yet they should.
I have no extra config, from what I'm reading this error should be enabled by default; Per pycodestyle 2.9.1 Docs

  • unless disabled per line with # noqa

I've also tried:

  • {flake8|pycodestyle} --select E721 test.py to explicitly select the error
  • {flake8|pycodestyle} --ignore E302 test.py to clear the default ignore list
  • {flake8|pycodestyle} --ignore E302 --select E721 test.py

Am I missing something?- I quite like this error and now I'm worried it's not catching other errors as well.


Solution

  • Hopefully this will help somebody in the future.

    This is proper behavior, the Docs do not correctly describe the error

    Per this comment in the source code:

    # Allow comparison for types which are not obvious
    
    • Location: pycodestyle.py, comparison_type(), line 1436 in version 2.10.0

    obvious types are:

    SINGLETONS = frozenset(['False', 'None', 'True'])
    
    • Location: pycodestyle.py, line 104 in version 2.10.0

    Their Doc example of if type(user) == User: is not obvious and therefor wouldn't trigger it

    Examples

    test = []
    print(type(test) == list)         # Pass
    print(type(test) == type(list))   # Pass
    print(type(test) == type(float))  # Pass
    print(type(test) == type(None))   # Fail
    print(type(test) == type(True))   # Fail
    

    I presume some Linters, possible pylint, does not work like this- thus my confusion, as I saw this error while using pylsp-all in Vim