Skip to content

"Google" profile is not quite Google style  #1486

@ssbr

Description

@ssbr

The Google style support added in #969 has a couple of subtle differences from the actual Google style guide:

  1. The import sorting is case sensitive for some reason, although I can't figure out why (case sensitivity is turned off). For example, the following is correct Google style:

    import collections
    import cProfile

    but isort reorders to place the cProfile import before collections:

    $ pipx run --python=python3.8 isort --profile=google test.py --diff
    --- /tmp/test.py:before 2020-09-17 10:28:34.858694
    +++ /tmp/test.py:after  2020-09-17 10:30:01.027299
    @@ -1,2 +1,2 @@
    +import cProfile
     import collections
    -import cProfile

    This is a style error due to the following wording in the Google Python style guide: "Within each grouping, imports should be sorted lexicographically, ignoring case, according to each module’s full package path" (emphasis added)

  1. The import sorting sorts by the wrong key. For example, the following is correct Google style:

    from a import z
    from a.b import c

    but isort swaps the order:

    $ pipx run --python=python3.8 isort --profile=google test2.py --diff
    --- /tmp/test2.py:before        2020-09-17 10:34:39.196377
    +++ /tmp/test2.py:after 2020-09-17 10:34:41.925939
    @@ -1,2 +1,2 @@
    +from a.b import c
     from a import z
    -from a.b import c

    This is super subtle -- "a.z" comes after "a.b.c" lexicographically, but the Google style guide, when it refers to the "full package path", actually means the a in from a import z. The style guide did not explicitly say this anywhere, so it's not fair to expect isort to know this. In fact, the only reason I know it is that the internal Google lint rules enforces it. So I had to change the style guide to be more explicit about this, so that I could file this bug report. ;)

    As of google/styleguide@f84020e , the style guide is explicit about this, and now says the following: "Within each grouping, imports should be sorted lexicographically, ignoring case, according to each module’s full package path (the path in from path import ...)."

(Incidentally, if you were curious, the style guide makes no rule about sorting e.g. "foo" with "Foo", and the internal linter accepts either order.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions