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

Multi Item Imports vs Duplicates "froms" #18

Open
cancan101 opened this issue Jul 27, 2017 · 6 comments
Open

Multi Item Imports vs Duplicates "froms" #18

cancan101 opened this issue Jul 27, 2017 · 6 comments

Comments

@cancan101
Copy link
Contributor

What do people think about these two import styles:

from project.stuff import foo
from project.stuff import bar

vs.

from project.stuff import foo, bar
# or
from project.stuff import (
    foo,
    bar,
)

/CC @nathansilberman @taion @itajaja @nevsan

@itajaja
Copy link
Member

itajaja commented Jul 27, 2017

second preferable

@taion
Copy link
Contributor

taion commented Jul 27, 2017

One import statement per module please. See https://www.python.org/dev/peps/pep-0008/#imports.

@nevsan
Copy link

nevsan commented Jul 27, 2017

Yes. And we should remind people that you can use parentheses to break them into multiple lines if they exceed the line length (any opinions here? I don't think it's necessary to always use parentheses if it fits on one legal line length).

@nathansilberman
Copy link

I like the first one, but it seems I'm clearly in the minority. For option #2, if we cannot fit the list on one line, which is preferrable:

Option a:

from a.b.c import (
d, e, f,
)

or Option b:

from a.b..c import (
d,
e,
f,
)

My pref is option (a)

@nevsan
Copy link

nevsan commented Jul 27, 2017

I like option (a). YAPF does something more like option (a). I usually prefer the YAPF default decisions when I don't have strong opinions.

@taion
Copy link
Contributor

taion commented Jul 27, 2017

I have something like this in the multiline section. The best way to go is to fit everything on one line if possible; otherwise, to put everything on its own line, per 4Catalyzer/flask-resty#136 (comment).

So

# When everything fits on one line:
(
    a, b, c,
)

# Assume these don't fit on one line:
(
    long_identifier_1,
    long_identifier_2,
    long_identifier_3,
)

# But almost never:
(
    a,
    b, c,
)

The latter avoids cruft in diffs from re-wrapping lines when e.g. alphabetizing imports.

There are very rare exceptions to the last; e.g. SQLAlchemy has Index(name, *columns), but:

# This:
Index(
    'ix_name',
    column_1, column_2, column_3,
)

# Is better than:
Index(
    'ix_name',
    column_1,
    column_2,
    column_3,
)

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

No branches or pull requests

5 participants