-
Notifications
You must be signed in to change notification settings - Fork 643
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
chore: convert to using relative imports #1728
base: main
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1728 +/- ##
=======================================
Coverage 92.02% 92.03%
=======================================
Files 76 76
Lines 4790 4793 +3
=======================================
+ Hits 4408 4411 +3
Misses 382 382
Flags with carried forward coverage won't be shown. Click here to find out more.
|
a9cf620
to
45f3959
Compare
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.
Thanks @JohnVillalovos this is huge so I just had a few general comments first, the rest LGTM but would probably be affected by the other comments if we decide on this.
gitlab/cli.py
Outdated
@@ -26,8 +26,10 @@ | |||
|
|||
from requests.structures import CaseInsensitiveDict | |||
|
|||
import gitlab.config | |||
from gitlab.base import RESTObject | |||
from . import config as gl_config |
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.
As above, should we maybe import only the stuff we need directly like with the other imports here in this commit?
from . import config as gl_config | |
from .config import GitlabConfigParser, ConfigError |
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, can do that. I'm usually not a big fan of doing from foo import spam, egg
where spam
and egg
are things inside the module. As then in my opinion it is harder when reading the code to know where something comes from. In this case when reading the code people can see it used as: gl_config.GitlabConfigParser
. But then again I am also doing a from . import config as gl_config
which is also somewhat confusing 😟
@@ -1,4 +1,4 @@ | |||
import gitlab.cli | |||
from . import cli |
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.
We seem to mix from-importing modules and functions/classes a lot (not a criticism of this PR, just how the project grew I assume). Maybe we could have a convention where we always functions/class if it's a reasonable number of things to import? E.g. if we don't need a large multi-line import, just import directly, otherwise the module.
from . import cli | |
from .cli import main |
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.
My convention would lean towards just import the module, not items within the module. As I think it makes the code more readable. We read the code much much more then we ever write it.
My goal (not that I achieve it) is that we make the code is easy to read as possible to reduce "cognitive load". I say this as a vim
user 😊
|
||
if __name__ == "__main__": | ||
gitlab.cli.main() | ||
cli.main() |
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.
If we do the above, then:
cli.main() | |
main() |
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.
Note: repeat of above comment
My convention would lean towards just import the module, not items within the module. As I think it makes the code more readable. We read the code much much more then we ever write it.
My goal (not that I achieve it) is that we make the code is easy to read as possible to reduce "cognitive load". I say this as a vim
user 😊
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 me that readability comes with full namespaces (I guess that has its own issues which is why you added relative imports) but not so much with relative imports.
For example with the change below, it's also not clear in the codebase whether we are calling functions from gitlab.cli
or gitlab.v4.cli
:
+ from .v4 import cli
# ...
parser = _get_base_parser()
- return gitlab.v4.cli.extend_parser(parser)
+ return cli.extend_parser(parser)
Maybe we can poke around in some popular packages and see what they are doing :) I'll have a look, feel free to add more here 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.
I checked a few and seems to me importing classes/functions is pretty common? black/requests/click/httpx all seem to almost exclusively do relative imports with functions/classes. mypy is more of a mixed bag and they do both, but not sure how they make the distinction.
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 example with the change below, it's also not clear in the codebase whether we are calling functions from
gitlab.cli
orgitlab.v4.cli
:+ from .v4 import cli # ... parser = _get_base_parser() - return gitlab.v4.cli.extend_parser(parser) + return cli.extend_parser(parser)
Well I disagree not being clear (in this case) because they are within three lines of each other 😊 Of course they won't always be within three lines of each other. Unfortunately it is not valid syntax to do from . import v4.cli
. To enhance clarity could do from .v4 import cli as v4_cli
??
But I personally find it much easier to comprehend cli.main()
vs main()
when reading the code. As it makes me think, "oh we are probably calling main() in a different module". Where with just main()
I think, "we are calling the main() function in this module"
45f3959
to
834677e
Compare
Note 834677e is only a rebase with no functional changes. Had conflicts after another PR merged. Will work on fixing things in regards to comments later on today most likely. |
c2dca37
to
9afb42f
Compare
7da5ac0
to
c873c92
Compare
c873c92
to
17ac7c1
Compare
It was confusing having __version__.py as we were importing the __version__ variable from __version__.py into the top-level namespace while also having __version__.py. So depending where we were in the import chain __version__ could refer to the module __version__.py or it could refer to the variable __version__
17ac7c1
to
89981c6
Compare
Switch to using relative imports to ensure that we are importing from within our library. Also use the form: from foo import bar as bar When we want to signify that we want the import to be re-exported. https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-no-implicit-reexport
89981c6
to
6b3e7c0
Compare
First commit renames
gitlab/__version__.py
togitlab.version.py
to minimize confusion.Switch to using relative imports to ensure that we are importing from
within our library.
Also use the form:
from foo import bar as bar
When we want to signify that we want the import to be re-exported.
https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-no-implicit-reexport