Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

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

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

nejch merged 1 commit intopython-gitlab:masterfromJohnVillalovos:jlvillal/import_star
Feb 25, 2021

Conversation

@JohnVillalovos
Copy link
Member

@JohnVillalovosJohnVillalovos commentedFeb 22, 2021
edited
Loading

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.

@JohnVillalovosJohnVillalovos marked this pull request as draftFebruary 22, 2021 23:28
@codecov-io
Copy link

Codecov Report

Merging#1319 (61a8c74) intomaster (d9fdf1d) willdecrease coverage by0.77%.
The diff coverage is96.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
FlagCoverage Δ
unit79.95% <96.36%> (-0.78%)⬇️

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

Impacted FilesCoverage Δ
gitlab/v4/objects/milestones.py69.81% <60.00%> (ø)
gitlab/v4/objects/clusters.py85.71% <75.00%> (ø)
gitlab/v4/objects/files.py50.00% <80.00%> (ø)
gitlab/v4/objects/ldap.py54.54% <80.00%> (ø)
gitlab/v4/objects/merge_requests.py65.45% <81.81%> (ø)
gitlab/v4/objects/epics.py73.68% <83.33%> (ø)
gitlab/v4/objects/commits.py78.26% <88.88%> (ø)
gitlab/v4/objects/projects.py69.20% <90.90%> (ø)
gitlab/v4/objects/pipelines.py88.00% <92.85%> (ø)
gitlab/v4/objects/users.py89.87% <97.22%> (ø)
... and49 more

Continue to review full report at Codecov.

Legend -Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing data
Powered byCodecov. Last updated9fdf1d...61a8c74. Read thecomment docs.

@JohnVillalovos
Copy link
MemberAuthor

JohnVillalovos commentedFeb 23, 2021
edited
Loading

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

#!/usr/bin/python3 -ttuimport argparseimport reimport sysfrom typing import List, SetBASE_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 0def 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 resultdef parse_args() -> argparse.Namespace:    parser = argparse.ArgumentParser()    parser.add_argument("-f", "--filename", required=True)    args = parser.parse_args()    return argsif "__main__" == __name__:    sys.exit(main())

@JohnVillalovosJohnVillalovos marked this pull request as ready for reviewFebruary 23, 2021 00:28
Copy link
Member

@nejchnejch 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
MemberAuthor

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_NAMEProgrammatically 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 avalid name.
@nejchnejch merged commit0b67ca2 intopython-gitlab:masterFeb 25, 2021
@JohnVillalovos
Copy link
MemberAuthor

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

@JohnVillalovosJohnVillalovos deleted the jlvillal/import_star branchFebruary 25, 2021 18:18
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nejchnejchnejch approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@JohnVillalovos@codecov-io@nejch

[8]ページ先頭

©2009-2025 Movatter.jp