-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format code with Black #843
base: master
Are you sure you want to change the base?
Conversation
My plan is to apply https://github.com/psf/black#migrating-your-code-style-without-ruining-git-blame if this is integrated. |
I strongly object to black's default choices; I take great care to ensure that my code is readable by humans, which requires heuristics that black has been unable to understand. I understand its use for projects with lots of contributors; however, let's be honest: the core of factory_boy is quite complex, and contributions far and few between — most of the work consists on contributions consists of guiding the contributor into understanding the core concepts and considering all use cases for their code; style has never in my experience been an issue with pull requests. So, that's a strong -1 for me. |
I agree that style is not a major hurdle to overcome for contributors, but a consistent coding style increases the source code readability. It also takes coding style out of the discussion entirely, which is the main benefit IMO. Black certainly cannot outperform a human on a given piece of code. However, to maintain a consistent coding-style across the project is particularly hard for humans, especially when multiple humans are contributing the code. I did a quick read-through of the source code after formatting it, to find cases where Black changed the layout in a way that decreased readability. A common example is when data structures have leading commas, Black spreads them on multiple lines: # Before Black
class MyFactory(DjangoModelFactory):
class Meta:
model = "myapp.Author"
django_get_or_create = ["foo", "bar", "baz", "other",]
# After Black
class MyFactory(DjangoModelFactory):
class Meta:
model = "myapp.Author"
django_get_or_create = [
"foo",
"bar",
"baz",
"other",
] When I spotted such changes, I fixed them (by removing the trailing comma, to let Black know the structure should not be expanded). If you find places that can be improved upon, I’m happy to take a look and discuss them. IMO, just because Black does not always outperform humans is not a reason to outright reject it. I don’t feel strongly about adding it in, but I believe it is valuable to the project:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After one hour of my life spent reviewing this code, this is my feedback:
- Switching from
'
to"
is noisy. I don't really care, but it might have been easier to review is split in a dedicated commit; - black provides some improvements in a few places, I agree with the pattern for multi-line interpolation (putting the
% (...)
on the second line instead of having the%
at the end of the first line); - It isn't able to put trailing commas in place, which is incompatible with my understanding (and factory_boy's flavour) of PEP8;
- It makes logging calls much nosier, whereas they should be kept compact in order to avoid breaking the reading flow of the program.
Unless we can address at least those two points (trailing commas, logging calls), I don't see the point of going through the hassle of migrating to black.
factory/builder.py
Outdated
if k in post_overrides | ||
}) | ||
post_declarations.update( | ||
{k: v for k, v in extra_maybenonpost.items() if k in post_overrides} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer this to be split across lines, so that the condition (if k in post_overrides
) stays easy to spot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change that would also solve this comment. #877
factory/builder.py
Outdated
if k not in post_overrides | ||
}) | ||
pre_declarations.update( | ||
{k: v for k, v in extra_maybenonpost.items() if k not in post_overrides} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change that would also solve this comment. #877
} | ||
), | ||
} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With all the added indent levels, I find the final structure much nosier.
@rbarrois I think much of the feedback could be resolved using "magic trailing commas":
The Black |
9e60d27
to
d7d33d5
Compare
Thanks for the feedback. I split the commit following the Jon’s suggestion #843 (comment).
A way to keep logging on a single line for most cases would be to increase the line length. That would also solve:
I’ll give that a try. |
…tions Avoid iterating twice over the extra_maybenonpost items. Clarify all items from extra_maybenonpost are split into two dicts.
Pushed a commit that sets the line length to 120. If we go that route, the |
Black is a very popular Python code formatter. It ensures a consistent coding style across the library. It is opinionated and not configurable, so that projects adopting Black are similar. The main advantage a code formatter is to avoid concerns about coding style. Many text editors have an integration for Black and can be configured to format on save. The isort profile has been set to "black" for compatibility. https://pycqa.github.io/isort/docs/configuration/black_compatibility/ https://github.com/psf/black Short multiline comprehensions were kept on multiple lines using # fmt: skip to improve the readability.
Black unifies strings to "".
Black will probably be moving out of beta during 2022. 🎉 |
Black is a very popular Python code formatter. It ensures a consistent
coding style across the library. It is opinionated and not configurable,
so that projects adopting Black are similar.
The main advantage a code formatter is to avoid concerns about coding
style. Many text editors have an integration for Black and can be
configured to format on save.
The isort profile has been set to "black" for compatibility.
https://pycqa.github.io/isort/docs/configuration/black_compatibility/
https://github.com/psf/black
TODOS
pyproject.toml
(later/earlier commit)pyproject.toml