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

Move imports up #27288

Closed
wants to merge 1 commit into from
Closed

Move imports up #27288

wants to merge 1 commit into from

Conversation

balloob
Copy link
Member

@balloob balloob commented Oct 7, 2019

Description:

Run script to move single line imports up. Then run isort on changed files to make the sorting correct.

Part of #27284

Hacked together with this script:

from pathlib import Path

component_dir = Path("./homeassistant/components")

import_lines = ("import", "from", ",", ")")

for fil in component_dir.glob("**/*.py"):
    # if "xiaomi_miio/fan.py" not in str(fil):
    #     continue

    lines = fil.read_text().split("\n")
    inject_import_pos = None
    to_push_up = []

    quotes_found = 0

    normal_code_started = False

    for no, line in enumerate(lines):
        if inject_import_pos is None:
            quotes_found += line.count('"""')

            if quotes_found >= 2:
                inject_import_pos = no + 1

            continue

        if not normal_code_started:
            if line == "":
                continue

            if any(s in line for s in import_lines):
                continue

        normal_code_started = True

        if " import " in line:
            to_push_up.append(no)

    if to_push_up:
        print(fil, to_push_up)

    for no in to_push_up:
        lines.insert(inject_import_pos, lines.pop(no).strip())

    # print("\n".join(lines))
    fil.write_text("\n".join(lines))

@balloob balloob mentioned this pull request Oct 7, 2019
9 tasks
@balloob balloob reopened this Oct 7, 2019
@@ -11,7 +13,6 @@
def setup(hass, config):
"""Set up the BeagleBone Black GPIO component."""
# pylint: disable=import-error
from Adafruit_BBIO import GPIO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this incarnation of the impl will leave a bunch of those # pylint: disable=import-errors behind; they should be cleaned up one way or another (either moved to end of the import line if still needed, or removed altogether if no longer necessary). Ditto possibly some # noqas if there are any.

Copy link
Member

@scop scop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, could remove the import-outside-toplevel disable and comment from pylintrc while at it.

@balloob
Copy link
Member Author

balloob commented Oct 7, 2019

This doens't fix all top imports.

I also see that legacy scene support is now going to kick our but, as it imports constants from integrations that now need their dependencies installed.

@balloob
Copy link
Member Author

balloob commented Oct 7, 2019

I will close this for now.

@balloob balloob closed this Oct 7, 2019
@balloob
Copy link
Member Author

balloob commented Oct 7, 2019

Actually that was just 1 import. Let's try to fix this.

@balloob
Copy link
Member Author

balloob commented Oct 7, 2019

I couldn't reopen this, new one at #27293

@lock lock bot locked and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants