-
Notifications
You must be signed in to change notification settings - Fork 121
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
refactor: Avoid implicit reexport in submodules #539
refactor: Avoid implicit reexport in submodules #539
Conversation
Codecov Report
@@ Coverage Diff @@
## main #539 +/- ##
==========================================
+ Coverage 94.46% 94.49% +0.03%
==========================================
Files 71 71
Lines 10728 10795 +67
Branches 1075 1075
==========================================
+ Hits 10134 10201 +67
Misses 419 419
Partials 175 175
Continue to review full report at Codecov.
|
5c38b3a
to
05c446c
Compare
Sorry, I just realized I merged some changes from a more recent PR that created conflicts. I'm happy to rebase and resolve these if you want. |
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'm +1 for this change, but I'd like to be sure others are on board as well.
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.
Some groups have style guides and other reasons to follow specific patterns. The one I'm suggesting is documented here: https://google.github.io/styleguide/pyguide.html#22-imports
from pystac.stac_object import STACObject | ||
from pystac.collection import Collection | ||
from pystac.errors import ExtensionNotImplemented | ||
from pystac.summaries import RangeSummary |
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 module only imports. e.g. These are a good balance between import pystac
which causes trouble and not having to worry about getting more symbols out of any particular module. So as long as modules names aren't mutated, then the imports do not have to change.
from pystac import asset
from pystac import summaries
from pystac import stac_object
from pystac import collection
from pystac import errors
from pystac import summaries
This also means it's a lot easier to find anything in use from a module within the current file even if the names are not as simple as they often are. e.g. a simple search for asset.
will find asset.Asset
and asset.SomeHelper
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.
That's a nice compromise. My main question would be whether it helps with avoiding circular imports, which are currently plaguing the code base. For example, running isort
will immediately break the entire code base, and there seems to be a bunch of duplicate imports (import pystac
followed by from pystac import foo
) which, if merged, also cause circular import errors.
@@ -12,16 +17,16 @@ class SummariesExtension: | |||
extension-specific class that inherits from this class and instantiate that. See | |||
:class:`~pystac.extensions.eo.SummariesEOExtension` for an example.""" | |||
|
|||
summaries: pystac.Summaries | |||
summaries: Summaries |
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.
from above, this would be summaries: summaries.Summaries
. That's a bit wordy, but really obvious where it comes from.
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.
Yeah, fixing the comments is another can of worms I'm not comfortable with tackling until we've nailed the imports themselves. I'm guessing they should resolve within the current set of imports, that is, if I from pystac import summaries
the comment should be summaries: summaries.Summaries
as you said, rather than summaries: pystac.summaries.Summaries
.
from pystac.asset import Asset | ||
from pystac.stac_object import STACObjectType | ||
from pystac.errors import ExtensionTypeError | ||
from pystac.item import Item | ||
from pystac.extensions.base import ExtensionManagementMixin, PropertiesExtension |
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.
Here I would suggest: from pystac.extentions import base
. Then it's base.PropertiesExtension
in the code.
I like this approach for the reasons that @schwehr mentions in this comment. It also keeps our import statements a bit shorter and means that classes/functions with the same name in different modules wouldn't conflict. We would have to resolve some shadowing issues since we use variable names like If we were to adopt this, I would also propose that in cases where we are importing a class purely for type annotations we always put it in a |
Thanks, that would be helpful. Then I can start changing all these PRs to use the import strategy suggested by @schwehr. |
I'm going to redo these PRs from scratch, so this may take a while… |
05c446c
to
6ef6656
Compare
@l0b0 Not sure if this is still needed if you are starting from scratch, but I rebased this onto |
I'm -0 on this change. I really like the simplicity of just using That said, I don't feel that strongly and if people think it will really improve the maintainability in the long run then I'm ok with these sort of changes for the sake of strictness. I do feel strongly that, as a user, I don't want to have to go digging for imports to use the types of PySTAC. Having a single I'm not particularly in favor of the "only import submodules rule", but that's just not the way I've coded Python. Again, if that's what folks agree is a good way to go for the internal library development, then I'm willing to go with it - it feels like preference tuning at this point which I guess marks a good point in the library's lifecycle if we're getting to that fine of detail. I'd just keep the developer experience of downstream users who don't have strongly typed tooling in mind when making changes that will affect the import strategy for those users (which may not apply to this PR but is more a general comment). |
Related Issue(s): #332
Description: I couldn't do the equivalent changes anywhere in the
serialization
submodule without running into circular imports, so I've postponed that until it's easier to fix.PR Checklist:
pre-commit run --all-files
)scripts/test
)