Skip to content

[Python] Use explicit imports #752

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

Merged
merged 1 commit into from
Dec 23, 2015
Merged

[Python] Use explicit imports #752

merged 1 commit into from
Dec 23, 2015

Conversation

practicalswift
Copy link
Contributor

No description provided.

@@ -1,6 +1,3 @@
from __future__ import absolute_import
from .main import main

Copy link
Contributor

Choose a reason for hiding this comment

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

This change causes an error:

apple/swift $ ./utils/cmpcodesize/cmpcodesize.py -h
Traceback (most recent call last):
  File "./utils/cmpcodesize/cmpcodesize.py", line 6, in <module>
    cmpcodesize.main()
AttributeError: 'module' object has no attribute 'main'

Copy link
Contributor

Choose a reason for hiding this comment

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

The error could be fixed with a corresponding change to utils/cmpcodesize/cmpcodesize.py:

diff --git a/utils/cmpcodesize/cmpcodesize.py b/utils/cmpcodesize/cmpcodesize.py
index 0390af7..eb9ecfa 100755
--- a/utils/cmpcodesize/cmpcodesize.py
+++ b/utils/cmpcodesize/cmpcodesize.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python

-import cmpcodesize
+from cmpcodesize.main import main

 if __name__ == '__main__':
-    cmpcodesize.main()
+    main()

Which I wouldn't be opposed to. I wrote it this way because it mirrored the structure of llvm/utils/lit/lit.py.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious, though: is it bad form to use this style of relative imports? If so, should a corresponding change be proposed to llvm/utils/lit as well?

/cc @ddunbar, author of lit

from SwiftBuildSupport import check_call, get_all_preset_names
from SwiftBuildSupport import get_preset_options, HOME, print_with_argv0
from SwiftBuildSupport import quote_shell_command, SWIFT_BUILD_ROOT
from SwiftBuildSupport import SWIFT_SOURCE_ROOT
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: How about instead:

from SwiftBuildSupport import (
    SWIFT_SOURCE_ROOT,
    SWIFT_BUILD_ROOT,
    print_with_argv0,
    quote_shell_command,
    check_call,
    get_preset_options,
    get_all_preset_names
)

I've seen this import style suggested in a few style guides:

@practicalswift
Copy link
Contributor Author

@modocache Thanks a lot for reviewing! I've updated accordingly. Please take a look at the updated code :-)

@gribozavr
Copy link
Contributor

@practicalswift I think this is an improvement, but I would recommend to switch to the code formatting suggested by @modocache:

from SwiftBuildSupport import (
    SWIFT_SOURCE_ROOT,
    SWIFT_BUILD_ROOT,
    print_with_argv0,
    quote_shell_command,
    check_call,
    get_preset_options,
    get_all_preset_names
)

since we normally don't do "endline" layout in Python code.

@practicalswift
Copy link
Contributor Author

@gribozavr Fixed :-)

@modocache
Copy link
Contributor

Looks great! Thanks for updating. I'll follow up with @ddunbar about how lit is structured.

gribozavr added a commit that referenced this pull request Dec 23, 2015
@gribozavr gribozavr merged commit d48fa73 into swiftlang:master Dec 23, 2015
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