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

Add isort for sorting imports #807

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

hwpang
Copy link
Contributor

@hwpang hwpang commented Apr 17, 2024

Description

Add isort in dev dependency and check that imports are properly sorted in CI

Example / Current workflow

n/a

Bugfix / Desired workflow

n/a

Questions

n/a

Relevant issues

n/a

Checklist

  • linted with flake8?
  • (if appropriate) unit tests added?

Copy link
Member

@JacksonBurns JacksonBurns left a comment

Choose a reason for hiding this comment

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

I think this will fix the circular import, please try it and resort and then push (push so we can all see if it fails).

chemprop/featurizers/__init__.py Show resolved Hide resolved
chemprop/featurizers/atom.py Show resolved Hide resolved
@JacksonBurns
Copy link
Member

argh more circular imports... let's chat about this offline. ugh

Copy link
Contributor

@davidegraff davidegraff left a comment

Choose a reason for hiding this comment

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

aside from the circular import issues you seem to have, I have just one small comment. Otherwise, lgtm! thanks!

chemprop/featurizers/atom.py Outdated Show resolved Hide resolved
@KnathanM KnathanM added this to the v2.1.0 milestone Apr 18, 2024
@hwpang
Copy link
Contributor Author

hwpang commented May 23, 2024

@JacksonBurns I've figured out a way to fix the circular import while using isort (there is only one place that we cannot import alphabetically, so I add a tag to tell isort to skip), please review!

from pathlib import Path
import sys

Copy link
Contributor

Choose a reason for hiding this comment

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

While we are sorting everything, would we also make sense to remove these extra lines between imports? I think we would still keep a line between the chemprop imports and other imports.

Copy link
Contributor

Choose a reason for hiding this comment

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

Almost all style guides define three sections of imports, which should be defined in the following order at the top of every file with a blank line between them:

  1. standard library
  2. third party
  3. local imports

I personally define a 4th section, "sublocal," e.g., CLI subpackage imports which I view as logically separate from core library (e.g., chemprop.*) imports.

Copy link
Contributor Author

@hwpang hwpang May 24, 2024

Choose a reason for hiding this comment

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

Yeah, isort uses these sections: FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER, see here

@@ -1,4 +1,5 @@
from os import PathLike

Copy link
Contributor

Choose a reason for hiding this comment

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

Did isort add this extra line? Why does it do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, because isort uses these sections: FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER. os is a standard library, and torch is a third party library.

from .transforms import UnscaleTransform
from .utils import Activation

__all__ = [
"Aggregation",
Copy link
Contributor

Choose a reason for hiding this comment

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

Originally the order of all matched the order of imports? These changes breaks that symmetry, are we okay with that or should the order in all also be updated?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is better IMO. The imports are in sorted order, but these are separate from the definitions this module exports. We should want the exports to also be in sorted order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the discussion. I see that isort sorts __all__ with sort_reexports = true in the config file. However, running isort with sort_reexports = true breaks our file for some reason. The output is copied below. If I remove all the imports in the same file, __all__ is actually sorted correctly. What do you think to be the best way to proceed? I'm happy to take suggestions.

from .agg import (
    Aggregation,
    AggregationRegistry,
    AttentiveAggregat__all__ = ['Activation', 'Aggregation', 'AggregationRegistry', 'AtomMessagePassing', 'AttentiveAggregation',
 'BCELoss', 'BCEMetric', 'BinaryAUPRCMetric', 'BinaryAUROCMetric', 'BinaryAccuracyMetric',
 'BinaryClassificationFFN', 'BinaryClassificationFFNBase', 'BinaryDirichletFFN',
 'BinaryDirichletLoss', 'BinaryF1Metric', 'BinaryMCCLoss', 'BinaryMCCMetric', 'BondMessagePassing',
 'BoundedMAEMetric', 'BoundedMSELoss', 'BoundedMSEMetric', 'BoundedMixin', 'BoundedRMSEMetric',
 'CrossEntropyLoss', 'CrossEntropyMetric', 'DirichletMixin', 'EvidentialFFN', 'EvidentialLoss',
 'LossFunction', 'LossFunctionRegistry', 'MAEMetric', 'MSELoss', 'MSEMetric', 'MVELoss', 'MccMixin',
 'MeanAggregation', 'MessagePassing', 'Metric', 'MetricRegistry', 'MulticlassClassificationFFN',
 'MulticlassDirichletFFN', 'MulticlassDirichletLoss', 'MulticlassMCCLoss', 'MulticlassMCCMetric',
 'MulticomponentMessagePassing', 'MveFFN', 'NormAggregation', 'Predictor', 'PredictorRegistry',
 'R2Metric', 'RMSEMetric', 'RegressionFFN', 'SIDLoss', 'SIDMetric', 'SpectralFFN', 'SumAggregation',
 'ThresholdedMixin', 'UnscaleTransform', 'WassersteinLoss', 'WassersteinMetric']
classDirichletFFN,
    MveFFN,
    Predictor,
    PredictorRegistry,
    RegressionFFN,
    SpectralFFN,
)
from .transforms import UnscaleTransform
from .utils import Activation

__all__ = [

from pathlib import Path
import sys

Copy link
Contributor

Choose a reason for hiding this comment

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

Almost all style guides define three sections of imports, which should be defined in the following order at the top of every file with a blank line between them:

  1. standard library
  2. third party
  3. local imports

I personally define a 4th section, "sublocal," e.g., CLI subpackage imports which I view as logically separate from core library (e.g., chemprop.*) imports.

from .transforms import UnscaleTransform
from .utils import Activation

__all__ = [
"Aggregation",
Copy link
Contributor

Choose a reason for hiding this comment

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

This is better IMO. The imports are in sorted order, but these are separate from the definitions this module exports. We should want the exports to also be in sorted order.

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.

None yet

4 participants