How to change default line length for black or flake8 in a poetry virtual environment?
Asked Answered
C

6

6

I have loaded black and flake8 into a poetry virtual environment. I'd like to change the default line length in black or flake8 so they agree. What is the best way to do this?

Chassis answered 19/3, 2020 at 1:35 Comment(1)
what have you tried so far? the configuration methodology for the tools doesn't change because you're using poetryHazlitt
K
5

Add a file .flake8 with the following :

[flake8]
max-line-length = 120

You can also do the same using a setup.cfg file.

Keep answered 30/3, 2022 at 9:10 Comment(0)
C
2

You can use pyproject-flake8 , a monkey patching wrapper to connect flake8 with pyproject.toml configuration.

or use FlakeHeaven : This project is a fork of FlakeHell.

FlakeHell and other forks of it such as flakehell/flakehell are no longer maintained and do not work with Flake8 4.0.x.

Cassidycassie answered 10/3, 2022 at 14:39 Comment(0)
T
2

flake8 doesnt natively support pyproject.toml file yet. You can install Flake8-pyproject to add this functionality.

pip install Flake8-pyproject

Then you can simply use the `pyproject.toml to change flake8 settings like below:

[tool.flake8]
max-line-length = 120
Tetracycline answered 23/2 at 6:23 Comment(0)
A
0

I think it good idea with

[tool.flake8]
max-line-length = 88
Acronym answered 7/2 at 13:9 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? (It looks a lot like the top-voted post!) If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Oxblood
W
0

Modern Python projects that use pyproject.toml can do the following:

# We hate arbitrary line lengths
[tool.black]
line-length = 999

No additional files needed.

pyproject.toml should be the default for all Poetry-based projects.

Wo answered 7/2 at 13:16 Comment(0)
F
-5

The short answer is add this to your pyproject.toml file (assuming you are using one since you are using poetry) and you should be good to go.

[flake8]
max-line-length = 88
extend-ignore = E203

This implies that the line length used by flake8 is set to 88, which is also the default used by black.

I would recommend that you take a look at the Line Length section of Black's README. The above snippet is taken from there. Black's authors also explain the rationale behind the choice of the default value. They also detail alternative options to make flake8 happy.

Futility answered 30/3, 2020 at 23:40 Comment(2)
A plain flake8 won't look into your pyproject.toml file. Put it into .flake8 instead.Frontage
or put it in setup.cfgTko

© 2022 - 2024 — McMap. All rights reserved.