black: Unexpected incompatibility with reorder-python-imports
Describe the bug
It is impossible to use Black 24.1.0 alongside reorder-python-imports. Using the example from #1872 , reorder-python-imports produces:
"""Module docstring."""
import typing
While formatting with black and default arguments produces the following:
"""Module docstring."""
import typing
This new behaviour makes the two tools incompatible. The author of reorder-python-imports suggests this is an issue with Black.
Is it possible to change Black to work better with reorder-python-imports please? I have used both tools together for a long time.
Environment
black, 24.1.0 (compiled: yes)
Python (CPython) 3.12.1
Also checked on the online formatter.
Additional context
A little bit of context from the Black changelog is that Black 24 and above will:
Enforce newline after module docstrings (#3932, #4028)
1872 was the original Black issue, and changes were implemented in 3932; 4028 doesn’t relate to imports.
About this issue
- Original URL
- State: open
- Created 5 months ago
- Reactions: 6
- Comments: 15 (3 by maintainers)
Commits related to this issue
- Use ruff in place of black and reorder-python-imports Unfortunately black and reorder-python-imports are no longer compatible between each other: https://github.com/asottile/reorder-python-imports/i... — committed to nicoddemus/pytest-mock by nicoddemus 5 months ago
- Use ruff in place of black and reorder-python-imports Unfortunately black and reorder-python-imports are no longer compatible between each other: https://github.com/asottile/reorder-python-imports/i... — committed to nicoddemus/execnet by nicoddemus 5 months ago
- Use ruff in place of black and reorder-python-imports Unfortunately black and reorder-python-imports are no longer compatible between each other: https://github.com/asottile/reorder-python-imports/i... — committed to nicoddemus/execnet by nicoddemus 5 months ago
- Replace reorder-python-imports by isort Unfortunately `black` and `reorder-python-imports` are no longer compatible between each other, and will not be in the foreseeable feature: https://github.com... — committed to ESSS/barril by nicoddemus 5 months ago
- Replace reorder-python-imports by isort Unfortunately `black` and `reorder-python-imports` are no longer compatible between each other, and will not be in the foreseeable feature: https://github.com... — committed to ESSS/oop-ext by nicoddemus 5 months ago
- Use ruff instead of black and reorder-python-imports (#239) Unfortunately black and reorder-python-imports are no longer compatible between each other: https://github.com/asottile/reorder-python-i... — committed to pytest-dev/execnet by nicoddemus 5 months ago
- Replace reorder-python-imports by isort due to black incompatibility Unfortunately black and reorder-python-imports are no longer compatible, and from the looks of it probably will not be compatible ... — committed to nicoddemus/pytest by nicoddemus 5 months ago
- chore: lock black version locking until further action due to https://github.com/psf/black/issues/4175 most likely course of action is to get rid of reorder-python-imports — committed to osuAkatsuki/bancho.py by NiceAesth 5 months ago
- Use isort to sort imports Switching because reorder-python-imports is incompatible with Black 24: https://github.com/asottile/reorder-python-imports/issues/366 / https://github.com/psf/black/issues/... — committed to adamchainz/django-upgrade by adamchainz 5 months ago
- Use isort to sort imports (#424) Switching because reorder-python-imports is incompatible with Black 24: https://github.com/asottile/reorder-python-imports/issues/366 / https://github.com/psf/black... — committed to adamchainz/django-upgrade by adamchainz 5 months ago
- Use isort to sort imports Switching because reorder-python-imports is incompatible with Black 24: https://github.com/asottile/reorder-python-imports/issues/366 / https://github.com/psf/black/issues/... — committed to adamchainz/apig-wsgi by adamchainz 5 months ago
- Use isort to sort imports (#481) Switching because reorder-python-imports is incompatible with Black 24: https://github.com/asottile/reorder-python-imports/issues/366 / https://github.com/psf/black... — committed to adamchainz/apig-wsgi by adamchainz 5 months ago
- Use isort to sort imports Switching because reorder-python-imports is incompatible with Black 24: https://github.com/asottile/reorder-python-imports/issues/366 / https://github.com/psf/black/issues/... — committed to adamchainz/blacken-docs by adamchainz 5 months ago
- Use isort to sort imports (#320) Switching because reorder-python-imports is incompatible with Black 24: https://github.com/asottile/reorder-python-imports/issues/366 / https://github.com/psf/black... — committed to adamchainz/blacken-docs by adamchainz 5 months ago
- chore: knock some sense into dependencies (#568) * chore: knock some sense into dependencies * chore: lock black version locking until further action due to https://github.com/psf/black/issues/... — committed to osuAkatsuki/bancho.py by NiceAesth 5 months ago
- chore: address deprecations caused by updating packages (#570) * chore: knock some sense into dependencies * chore: lock black version locking until further action due to https://github.com/psf... — committed to osuAkatsuki/bancho.py by NiceAesth 5 months ago
- ci: replace some Python checks with ruff Unfortunately, black v24+ and reorder-python-imports are incompatible: asottile/reorder-python-imports#367 asottile/reorder-python-imports#366 psf/black#4175... — committed to dupuy/reliabot by dupuy 5 months ago
- ci: replace many pre-commit checks/etc. with ruff (#38) Add many additional checks from flake8 and others incorporated into ruff. Unfortunately, black v24+ and reorder-python-imports are incompati... — committed to dupuy/reliabot by dupuy 4 months ago
- fix (cicd): roll back pre-commit hook versions - roll back necessary due to https://github.com/psf/black/issues/4175 — committed to AlthausKonstantin/pylero by AlthausKonstantin 4 months ago
- fix (cicd): roll back pre-commit hook versions - roll back necessary due to https://github.com/psf/black/issues/4175 — committed to AlthausKonstantin/pylero by AlthausKonstantin 4 months ago
maybe just use
isortwith something like:I’ve decided to migrate my projects to isort with the configuration:
isort is still sufficiently fast for me and it does have a goal of supporting Black.
The guy who maintains reorder-python-imports is very “my way or the highway”, I suggest you take that advice and use isort or the fork suggestion @adamchainz made. The place I work already dropped reorder-python-imports not because it didn’t work for us but specifically because of his attitude. If one of you does fork it please let me know, we would definitely consider switching to an import sorter that had the same sort of considered opination that Black has.
Might be worth asking Ruff to implement the one-import-per-line style as an option. (I searched their issue tracker but didn’t find an existing issue asking for it.)
It’s unfortunate that nobody reported this while the feature was in the preview style and in the alphas. Our guarantee is that we’ll keep the style the same for the rest of the year, so we can’t change it now.
I think it should be Black’s job to decide on whitespace outside the import block, not reorder-python-imports’s. Unfortunately that means I don’t have a good resolution for you.
🤷 This is a real shame, I really like the speed and low-configurability of reorder-python-imports. I do also think its rearrangement of non-import newlines is outside its wheelhouse.
FWIW, below is the small patch that makes reorder-python-imports compatible. Debating whether it’s worth forking to make an “always Black-compatible” version…
I suggest for this issue to be closed, with the solution being using
isort: https://github.com/psf/black/issues/4175#issuecomment-1936708759Perhaps it can be pinned for some time so others can find it quickly. 👍
The only reason to change that I am aware of is to make Black compatible with reorder-python-imports again. Is that a good reason or not? I’ll let you decide.
I agree both are opinionated tools with a specific style. I should perhaps make it explicit here that the style implemented by reorder-python-imports has “a single aim: reduce merge conflicts” with a documented rationale. A short example is:
The other functionality in reorder-python-imports is also available in other import sorting tools. I looked at isort and ruff; usort is new to me, thank you for mentioning it. I am not aware of a tool other than reorder-python-imports that implements this “reduce merge conflicts” import style.
isort is the only documented compatible import ordering tool for black. Does that imply anything about future compatibilty?
The ignore comments ( # fmt: skip after the closing “”" ) I mentioned above are OK for a short term workaround. Longer term, if neither tool is going to change, you’re right I’ll need to carefully reconsider my tool choices.
Edited to add: thanks again for dealing with this unfortunate incompatibility carefully. I appreciate your help.