-
Notifications
You must be signed in to change notification settings - Fork 421
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
Thoughts on Implementing black and isort in MetPy #1317
Comments
🤨 I definitely have misgivings about how black formats some code. Were you thinking of having this run by default as a git pre-commit hook? |
Yes, it is firstly that. After working with black for a little while, I've realized how much time I spend making linting pass when working on MetPy...not enough to be a complete hindrance to my work, but definitely enough to be annoying. Also, it removes the burden of deciding how to best make flake8 happy...or of reaching barely passable formatting and giving up (Exhibit 1: https://github.com/Unidata/MetPy/blob/v1.0.0rc1/src/metpy/xarray.py#L842-L868). However, there is also the argument of consistent formatting across code bases in the ecosystem, which I think is a laudable goal. There is something to be said about "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." Also, this is me preemptively getting the conversation started before something like @exporter.export
@preprocess_xarray
@check_units(
"[length]",
"[speed]",
"[speed]",
"[length]",
bottom="[length]",
storm_u="[speed]",
storm_v="[speed]",
)
def storm_relative_helicity(
height,
u,
v,
depth,
*,
bottom=0 * units.m,
storm_u=0 * units("m/s"),
storm_v=0 * units("m/s")
): slips into one of my PRs due to new habits. 😉
Not so much as run by default as a pre-commit hook, but with a CI check, PR checklist item, and optional pre-commit hook like Dask and xarray (e.g. http://xarray.pydata.org/en/stable/contributing.html#code-formatting). |
I was just reading about this the other day explaining the good and bad things about black and isort. |
@jthielen I'll have to think it over some more, but I'm leaning towards at least allowing it. Is there a way to run it in some kind of "diff" mode so we don't have to do a massive rewrite of files? |
If I'm interpreting this correctly (only auto-format the changes made in a given git diff), I think the answer is no. While I think it would be neat to have such an option were in place for incremental adoption, the README states:
|
Also for isort it was indicated to be careful with this module as it might screw up the list of from and import at the beginning of file. Python is quite finicky about the placement of modules as it has to import in the right order for some modules to work without giving an import error. This has to do with dependencies. This is also flagged in flake8. This happened to my code. |
MetPy has already been running with a lint check to alphabetize and group imports, just like In general, if you're having import order issues, in the overwhelming amount of cases (but not all) this means something isn't working right. This sometimes indicates a mismatch/incompatibility in what library things are compiled against. |
It only happened in rare cases where an import was needed before other imports. Maybe this has been fixed over the years. Didn't see this happen for a while now. Only in a few cases where flake8 flagged it. Since correcting this it didn't happen afterwards. |
I love providing my unqualified two cents here, so here I go again :) When I first started using With regards to |
In the spirit of Hackotoberfest, if I were to take this one, would it be appreciated? |
@MinchinWeb somebody running it and submitting it so I can see what horrors it does to our codebase would be fine and appreciated (and accepted for hacktoberfes--don't forget to set the line length to 95). Whether it gets merged would depend on how much it offends my delicate code sensibilities. 😉 |
For some reason, this cracks me up :) #1530 is just isort, #1531 is both isort and black (black would mess with your imports a little too, so it seems to me that if you do black, you should do isort too. And this way, hopefully there are no merge conflicts if you pick up both). Black mostly seems to be replacing quote marks (single quotes to double quotes) and adding lots of space to matrices. As a side, because black adjusts the code so significantly, almost any other merged code is going to cause painful merge conflicts, so if you like what you see, I'd suggest merging it in right away. On the Hackotoberfest front, they are only "accepting" Pull Requests this year "by merging it, adding the 'hacktoberfest-accepted' label to it, or [a maintainer] approving it." So if you would add the label (hacktoberfest-accepted) while you decide whether (or "weather", heehee) your code sensibilities are offended, that would be appreciated 😉 |
@MinchinWeb Thanks and happy to. |
So one year later, while I still hate many of black's formatting decisions, we're quickly reaching the point where having NO autoformatter is not an acceptable choice for a good Python project. So if I can't figure out how to make YAPF do what I want, we'll just need to adopt black (or blue). |
Even though I don't agree with some decisions that are being used for formatting code, I started using it as suggested in part of the code and it doesn't seem to create any problems at the moment. I do put imported modules (meaning not together) on separate lines and it seems to work fine. As for function parameters, I put parameters on separate lines when it doesn't fit on one line. I think it is just a matter of preference. However I still use a line length of 95 characters as suggested for Metpy. |
Based on xarray and pint at least, there seems to be some momentum in the ecosystem towards automatic code formatting with black and isort, and after some initial uncertainty, I've started taking a liking to it myself. Would this be something worth pursuing in MetPy? @dopplershift IIRC, you've had some dissension with black's formatting choices, but I thought it would still be worth discussing.
xref hgrecco/pint#929
The text was updated successfully, but these errors were encountered: