Giter VIP home page Giter VIP logo

pyproject-flake8's Introduction

pyproject-flake8 (pflake8)

A monkey patching wrapper to connect flake8 with pyproject.toml configuration.

Update concerning versioning:

pyproject-flake8 so far came without explicit pinning of a flake8 version. Since a recent update broke compatibility โ€“ not unexpectedly โ€“ the issue arose, how to handle incompatible versions of flake8 and pyproject-flake8. Since there are good reasons for and against version pinning, this project now tries to follow a mix of both: Release versions will follow and pin identical flake8 versions, alpha versions (specific to pyproject-flake8) will pin to the similar non-alpha version of flake8, or later. That way, users of pyproject-flake8 can decide, whether they want a fixed version known to work, or a minimum version, getting later versions, at the risk of future breakage.

Versions 0.0.1x are pyproject-flake8 internal versions and do not correspond to flake8. Furthermore, flake8 3.8.0 was chosen as the first flake8 version to mirror this way.

tl;dr:

# e.g., suggested installation / dependency ... depending on flake8==5.0.4

# for Python 3.8+
pip install pyproject-flake8==5.0.4

# for Python 3.6+
pip install pyproject-flake8==5.0.4.post1

Rationale

flake8 is one of the most popular Python linters, pyproject.toml has become the standard for Python project metadata.

More and more tools are able to utilize a shared pyproject.toml, alleviating the need for many individual configuration files cluttering a project repository.

Since excellent flake8 is not aimed to support pyproject.toml, this wrapper script tries to fix the situation.

Installation

From github

pip install .

From PyPI

pip install pyproject-flake8

Building packages

Use your favorite PEP517 compliant builder, e.g.:

# install first via: pip install build
python -m build
# packages will reside in dist/

Usage

Call pflake8 instead of flake8.

Configuration goes into the tool.flake8 section of pyproject.toml:

[tool.flake8]
max-line-length = 88
extend-ignore = ["E203"]
max-complexity = 10

See also

Two other projects aim to address the same problem:

Both seem to try to do a lot more than just getting pyproject.toml support. pyproject-flake8 tries to stay minimal while solving its task.

flake8-pyproject adds only pyproject.toml support, and does this as a Flake8 plugin, allowing the original flake8 command to work (rather than using pflake8).

Caveat

This script monkey-patches flake8 and the configparser library of Python, therefore loading it as a module may have unforeseen consequences. Alpha quality. Use at your own risk. It will likely break if either Python or flake8 restructure their code significantly. No guarantees for stability between versions.

License

Unlicense

pyproject-flake8's People

Contributors

agronholm avatar csachs avatar davidwaroquiers avatar fliiiix avatar heckad avatar i-aki-y avatar johnthagen avatar kianmeng avatar pietropasotti avatar q0w avatar sasanjac 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  avatar  avatar

pyproject-flake8's Issues

Use TOML lists rather than comma separated strings

For a tag such as extend-ignore, a more natural TOML API for the user would be that instead of:

extend-ignore = "E203,E204"

Would be:

extend-ignore = ["E203", "E204"]

Behind the scenes, pyproject-flake8 could add commas as needed as it calls out to flake8, but I think it would be cleanest if this was hidden from the user.

This is also more closely to what an actual TOML implementation would look like for flake8.

Not compatible with flake8 5.0

  File "/Users/neil/Library/Caches/pypoetry/virtualenvs/generators-0uytuYoH-py3.10/lib/python3.10/site-packages/pflake8/__init__.py", line 54, in <module>
    class ModifiedConfigFileFinder(flake8.options.config.ConfigFileFinder):
AttributeError: module 'flake8.options.config' has no attribute 'ConfigFileFinder'

Document "integration" with vscode flake8 plugin

Perhaps this should have been obvious, but you can tell the vscode flake8 plugin to call pflake8 by placing this in .vscode/settings.json (presuming that poetry creates venv's called .venv in your project root as it does for me).

{
    "flake8.importStrategy": "fromEnvironment",
    "flake8.path": [
        ".venv/bin/pflake8"
    ],
}

If you don't feel like putting this in the readme, even just having this closed issue lying around might be enough to make this solution searchable.

pflake8 6.0.0 overrides some pyproject.toml parameters with default values when jobs>1

Each worker of flake8 ignores pyproject.toml in --jobs > 1 executions.

Steps to reproduce:

I have to files (file1.py, file2.py) with same content:

print("long line =============================================================================================")

It violates 2 rules: W292 (newline in EOF) and E501 (max-line-length)

In my pyproject.toml file I have following flake8 configuration:

[tool.flake8]
max-line-length = 120
ignore = "W292"

Running pflake8 -vv --jobs=2 on version 5.0.4. works fine. However same command on version 6.0.0 raises E501 with default 79 char parameter.

.\file1.py:1:80: E501 line too long (112 > 79 characters)
.\file2.py:1:80: E501 line too long (112 > 79 characters)

Full execution logs are attached:
5_0_4.txt
6_0_0.txt

On line 92 of 6_0_0.txt I can see that parameter from toml file is used. But then each worker reads config again but this time not reading toml file configuration.

AttributeError in _read

pflake8 my_file.py (ran by tox) fails with:

[...]
  File "/my-proj/.tox/lint/lib/python3.8/site-packages/flake8/options/config.py", line 66, in _read_config
    found_files.extend(config.read(filename))
  File "/usr/lib/python3.8/configparser.py", line 697, in read
    self._read(fp, filename)
  File "/my-proj/.tox/lint/lib/python3.8/site-packages/pflake8/__init__.py", line 30, in _read
    for key, value in section_to_copy.items():
AttributeError: 'list' object has no attribute 'items'

My pyproject.toml file contains only:

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

It looks to me like an obvious tiny bug; the default value of toml_config.get('tool') should not be [] but {}

Issue with command line arguments

Not sure if this is the right place to put this, although limiting the version of pyproject-flake8<6.0.0 did resolve the issue. There seems to be a problem with the command line argument type beying passed as an str rather than a function which is resulting in the error below. I put a print statement with the arguments being passed just before this line: .tox/lint/lib/python3.10/site-packages/flake8/options/manager.py", line 274, in add_option

['--copyright-min-file-size']
{'action': 'store', 'default': 0, 'type': 'int', 'help': 'Minimum number of characters in a file before requiring a copyright notice.'}
Traceback (most recent call last):
  File ".tox/lint/bin/pflake8", line 8, in <module>
    sys.exit(main())
  File ".tox/lint/lib/python3.10/site-packages/flake8/main/cli.py", line 23, in main
    app.run(argv)
  File ".tox/lint/lib/python3.10/site-packages/flake8/main/application.py", line 198, in run
    self._run(argv)
  File ".tox/lint/lib/python3.10/site-packages/flake8/main/application.py", line 186, in _run
    self.initialize(argv)
  File ".tox/lint/lib/python3.10/site-packages/flake8/main/application.py", line 165, in initialize
    self.plugins, self.options = parse_args(argv)
  File ".tox/lint/lib/python3.10/site-packages/flake8/options/parse_args.py", line 51, in parse_args
    option_manager.register_plugins(plugins)
  File ".tox/lint/lib/python3.10/site-packages/flake8/options/manager.py", line 250, in register_plugins
    add_options(self)
  File ".tox/lint/lib/python3.10/site-packages/flake8_copyright.py", line 56, in add_options
    register_opt(
  File ".tox/lint/lib/python3.10/site-packages/flake8_copyright.py", line 30, in register_opt
    parser.add_option(*args, **kwargs)
  File ".tox/lint/lib/python3.10/site-packages/flake8/options/manager.py", line 274, in add_option
    self._current_group.add_argument(*option_args, **option_kwargs)
  File "/usr/lib/python3.10/argparse.py", line 1440, in add_argument
    raise ValueError('%r is not callable' % (type_func,))
ValueError: 'int' is not callable

Pre-commit hook and pyproject-flake8

Hi ๐Ÿ™‚

Thanks for this nice addition to the flake8 ecosystem!
I'm currently working on a project relying on pre-commit-config.yaml to insure consistency in the code style.

Since the default commit hook does not include your package, I tried using the method described here:

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

My config for flake8 in pyproject.toml is:

[tool.flake8]
ignore = "E203, E266, E501, W503, F403, F401"
exclude = ".git, .mypy_cache, .pytest_cache, build, dist"
max-line-length = 89
max-complexity = 18
select = "B,C,E,F,W,T4,B9"

Yet, when running the commit hook, I get:

E501 line too long (87 > 79 characters)

This means that the configuration is not parsed properly from the pyproject.toml.
Do you have any idea to solve this?

Thanks ๐Ÿ™‚

Parsing Issue when using black's exclude format

I will be taking a crack at a PR on this in a little bit, just want to document the issue, as this is a brand new project and I don't expect support with an alpha designation.

Thank you for getting the ball rolling on a minimal wrapper for pyproject support was just thinking of doing something the other day.

ISSUE:

TOML parser seems to fail on some of the more verbose syntax that the black tool has introduced into its code block

example from their documents https://black.readthedocs.io/en/stable/pyproject_toml.html#configuration-format

[tool.black]
line-length = 120
target-version = ['py38']
include = '\.pyi?$'
exclude = '''
(
  /(
      \.eggs         # exclude a few common directories in the
    | \.git          # root of the project
    | \.hg
    | \.mypy_cache
    | \.tox
    | \.venv
    | _build
    | buck-out
    | build
    | dist
  )
)
'''

this results in a parsing error

Unable to parse config file: Source contains parsing errors: '/PATH/TO/PROJECT_ROOT/pyproject.toml'
	[line 50]: '(\n'
	[line 64]: ')\n'
	[line 65]: "'''"

pre-commit args aren't supported

Hi,

First of all, thank you for creating this really handy tool.
I tried to pass some extra arguments to pflake8 command using args key https://pre-commit.com/index.html#config-args, but it it looks like this isn't supported.
As a workaround I had to add those extra arguments to the entry key.

entry: pipenv run pflake8 --verbose

It'd be great if pre-commit args were supported.

Thanks

Issue with configparser

Running python 3.12 I'm seeing the following error at runtime:

AttributeError: module 'configparser' has no attribute 'SafeConfigParser'. Did you mean: 'RawConfigParser'

Per the Python 3.11 release notes, SafeConfigParser was slated for removal in 3.12.

pflake8 doesn't propagate the flake8 exit code when used as a runnable module

If I run

python -m pflake8 nonexisting.py 2>&1 > /dev/null
echo $?

the exit code is 0. (This is tested on macOS, zsh).

For comparison, the following variants all return 1 as exit code

pflake8 nonexisting.py 2>&1 > /dev/null
python -m flake8 nonexisting.py 2>&1 > /dev/null
flake8 nonexisting.py 2>&1 > /dev/null

I would definitely expect python -m pflake8 nonexisting.py to also have a non-zero exit code (exit code of 1, in fact).

Real booleans not supported in pyproject.toml

Using a real boolean in pyproject.toml for flake8 config breaks pflake8:

[tool.flake8]
count = true
Traceback (most recent call last):
[...]
  File "/usr/local/Cellar/[email protected]/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/configparser.py", line 1165, in _convert_to_boolean
    if value.lower() not in self.BOOLEAN_STATES:
AttributeError: 'bool' object has no attribute 'lower'

As a workaround booleans have to be written as string:

[tool.flake8]
count = "true"

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.