Code Monkey home page Code Monkey logo

flake8-pyproject's Introduction

Flake8-pyproject

Flake8 plug-in loading the configuration from pyproject.toml

Flake8 cannot be configured via pyproject.toml, even though virtually all other Python dev tools have adopted it as the central location for project configuration. The discussion of the original proposal (#234) was closed as "too heated", subsequent feature and pull requests were marked as "spam" (#1332, #1421, #1431, #1447, #1501).

Flake8-pyproject also has bad manners and force-feeds Flake8 the spam it so despises. It registers itself as a Flake8 plug-in to seamlessly load the configuration from pyproject.toml when you run the flake8 command.

Usage

Say your Flake8 configuration in .flake8 (or in tox.ini, or setup.cfg) is this:

[flake8]
ignore = E231, E241
per-file-ignores =
    __init__.py:F401
max-line-length = 88
count = true

Copy that [flake8] section to pyproject.toml, rename it as [tool.flake8], and convert the key–value pairs to the TOML format:

[tool.flake8]
ignore = ['E231', 'E241']
per-file-ignores = [
    '__init__.py:F401',
]
max-line-length = 88
count = true

Then run flake8 in the project root folder, where pyproject.toml is located.

In case your TOML-based configuration is contained in a different folder, or the file has a different name, specify the location with the --toml-config command-line option.

For compatibility with earlier versions of this package, and perhaps extra reliability in terms of possible future breakage of the plug-in hook, the package also provides a flake8p command that could be called alternatively to lint the code.

Implementation

Flake8 uses RawConfigParser from the standard library to parse its configuration files, and therefore expects them to have the INI format.

This library hooks into Flake8's plug-in mechanism to load the configuration from pyproject.toml instead, if it finds such a file in the current folder (working directory). It then creates a RawConfigParser instance, converting from the TOML input format, and passes it on to Flake8 while discarding configuration options that would otherwise be sourced from elsewhere.

As of Python 3.11, a TOML parser is part of the standard library (PEP 680). On older Python installations, we rely on Tomli.

A few very simple integration tests round out the package, making sure that any one of the possible configuration files are in fact accepted when pyproject.toml isn't found.

Pre-commit hook

Use the pre-commit hook for Flake8 itself and make sure this package here is installed as well. The pre-commit configuration, in .pre-commit-config.yaml, would then look like so:

- repo: https://github.com/pycqa/flake8
  rev: 6.0.0
  hooks:
    - id: flake8
      additional_dependencies: [Flake8-pyproject]

Change the revision to whatever is the latest release version of Flake8.

release coverage

flake8-pyproject's People

Contributors

avylove avatar ekalosak avatar jamestwebber avatar john-hen avatar justcallmelarry avatar mataha avatar mattbwilmes avatar vcalvert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

flake8-pyproject's Issues

Support `flake8:local-plugins`

Flake8 supports a second section in the setup.cfg that specifies local plugins. I'm not able to get that to work with this tool, and I took a look at the source and it looks like it only checks the normal flake8 section. Any chance support for this other section could be added?
Also just wanted to say thanks and that this project is amazing. πŸ‘

Flake8 `--config` option not supported

Firstly, this is an awesome project, I have been waiting for this for years! So before I begin, keep it up, you're helping make the Python community amazing!


When I go to the directory with pyproject.toml and invoke flake8, everything works fine. πŸ‘

However, when I try flake8 --config pyproject.toml, I get the following:

Traceback (most recent call last):
  File "/path/to/venv/bin/flake8", line 8, in <module>
    sys.exit(main())
  File "/path/to/venv/lib/python3.10/site-packages/flake8/main/cli.py", line 22, in main
    app.run(argv)
  File "/path/to/venv/lib/python3.10/site-packages/flake8/main/application.py", line 336, in run
    self._run(argv)
  File "/path/to/venv/lib/python3.10/site-packages/flake8/main/application.py", line 324, in _run
    self.initialize(argv)
  File "/path/to/venv/lib/python3.10/site-packages/flake8/main/application.py", line 296, in initialize
    cfg, cfg_dir = config.load_config(
  File "/path/to/venv/lib/python3.10/site-packages/flake8/options/config.py", line 80, in load_config
    if not cfg.read(config, encoding="UTF-8"):
  File "/path/to/.pyenv/versions/3.10.3/lib/python3.10/configparser.py", line 698, in read
    self._read(fp, filename)
  File "/path/to/.pyenv/versions/3.10.3/lib/python3.10/configparser.py", line 1117, in _read
    raise e
configparser.ParsingError: Source contains parsing errors: 'pyproject.toml'
	[line 60]: ']\n'
	[line 65]: ']\n'
	[line 102]: ']\n'
	[line 111]: ']\n'
	[line 168]: ']\n'

All of the lines reported (60, 65, 102, 111, 168) correspond with the ending ] from a list. Also, some of them are for configs for other tools (e.g. pylint).

For example, line 60 and 65 are shown below (I added a comment to make it clear):

[tool.pylint.messages_control]

# Disable the message, report, category or checker with the given id(s).
disable = [
    "missing-docstring",  # Let pep257 take care of docstrings
    "empty-docstring",  # Let pep257 take care of docstrings
    "too-few-public-methods",  # Don't care for this level of linting
    "too-many-ancestors",  # XYZ makes heavy use of inheritance
    "fixme",  # codetags are useful
    "too-many-arguments",  # Don't care to enforce this
    "wrong-import-order",  # Rely on isort for this
    "ungrouped-imports",  # Rely on isort for this
    "unused-wildcard-import",  # Wildcard imports are convenient
    "wildcard-import",  # Wildcard imports are convenient
    "unsubscriptable-object",  # Buggy, SEE: https://github.com/PyCQA/pylint/issues/3637
    "unexpected-keyword-arg",  # pylint doesn't understand XYZ
    "logging-fstring-interpolation",  # f-strings are convenient
    "invalid-name",  # Align with equations (e.g. short names, upper case)
]  # Line 60

# Enable the message, report, category or checker with the given id(s).
enable = [
    "useless-suppression",  # Print unused `pylint: disable` comments
]  # Line 65

Is there anything you can do about this? I use flake8 --config in CI, so currently I can't use Flake8-pyproject until this is fixed.

Versions

I am using Python 3.10.3 and the following:

flake8==5.0.4
Flake8-pyproject==1.1.0.post0

the name case

hi bro ! thank you so much for your contribution, your project is great. would you kindly consider renaming your repo to lowercase, so we, the pedantic perfectionists, can praise you even more ! you know, the big F hurts our eyes. thank you !

parse_args() used in hook.py

In hook.py, there is a call to parse_args(), which assumes that flake8 is being called from the command line:

arguments = option_manager.parser.parse_args()

However, if someone calls flake8.main.cli.main() using a separate script that is called with command line options other than those used by flake8, this will trigger an error.

Preferred Approach

It would be better to accept the argv passed into flake8.main.cli.main() and call arguments = parse_known_args(argv) before checking for arguments.toml_config

Backup Approach

A less ideal solution that would be a quicker temporary fix would be to use parse_known_args() to call arguments, _ = parse_known_args() before checking for arguments.toml_config

Unrecognized argument toml config

Hi,

After install your module with poetry I can't use my custom path config,

I get flake8: error: unrecognized arguments: --toml-config

when I launch poetry run flake8 --toml-config src/pyproject.toml

The poetry show show me that all is installed

flake8              6.1.0
pyproject-flake8    6.1.0

Thanks

Running via Flake8 pre-commit hook

Not sure if this is just user error, but thought I would mention it. I believe I had this working using the normal flake8 pre-commit hook, but it seems like a recent update somewhere changed things. When I tried to commit, it ran but ignored my config in pyproject.toml.

I am able to use this with the Flake8-pyproject hook in your README, so I'm good for now. But since the docs say that it should work with the normal hook I wanted to bring it to your attention.

Thanks for the package! πŸ‘

Override `flake8` script?

Hello, thanks for the project, it looks good! I was wondering if this app could take over the flake8 script. This way everything with work in integration with pyproject.toml
Currently one has to update scripts to use flake8p over flake8, then update the IDE to use flake8p instead of flake8, and then document and expect all other team members to do the same on their IDEs (which if they use the same as me I can give support, but if they use another it's harder).

Let me know what you think of this feature, I can also submit a PR.

Regards!

Remove upper pinning for flake 8 and add test for flake8 main

When flake8 moved to 6.0, pip decided version 0.9.0 was the only one that would meet the requirements since the version was pinned in 0.9.1. Since the configuration loading is different in that version, the config was ignored. You fixed this a couple days later so thanks for addressing it quickly. However, removing the pinning of flake8 < 7 would be better because when flake8 7 comes out it's more likely to work with the latest version than with 0.9.0, which is what will get pulled otherwise.

It would also be good to add a CI test that runs against flake8 main. The test could be non-critical with the continue-on-error option or not. This way there would be heads-up about any breaking behavior coming down the line.

Error with `flake8-mock`

Hello
i cant interpret this error:

There was a critical error during execution of Flake8:
plugin code for `flake8-mock[flake8_mock]` does not match ^[A-Z]{1,3}[0-9]{0,3}$

Flake8-pyproject doesn't work with flake8 `6.1.0`

flake8 version 6.1.0 was recently released on conda forge but unfortunately seems to be incompatible with flake8-pyproject version 1.2.3. With version 6.1.0 my configuration in my pyproject.toml are ignored. Downgrading to 6.0.0 resolves this issue.

Does not support Flake8 5+

Hi there,

Flake8 5.0.0 refactored the options config parsing, and removed the class that is currently mutated, preventing this method from working any more.

`flake8p` is only aware of `pyproject.toml` in the current folder

As I've noticed today, when running flake8p in a folder below the main project folder, it does not pull the configuration from pyproject.toml (then in a folder or more above). This used to work before, so is due to the refactoring in Flake8 version 5 and the new "hook" tailored to that. Mea culpa. This should be fixed, somehow.

As of now, flake8p only works as expected when run from the folder that contains pyproject.toml. The existing test cases all assume that simple folder structure, so a test case should be added that tests running from a subfolder.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.