X Tutup
Skip to content

chore: remove usage of 'from ... import *'#1319

Merged
nejch merged 1 commit intopython-gitlab:masterfrom
JohnVillalovos:jlvillal/import_star
Feb 25, 2021
Merged

chore: remove usage of 'from ... import *'#1319
nejch merged 1 commit intopython-gitlab:masterfrom
JohnVillalovos:jlvillal/import_star

Conversation

@JohnVillalovos
Copy link
Member

@JohnVillalovos JohnVillalovos commented Feb 22, 2021

In gitlab/v4/objects/*.py remove usage of:

  • from gitlab.base import *

  • from gitlab.mixins import *

    Change them to:

    • from gitlab.base import CLASS_NAME
    • from gitlab.mixins import CLASS_NAME

    Programmatically update code to explicitly import needed classes only.

After the change the output of:
$ flake8 gitlab/v4/objects/*py | grep 'REST|Mixin'

Is empty. Before many messages about unable to determine if it was a
valid name.

@JohnVillalovos JohnVillalovos marked this pull request as draft February 22, 2021 23:28
@codecov-io
Copy link

Codecov Report

Merging #1319 (61a8c74) into master (d9fdf1d) will decrease coverage by 0.77%.
The diff coverage is 96.36%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1319      +/-   ##
==========================================
- Coverage   80.72%   79.95%   -0.78%     
==========================================
  Files          69       71       +2     
  Lines        3627     3747     +120     
==========================================
+ Hits         2928     2996      +68     
- Misses        699      751      +52     
Flag Coverage Δ
unit 79.95% <96.36%> (-0.78%) ⬇️

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

Impacted Files Coverage Δ
gitlab/v4/objects/milestones.py 69.81% <60.00%> (ø)
gitlab/v4/objects/clusters.py 85.71% <75.00%> (ø)
gitlab/v4/objects/files.py 50.00% <80.00%> (ø)
gitlab/v4/objects/ldap.py 54.54% <80.00%> (ø)
gitlab/v4/objects/merge_requests.py 65.45% <81.81%> (ø)
gitlab/v4/objects/epics.py 73.68% <83.33%> (ø)
gitlab/v4/objects/commits.py 78.26% <88.88%> (ø)
gitlab/v4/objects/projects.py 69.20% <90.90%> (ø)
gitlab/v4/objects/pipelines.py 88.00% <92.85%> (ø)
gitlab/v4/objects/users.py 89.87% <97.22%> (ø)
... and 49 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d9fdf1d...61a8c74. Read the comment docs.

@JohnVillalovos
Copy link
Member Author

JohnVillalovos commented Feb 23, 2021

I updated the files using this script and than ran tox -e black to fix formatting

#!/usr/bin/python3 -ttu
import argparse
import re
import sys

from typing import List, Set


BASE_CLASSES = ("RESTObject", "RESTObjectList", "RESTManager")
MIXIN_CLASSES = (
    "AccessRequestMixin",
    "BadgeRenderMixin",
    "CreateMixin",
    "CRUDMixin",
    "DeleteMixin",
    "DownloadMixin",
    "GetMixin",
    "GetWithoutIdMixin",
    "ListMixin",
    "NoUpdateMixin",
    "ObjectDeleteMixin",
    "ParticipantsMixin",
    "RefreshMixin",
    "RetrieveMixin",
    "SaveMixin",
    "SetMixin",
    "SubscribableMixin",
    "TimeTrackingMixin",
    "TodoMixin",
    "UpdateMixin",
    "UserAgentDetailMixin",
)


def main() -> int:
    args = parse_args()

    fixup_file(filename=args.filename)
    return 0


def fixup_file(*, filename: str):
    content = []
    with open(filename, "r") as in_file:
        for line in in_file.readlines():
            line = line.rstrip()
            content.append(line)

    base_classes = scan_classes(classes=BASE_CLASSES, content=content)
    mixin_classes = scan_classes(classes=MIXIN_CLASSES, content=content)

    output = []
    for line in content:

        if line.startswith("from gitlab.base import REST"):
            print("This file seems to already have been processed. Exiting")
            return
        if line.startswith("from gitlab.mixins import ("):
            print("This file seems to already have been processed. Exiting")
            return

        if line.startswith("from gitlab.base import"):
            if not base_classes:
                continue
            output.append(
                "from gitlab.base import {}".format(",".join(sorted(base_classes)))
            )
            continue

        if line.startswith("from gitlab.mixins import"):
            if not mixin_classes:
                continue
            output.append(
                "from gitlab.mixins import {}".format(",".join(sorted(mixin_classes)))
            )
            continue

        output.append(line)

    with open(filename, "w") as out_file:
        for line in output:
            print(line, file=out_file)


def scan_classes(*, classes: List[str], content: List[str]) -> Set[str]:
    result = set()
    for line in content:
        for class_name in classes:
            match = re.search(r"\b" + class_name + r"\b", line)
            if match:
                result.add(class_name)
    return result


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()

    parser.add_argument("-f", "--filename", required=True)

    args = parser.parse_args()
    return args


if "__main__" == __name__:
    sys.exit(main())

@JohnVillalovos JohnVillalovos marked this pull request as ready for review February 23, 2021 00:28
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 for this @JohnVillalovos! I'm happy to see more star imports gone 😁

I'm just thinking that since these will be used in every new module as resources are added, perhaps we could import them all as needed into the module's namespace to keep a smaller footprint in the class definitions (and this diff would also be smaller). Especially since some of them will also grow with type annotations. Like this:

https://github.com/encode/httpx/blob/0f280af8b170ed5cc48c12a894f71a8b5762f748/httpx/_client.py#L34-L47 and
https://github.com/encode/httpx/blob/0f280af8b170ed5cc48c12a894f71a8b5762f748/httpx/__init__.py#L6-L35

And similar in requests, although I prefer the above formatting in httpx (black probably won't let us do otherwise anyway 😄 ):

https://github.com/psf/requests/blob/bdc00eb0978dd3cdd43f7cd1f95ced7aa75fab39/requests/__init__.py#L124-L128

I think this would be a bit more readable for future contributors while still explicit. WDYT? Since you were already able to script this it might be a quick change for you :) The only downside would be adding mixins to imports as they get added to modules each time.

@JohnVillalovos
Copy link
Member Author

Sure. I think I could modify the script to do that... Let me look into it when I finish my work day.

Thanks for the review!

In gitlab/v4/objects/*.py remove usage of:
  * from gitlab.base import *
  * from gitlab.mixins import *

Change them to:
  * from gitlab.base import CLASS_NAME
  * from gitlab.mixins import CLASS_NAME

Programmatically update code to explicitly import needed classes only.

After the change the output of:
  $ flake8 gitlab/v4/objects/*py | grep 'REST\|Mixin'

Is empty. Before many messages about unable to determine if it was a
valid name.
@JohnVillalovos JohnVillalovos requested a review from nejch February 25, 2021 16:05
@nejch nejch merged commit 0b67ca2 into python-gitlab:master Feb 25, 2021
@JohnVillalovos
Copy link
Member Author

Sweet. Less 'import *' in the code 😊 Thanks for the merge!

@JohnVillalovos JohnVillalovos deleted the jlvillal/import_star branch February 25, 2021 18:18
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.

3 participants

X Tutup