Search code examples
pythonpython-3.xpython-black

Can I get the black python code format tool to recognize what directory my code is in


I am trying to integrate the black python code formatting tool into my workflow. As a test I have created a directory with the following structure.

hello
|_ pyproject.toml
|_ hello
    |_main.py

The pyproject.toml file has the following information in it.

[tool.poetry]
name = "hello"
version = "0.1.0"
description = ""
authors = ["my Name <name@gmail.com>"]
readme = "README.rst"

[tool.poetry.dependencies]
python = "^3.10"


[tool.poetry.group.dev.dependencies]
pytest = "^7.2.1"
flake8 = "^6.0.0"
mypy = "^1.0.0"
black = "^23.1.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

classifiers = [
    "Development Status :: 4 - Beta",
    "Programming Language :: Python :: 3",
    "Programming Language :: Python :: 3.10",
    "License :: OSI Approved :: MIT License",
    "Operating System :: MacOS",
    "Operating System :: POSIX :: Linux",
]

[tool.black]
line-length = 90
target-version = ['py38', 'py39', 'py310']
include = ['\.pyi?$', 'hello']
exclude = '''
/(
    \.eggs
  | \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
  # The following are specific to Black, you probably don't want those.
  | blib2to3
  | tests/data
  | profiling
)/
'''

As you can see, I include the name of my source code directory, hello in the include line. From the uppermost hello durectory, if I type black hello it will look into the lowermost hello directory and format any code in that directory. If I cd to the lowermost hello directory and type black or black main.py it will format the main.py code. However, is there a way to use the pyproject.toml file to tell black where my source code is, such that from the upper most hello directory I can just type black and it will look into the lowermost hello directory without me explicitly pointing it there from command line.

Presently when I type black from the uppermost hello directory I get the message Usage of black [OPTIONS] SRC ... One of 'SRC' or 'code' is required


Solution

  • --include, --exclude and --force-exclude options are only applied during automatic file discovery, ie. when Black traverses the filesystem for files to format. This happens any time you pass it a folder. However, as you've discovered yourself, Black still needs at least one path to run, whether that's a file or folder.

    There is this issue on the GitHub issue tracker about allowing SRC to be specified in pyproject.toml. The general sentiment is it will not be supported for usability and safety reasons (TL;DR: let's not make black a footgun).

    The point of those flags is so you can run black . from the root of your project and Black will discover the right files to format (and won't format anything you don't want it to format).

    You can simply remove hello from the include configuration and Black will do the right thing (try adding the --verbose flag to see what how it's discovering files to format). Also, I see that you've taken the configuration used by Black itself to format itself. For the time being, you don't need more than this:

    [tool.black]
    line-length = 90
    target-version = ['py310', 'py311']
    

    You can always add more options / tweak it if you need more control.