Go to file
Shantanu 23dfc5b2c3
Fix ignoring input files for symlink reasons (#4222)
This relates to #4015, #4161 and the behaviour of os.getcwd()

Black is a big user of pathlib and as such loves doing `.resolve()`,
since for a long time it was the only good way of getting an absolute
path in pathlib. However, this has two problems:

The first minor problem is performance, e.g. in #3751 I (safely) got rid
of a bunch of `.resolve()` which made Black 40% faster on cached runs.

The second more important problem is that always resolving symlinks
results in unintuitive exclusion behaviour. For instance, a gitignored
symlink should never alter formatting of your actual code. This kind of
thing was reported by users a few times.

In #3846, I improved the exclusion rule logic for symlinks in
`gen_python_files` and everything was good.

But `gen_python_files` isn't enough, there's also `get_sources`, which
handles user specified paths directly (instead of files Black
discovers). So in #4015, I made a very similar change to #3846 for
`get_sources`, and this is where some problems began.

The core issue was the line:
```
root_relative_path = path.absolute().relative_to(root).as_posix()
```
The first issue is that despite root being computed from user inputs, we
call `.resolve()` while computing it (likely unecessarily). Which means
that `path` may not actually be relative to `root`. So I started off
this PR trying to fix that, when I ran into the second issue. Which is
that `os.getcwd()` (as called by `os.path.abspath` or `Path.absolute` or
`Path.cwd`) also often resolves symlinks!
```
>>> import os
>>> os.environ.get("PWD")
'/Users/shantanu/dev/black/symlink/bug'
>>> os.getcwd()
'/Users/shantanu/dev/black/actual/bug'
```
This also meant that the breakage often would not show up when input
relative paths.

This doesn't affect `gen_python_files` / #3846 because things are always
absolute and known to be relative to `root`.

Anyway, it looks like #4161 fixed the crash by just swallowing the error
and ignoring the file. Instead, we should just try to compute the actual
relative path. I think this PR should be quite safe, but we could also
consider reverting some of the previous changes; the associated issues
weren't too popular.

At the same time, I think there's still behaviour that can be improved
and I kind of want to make larger changes, but maybe I'll save that for
if we do something like #3952

Hopefully fixes #4205, fixes #4209, actual fix for #4077
2024-02-12 00:04:09 -08:00
.github Bump pypa/cibuildwheel from 2.16.4 to 2.16.5 (#4212) 2024-02-05 01:09:21 -08:00
action Remove ENV_PATH on Black action completion (#3759) 2023-08-08 11:12:05 -07:00
autoload Remove outdated mentions of runtime support of Python 3.7 (#3896) 2023-09-18 10:35:07 -07:00
docs Remove redundant parentheses in case statement if guards (#4214) 2024-02-07 06:55:02 -08:00
gallery Check self format for the whole repo (#3750) 2023-06-25 06:53:26 -07:00
plugin Vim plugin: allow using system black rather than virtualenv (#3309) 2022-10-27 18:55:33 -05:00
profiling Move profiling data out of tests/data 2018-06-09 15:48:41 -07:00
scripts fix: minor issue with schemastore part of script (#4195) 2024-02-01 09:25:38 -08:00
src Fix ignoring input files for symlink reasons (#4222) 2024-02-12 00:04:09 -08:00
tests Fix ignoring input files for symlink reasons (#4222) 2024-02-12 00:04:09 -08:00
.flake8 docs: Refactor pycodestyle/Flake8 compatibility docs (#4194) 2024-02-01 22:00:41 -08:00
.git_archival.txt Use GH action version when version argument not specified (#3543) 2023-03-27 18:40:27 -07:00
.gitattributes Use GH action version when version argument not specified (#3543) 2023-03-27 18:40:27 -07:00
.gitignore chore: ignore node_modules (produced by a pre-commit check) (#4184) 2024-01-27 20:24:36 -08:00
.pre-commit-config.yaml feat: add schema and validate-pyproject support (#4181) 2024-01-29 07:56:48 -08:00
.pre-commit-hooks.yaml Revert "confine pre-commit to stages (#3940)" (#4137) 2024-01-01 16:55:25 -08:00
.prettierrc.yaml Make Prettier preserve line ending type (#2244) 2021-05-17 14:38:43 -04:00
.readthedocs.yaml Bump RTD Python version from 3.8 to 3.11 (#3868) 2023-09-09 09:08:28 -07:00
action.yml Fix CI failing (#3957) 2023-10-23 10:37:14 -07:00
AUTHORS.md Maintainers += Shantanu Jain (hauntsaninja) (#3792) 2023-07-16 18:16:12 -07:00
CHANGES.md Fix ignoring input files for symlink reasons (#4222) 2024-02-12 00:04:09 -08:00
CITATION.cff Fix typo in CITATION.cff (#3779) 2023-07-10 19:38:01 -07:00
CONTRIBUTING.md Reorganize docs v2 (GH-2174) 2021-05-08 15:17:38 -04:00
Dockerfile [docker] Build with 3.12 image (#4055) 2023-11-18 18:09:47 -08:00
LICENSE Initial commit 2018-03-14 12:55:32 -07:00
pyproject.toml feat: add schema and validate-pyproject support (#4181) 2024-01-29 07:56:48 -08:00
README.md Update README.md (#3997) 2023-10-29 10:28:33 -07:00
SECURITY.md Add SECURITY.md (#3612) 2023-03-18 10:41:48 -07:00
test_requirements.txt Unpin pytest-xdist (#3772) 2023-07-10 11:49:40 -07:00
tox.ini feat: add schema and validate-pyproject support (#4181) 2024-01-29 07:56:48 -08:00

Black Logo

The Uncompromising Code Formatter

Actions Status Documentation Status Coverage Status License: MIT PyPI Downloads conda-forge Code style: black

“Any color you like.”

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Blackened code looks the same regardless of the project you're reading. Formatting becomes transparent after a while and you can focus on the content instead.

Black makes code review faster by producing the smallest diffs possible.

Try it out now using the Black Playground. Watch the PyCon 2019 talk to learn more.


Read the documentation on ReadTheDocs!


Installation and usage

Installation

Black can be installed by running pip install black. It requires Python 3.8+ to run. If you want to format Jupyter Notebooks, install with pip install "black[jupyter]".

If you can't wait for the latest hotness and want to install from GitHub, use:

pip install git+https://github.com/psf/black

Usage

To get started right away with sensible defaults:

black {source_file_or_directory}

You can run Black as a package if running it as a script doesn't work:

python -m black {source_file_or_directory}

Further information can be found in our docs:

Black is already successfully used by many projects, small and big. Black has a comprehensive test suite, with efficient parallel tests, and our own auto formatting and parallel Continuous Integration runner. Now that we have become stable, you should not expect large formatting changes in the future. Stylistic changes will mostly be responses to bug reports and support for new Python syntax. For more information please refer to The Black Code Style.

Also, as a safety measure which slows down processing, Black will check that the reformatted code still produces a valid AST that is effectively equivalent to the original (see the Pragmatism section for details). If you're feeling confident, use --fast.

The Black code style

Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in place. Style configuration options are deliberately limited and rarely added. It doesn't take previous formatting into account (see Pragmatism for exceptions).

Our documentation covers the current Black code style, but planned changes to it are also documented. They're both worth taking a look at:

Changes to the Black code style are bound by the Stability Policy:

Please refer to this document before submitting an issue. What seems like a bug might be intended behaviour.

Pragmatism

Early versions of Black used to be absolutist in some respects. They took after its initial author. This was fine at the time as it made the implementation simpler and there were not many users anyway. Not many edge cases were reported. As a mature tool, Black does make some exceptions to rules it otherwise holds.

Please refer to this document before submitting an issue just like with the document above. What seems like a bug might be intended behaviour.

Configuration

Black is able to read project-specific default values for its command line options from a pyproject.toml file. This is especially useful for specifying custom --include and --exclude/--force-exclude/--extend-exclude patterns for your project.

You can find more details in our documentation:

And if you're looking for more general configuration documentation:

Pro-tip: If you're asking yourself "Do I need to configure anything?" the answer is "No". Black is all about sensible defaults. Applying those defaults will have your code in compliance with many other Black formatted projects.

Used by

The following notable open-source projects trust Black with enforcing a consistent code style: pytest, tox, Pyramid, Django, Django Channels, Hypothesis, attrs, SQLAlchemy, Poetry, PyPA applications (Warehouse, Bandersnatch, Pipenv, virtualenv), pandas, Pillow, Twisted, LocalStack, every Datadog Agent Integration, Home Assistant, Zulip, Kedro, OpenOA, FLORIS, ORBIT, WOMBAT, and many more.

The following organizations use Black: Facebook, Dropbox, KeepTruckin, Lyft, Mozilla, Quora, Duolingo, QuantumBlack, Tesla, Archer Aviation.

Are we missing anyone? Let us know.

Testimonials

Mike Bayer, author of SQLAlchemy:

I can't think of any single tool in my entire programming career that has given me a bigger productivity increase by its introduction. I can now do refactorings in about 1% of the keystrokes that it would have taken me previously when we had no way for code to format itself.

Dusty Phillips, writer:

Black is opinionated so you don't have to be.

Hynek Schlawack, creator of attrs, core developer of Twisted and CPython:

An auto-formatter that doesn't suck is all I want for Xmas!

Carl Meyer, Django core developer:

At least the name is good.

Kenneth Reitz, creator of requests and pipenv:

This vastly improves the formatting of our code. Thanks a ton!

Show your style

Use the badge in your project's README.md:

[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Using the badge in README.rst:

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
    :target: https://github.com/psf/black

Looks like this: Code style: black

License

MIT

Contributing

Welcome! Happy to see you willing to make the project better. You can get started by reading this:

You can also take a look at the rest of the contributing docs or talk with the developers:

Change log

The log has become rather long. It moved to its own file.

See CHANGES.

Authors

The author list is quite long nowadays, so it lives in its own file.

See AUTHORS.md

Code of Conduct

Everyone participating in the Black project, and in particular in the issue tracker, pull requests, and social media activity, is expected to treat other people with respect and more generally to follow the guidelines articulated in the Python Community Code of Conduct.

At the same time, humor is encouraged. In fact, basic familiarity with Monty Python's Flying Circus is expected. We are not savages.

And if you really need to slap somebody, do it with a fish while dancing.