-1

I'm using VSCode and have configured black and flake8 for dev container. My settings.json looks like this:

{
    "python.linting.enabled": true, 
    "python.linting.lintOnSave": true, 
    "python.linting.mypyEnabled": true, 
    "python.linting.pylintEnabled": false, 
    "python.linting.flake8Enabled": true, 
    "python.linting.flake8Args": [ 
        "--max-line-length", 
        "88", 
        "--ignore=E203,W503,W504" 
    ],
    "python.formatting.provider": "black", 
    "editor.formatOnSave": true, 
    "editor.codeActionsOnSave": {
        "source.organizeImports": true 
    },
    "python.formatting.blackArgs": [ 
        "--line-length", 
        "88", 
        "--experimental-string-processing" 
    ],
    "python.testing.unittestEnabled": false, 
    "python.testing.pytestEnabled": true, 
    "autoDocstring.docstringFormat": "google"
}

Since black's default maximum line length is 88, I've explicitly set the flake8 and black configuration to 88 to avoid conflicts. However, when I have a single string exceeding 89 characters, flake8 warns with an E501 error ("line too long"), but black doesn't automatically format it. Even if I manually fix the error reported by flake8 to bring it below 88 characters, running "format document" with black reverts the string to its original length.

Here's an example:

from modeling.estimation.performance_analysis.monte_carlo.least_squares.error_grids import BaseGrid

I encountered an E501 error from flake8 and fixed it manually:

from modeling.estimation.performance_analysis.monte_carlo.least_squares.error_grids import ( \
    BaseGrid,
)

However, upon running "format document" with black, the string reverts to the original length:

from modeling.estimation.performance_analysis.monte_carlo.least_squares.error_grids import BaseGrid

In a previously posted question, there was a suggestion to use # fmt: off/on as a workaround.

Is there a way to enable the line length limits of both black and flake8 as intended without using # fmt: off/on?

aykhr
  • 1
  • 1
  • imo just turn off flake8's E501 -- if you have black managing your formatting you don't need to care about whether black successfully hits its target line length – anthony sottile Aug 14 '23 at 22:06
  • @anthonysottile Thank you for your comment. Based on your advice, I went ahead and adjusted my configuration to ignore `E501` errors in flake8. This change successfully made the `E501: line too long` error not shown. However, I encountered a new issue where `black` doesn't automatically insert a backslash (`\`) for line breaks in import statements. – aykhr Aug 15 '23 at 03:18
  • I've been following the discussion in [GitHub Issue #1802](https://github.com/psf/black/issues/1802), and one of the comments mentioned, "When there are no spaces in the string, black doesn't work and doesn't break the string in multiple lines." My concern specifically relates to import statements where spaces cannot be inserted. Given this limitation, I'm wondering if there's a configuration or setting that allows `black` to automatically insert the necessary backslashes and format the import statement into multiple lines, even in situations where spaces can't be introduced. – aykhr Aug 15 '23 at 03:19
  • black intentionally has no configuration. the whole point is to let the formatter do its job and not care about what it does – anthony sottile Aug 15 '23 at 14:14
  • Thank you for your response. As a potential solution, I'm considering configuring the line length limits for both black and flake8 based on the import statement with the highest character count. – aykhr Aug 15 '23 at 16:49
  • Are there any updates? This doesn't happen on my machine. – JialeDu Aug 23 '23 at 05:47

0 Answers0