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

chore: convert to using relative imports #1728

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

JohnVillalovos
Copy link
Member

@JohnVillalovos JohnVillalovos commented Dec 5, 2021

First commit renames gitlab/__version__.py to gitlab.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

@codecov-commenter
Copy link

codecov-commenter commented Dec 5, 2021

Codecov Report

Merging #1728 (6b3e7c0) into main (c6d7e9a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1728   +/-   ##
=======================================
  Coverage   92.02%   92.03%           
=======================================
  Files          76       76           
  Lines        4790     4793    +3     
=======================================
+ Hits         4408     4411    +3     
  Misses        382      382           
Flag Coverage Δ
cli_func_v4 81.38% <96.66%> (+0.01%) ⬆️
py_func_v4 80.24% <86.29%> (+0.03%) ⬆️
unit 83.18% <93.70%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
gitlab/version.py 100.00% <ø> (ø)
gitlab/__init__.py 100.00% <100.00%> (ø)
gitlab/__main__.py 100.00% <100.00%> (ø)
gitlab/base.py 90.22% <100.00%> (ø)
gitlab/cli.py 98.40% <100.00%> (+0.03%) ⬆️
gitlab/client.py 90.51% <100.00%> (-0.05%) ⬇️
gitlab/config.py 94.44% <100.00%> (ø)
gitlab/const.py 100.00% <100.00%> (ø)
gitlab/mixins.py 91.52% <100.00%> (-0.03%) ⬇️
gitlab/v4/cli.py 81.25% <100.00%> (-0.14%) ⬇️
... and 61 more

@JohnVillalovos JohnVillalovos marked this pull request as draft December 5, 2021 20:05
@JohnVillalovos JohnVillalovos marked this pull request as ready for review December 5, 2021 20:12
Copy link
Member

@nejch nejch left a 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/__init__.py Outdated Show resolved Hide resolved
gitlab/cli.py Outdated Show resolved Hide resolved
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
Copy link
Member

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?

Suggested change
from . import config as gl_config
from .config import GitlabConfigParser, ConfigError

Copy link
Member Author

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
Copy link
Member

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.

Suggested change
from . import cli
from .cli import main

Copy link
Member Author

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()
Copy link
Member

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:

Suggested change
cli.main()
main()

Copy link
Member Author

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 😊

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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 or gitlab.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"

gitlab/cli.py Show resolved Hide resolved
@JohnVillalovos
Copy link
Member Author

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.

@JohnVillalovos JohnVillalovos force-pushed the jlvillal/relative_imports branch 6 times, most recently from c2dca37 to 9afb42f Compare December 16, 2021 01:25
@JohnVillalovos JohnVillalovos force-pushed the jlvillal/relative_imports branch 2 times, most recently from 7da5ac0 to c873c92 Compare December 24, 2021 04:46
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__
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
@JohnVillalovos JohnVillalovos marked this pull request as draft February 12, 2022 16:53
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

3 participants