Search code examples
pythonpep8flake8

How to get programatic access to list of Pep8 Error codes


I'm having a tough time finding how how to get a list of the pep8 error codes and some table or function to lookup their messages or properties.

There is a list here: https://gist.github.com/mjgreen/bda13692b696669cb3fcd5a0fb682958

I've tried pep8, pycodestyle, flake8, and autoflake8, but I'm not having luck. Whatever way exists to grab these doesn't exist the way I thought it would. (I was expecting a hard-coded message table). I'm probably just being dense. My thought is flake8 must have a way to get them, it prints them out.

How do I import flake8 and then just generate some dictionary that maps error codes to error messages / and or properties?


Solution

  • if you're looking particularly for "codes that could be emitted from flake8 plugins" given your current plugin set there isn't a way to know all of those

    the way plugin registration works is flake8 is handed a prefix of the error codes that a plugin might emit -- the plugin is then free to emit anything in that range (and with any arbitrary string for message)

    the plugin is also free to emit error codes outside of their prefix, but they are ignored (this may be come an error in the future, I haven't quite decided what the correct behaviour here is yet)

    collecting those prefixes is relatively straightforward (though you'd probably also want to think about flake8:local-plugins as well):

    for dist in importlib.metadata.distributions():
        for ep in dist.entry_points or ():
            if ep.group == 'flake8.extension':
                print(ep.name)
    

    note here that pycodestyle is a special case here (due to historical reasons)


    pycodestyle (previously named pep8) - the tool which implements the "pep8" error codes also does not provide programmatic access to the codes and messages (they are inlined in the functions which produce the errors)

    as such any such list of codes and messages is unofficial and likely outdated / incorrect


    disclaimer: I am the current flake8 maintainer and I am one of the maintainers of pycodestyle