Skip to content
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

isort support #1323

Merged
merged 4 commits into from
Mar 3, 2023
Merged

isort support #1323

merged 4 commits into from
Mar 3, 2023

Conversation

VeckoTheGecko
Copy link
Contributor

@VeckoTheGecko VeckoTheGecko commented Mar 2, 2023

isort sorts your imports so that you don't have to. The tool organises them into 5 sections separated by newlines:

  • FUTURE (eg. from __future__ import annotations)
  • STDLIB (eg. import os)
  • THIRDPARTY (eg. import xarray)
  • FIRSTPARTY (eg. import parcels.Field)
  • LOCALFOLDER (eg. from .utils import square)

Within each section the imports are sorted alphabetically, and also squashes imports down to one line.
eg.

from parcels.field import Field
from parcels.field import NestedField
from parcels.field import SummedField
from parcels.field import VectorField

becomes

from parcels.field import Field, NestedField, SummedField, VectorField

This will make import statements through the codebase much more structured, and easier to read. isort also has good integration with pre-commit.

You can add # isort:skip to a line to prevent isort from moving the import statement.

@VeckoTheGecko
Copy link
Contributor Author

Although this tool does standardize the code (so people don't have to worry about import orders), it does result in a large diff in this PR (and hence soiling git blame).

If having a good git blame is important, might be worth adding a .git-blame-ignore-revs file referencing commits that implement large formatting changes so they get ignored in git blame view.

@VeckoTheGecko
Copy link
Contributor Author

VeckoTheGecko commented Mar 2, 2023

That's pretty much it for this feature. Happy to discuss more, or merge if it looks good. Completely up to you and your team @erikvansebille if you want to merge this, or stick with manual import sorting.

Comment on lines -14 to -15
from parcels import ParticleSetSOA, ParticleFileSOA, KernelSOA # noqa
from parcels import ParticleSetAOS, ParticleFileAOS, KernelAOS # noqa
Copy link
Contributor Author

@VeckoTheGecko VeckoTheGecko Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isort will mess with the custom sorting here.

Comment on lines +8 to +29
from parcels import ( # noqa
AdvectionAnalytical,
AdvectionDiffusionEM,
AdvectionDiffusionM1,
AdvectionEE,
AdvectionRK4,
AdvectionRK4_3D,
AdvectionRK45,
ErrorCode,
Field,
FieldSet,
JITParticle,
KernelAOS,
KernelSOA,
ParticleFileAOS,
ParticleFileSOA,
ParticleSetAOS,
ParticleSetSOA,
ScipyParticle,
StateCode,
logger,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why there's only one class per line in this import call (and similar files)? From the isort documentation, it seems that multiple classes are also supported. Or is this intentional because it's even cleaner?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the way imports are formatted using the black auto formatter, which isort is compatible with by specifying the profile to be black (see the modified setup.cfg).

Although we don't use black at the moment, it is a really popular formatter that I think would be good to consider introducing in future. This is just a small step in that direction, preventing reformatting the imports again should we start using black.

@erikvansebille
Copy link
Member

Thanks for implementing this, @VeckoTheGecko; looks great! I agree with you that using tools like isort to clean up the code (and keep it clean) will facilitate development. So I'm happy to merge into master after you've answered my question above on the one-per-line-import in the test*-files

@erikvansebille erikvansebille merged commit b99b92a into OceanParcels:master Mar 3, 2023
@VeckoTheGecko VeckoTheGecko deleted the isort branch March 3, 2023 08:23
@VeckoTheGecko
Copy link
Contributor Author

@erikvansebille would you mind bringing new_docs up to date with master? I'm wanting to branch off new_docs with these new changes :)

@erikvansebille
Copy link
Member

@erikvansebille would you mind bringing new_docs up to date with master? I'm wanting to branch off new_docs with these new changes :)

Yup, will do after I also merged #1325. But can't you do it yourself?

@VeckoTheGecko
Copy link
Contributor Author

VeckoTheGecko commented Mar 3, 2023

new_docs is on the parcels repo, so I have no permissions or write access (outside of submitting PRs into it). So I can't merge master into it

@erikvansebille
Copy link
Member

OK, I've just invited you to the OceanParcels team, so that you can commit directly on the OceanParcels repo. Welcome to the team!

@VeckoTheGecko
Copy link
Contributor Author

Glad to be aboard! 🥳🥳🥳🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants