-
-
Notifications
You must be signed in to change notification settings - Fork 782
Prefer explicit imports from st2client.models #5333
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
Prefer explicit imports from st2client.models #5333
Conversation
Before this change, some places explicitly imported models from `st2client.models.<model>` and other places imported models from `st2client.models`. These `st2client.models` imports rely on the fact that `st2client/st2client/models/__init__.py` implicitly imports all models like: `from st2client.models.<model> import * # noqa` Note the "# noqa" which silences our linters instead of dealing with the bad `import *` practice. For external code, relying on the `from ... import *` in `st2client.models` is fine; And, doing so allows 3rd party code to have a much nicer interface. However, in ST2 code we should prefer explicit imports.
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.
+1 for no star imports :)
| from six.moves import range | ||
|
|
||
| from st2client import models | ||
| from st2client.models.action import Action, Execution |
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 thought black forces import per line, but maybe that's not the case.
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.
For importing packages or modules (import ...), yes it is one import per line as in PEP 8, but not for importing something from a module (from ... import ...).
I find splitting the from ... import ... imports across multiple lines with the same from ... import statement on each line to be overly verbose. If there are too many to fit on one line, then I go for this syntax (assuming it works with the project's code style):
from some.package.module import (
MY_CONSTANT,
my_function,
MyClass,
something_else,
another_something_else,
)
I'm not sure if isort or some other tool could enforce that style, but that wouldn't be part of this PR, however we do it.
These answers discuss PEP 8 and import styling:
https://stackoverflow.com/a/15011456
https://stackoverflow.com/a/38427828
Follow code style introduced in #5333
Before this change, some places explicitly imported models from
st2client.models.<model>and other places imported models fromst2client.models. Thesest2client.modelsimports rely on the fact thatst2client/st2client/models/__init__.pyimplicitly imports all models like:Note the "# noqa" which silences our linters instead of dealing with the bad
import *practice.For external code, relying on the
from ... import *inst2client.modelsis fine; And, doing so allows 3rd party code to have a much nicer interface. However, in ST2 code we should prefer explicit imports.