-
-
Notifications
You must be signed in to change notification settings - Fork 582
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
"Google" profile is not quite Google style #1486
"Google" profile is not quite Google style #1486
Comments
First one seems to be simply a omission |
Hi @ssbr, Thanks for reporting! It's important for us to make the google profile fully compatible with its namesake - I think @lntuition is on the right path on how to accomplish that. If you have any other test example snippets you can provide, or any good public OpenSource projects from Google that utilize the Python style guide - let me know and we can expand the testing on isort's end to include those as well. This will be a priority for the next minor release due out in October. Thanks! ~Timothy |
@lntuition The thing that confused me w.r.t. @timothycrosley the project in this case was https://github.com/ssbr/refex, but it's definitely not style-complaint already (part of why I wanted to run isort...). I couldn't see any other problems with the isort output though. I would guess that any Python project in the google org, or a non-google github org that's still run by google or alphabet, would work. Maybe these are some good places to start (picking projects I know have had involvement with the actual Google Python team, and so are more likely to have good style):
If isort does them dirty even after the two fixes in OP, you can send me the diff and I can try to look into if/why the google style guide differs on that point. (Though chances are good that it's just a style error -- no project is perfect!) A couple of common things that might crop up are: libraries moving from third-party to first-party section (e.g. enum in py2 vs py3, or typing); ordering problems due to library renames that weren't followed by reordering imports, especially as part of the opensourcing process (this is the big thing that bit me personally). |
@ssbr and @lntuition I think what we likely need to turn off per case-sensitivity, is turning off the order by type setting that defaults to |
Wait, is But yeah, since google style forbids importing anything except modules, grouping by type doesn't make sense, and would only serve to break the ordering rules. |
I don't think it's that |
@ssbr I've finished including the requested updates to the Google profile in develop, and plan on releasing soon. If you have a chance, I'd love to get your feedback on the diffs isort produces for the projects you linked. In particular, if isort needs to change anything more to be fully compatible with the Google style for imports: abseil-py: https://gist.github.com/timothycrosley/56eda241c0d92a25b4b0afba03621ae0 Thanks! ~Timothy |
I took a look today :) tl;dr: I think there's one big type of diff that might matter for other Google projects, where isort is systematically disagreeing with how Google enforces its style guide, but I would personally be happy with the current output, I think. I'll leave it up to you if you want to reopen for that case or keep closed until someone who wants it chimes in. Wrong diffsThis is the big one I referenced in the tldr. It isn't "wrong" per se, but is a very large difference in handling to what all Google projects will do, so it might be worth adding a tweak for this. --- a/absl/flags/_argument_parser.py
+++ b/absl/flags/_argument_parser.py
@@ -27,9 +27,10 @@ import csv
import io
import string
-from absl.flags import _helpers
import six
+from absl.flags import _helpers Diffs like this are because absl differs from isort on what it believes "first-party" means -- in the internal source repo that absl is drawn from, anything that is in the Rather than TBH I expect some projects would actually prefer isort's current output. I know I would. Not "wrong" per se, but Google allows --- a/pytype/pyi/parser.py
+++ b/pytype/pyi/parser.py
@@ -12,7 +12,8 @@ from pytype.pytd import pytd
from pytype.pytd import pytd_utils
from pytype.pytd import slots as cmp_slots
from pytype.pytd import visitors
-from pytype.pytd.parse import parser_constants # pylint: disable=g-importing-member
+from pytype.pytd.parse import \
+ parser_constants # pylint: disable=g-importing-member LGTM diffsThis is an error in _argument_parser.py, and isort is correct. I can't really explain the order they used, to be honest. --- a/absl/flags/_argument_parser.pyi
+++ b/absl/flags/_argument_parser.pyi
@@ -15,9 +15,9 @@
"""Contains type annotations for _argument_parser.py."""
-from typing import Text, TypeVar, Generic, Iterable, Type, List, Optional, Sequence, Any
-
import enum
+from typing import (Any, Generic, Iterable, List, Optional, Sequence, Text,
+ Type, TypeVar) Many of the whitespace or ordering changes like this are -- they aren't style errors in Google style, but optional formatting decision. I think isort is justified in making this kind of change. --- a/absl/flags/_defines.py
+++ b/absl/flags/_defines.py
@@ -33,7 +33,7 @@ from absl.flags import _validators
# pylint: disable=unused-import
try:
- from typing import Text, List, Any
+ from typing import Any, List, Text
except ImportError:
pass This diff is concerning: the two blank lines are permitted for non-definition things, and required for That said, I see a similar diff in pytype that introduces a blank line before a def to change style-nonconformant code to be conforming, so I think it's fine. --- a/absl/flags/_exceptions.py
+++ b/absl/flags/_exceptions.py
@@ -26,7 +26,6 @@ import sys
from absl.flags import _helpers
-
_helpers.disclaim_module_ids.add(id(sys.modules[__name__])) "Wrong" but WAI :)You have no reason to fix this: you can see here they disabled an internal-only lint rule that enforces part of the import style. Naughty devs >:( (In fact, as they're not importing modules, but classes/functions, that's two style guide violations!) --- a/setup.py
+++ b/setup.py
@@ -10,7 +10,8 @@ import re
import shutil
import sys
-from setuptools import setup, Extension # pylint: disable=g-multiple-import
+from setuptools import Extension # pylint: disable=g-multiple-import
+from setuptools import setup |
Thanks so much for this detailed feedback!
Based on the information you provided, I think I'm okay with the current behaviour then. isort does provide ways to change how it defines placement - that can be used separately from profiles (which so far have primarily been for the style - though if there was an easy way to include placement rules that applied widely I would do that as well). In this particular case, it sounds like the diff can be resolved simply by adding
That's right! By default the start of a method or class will cause isort to use 2 lines, and otherwise use 1. So I think this should be safe. Thanks! ~Timothy |
Update: this has just been released to PyPI in 5.6.0 release of isort: https://pycqa.github.io/isort/CHANGELOG/#560-october-7-2020 Thanks! ~Timothy |
This should, eventually (with e.g. instructions in the contribution section, CI, etc.) make it easier to contribute to refex without breaking any style things. It also resolves a bunch of bad imports that are bad for various reasons (e.g. not moving the raw_ast import when it was renamed to ast_matchers, or stuff about whether typing is a third-party library, or...) Pretty much everything looks good. Humongous thanks to @timothycrosley and everyone on [isort issue #1486](PyCQA/isort#1486). This also lets me ignore the internal linter that keeps bugging me about where refex imports go. One would imagine they should go in the first-party section -- internal linter doesn't agree (for reasons described in that issue), but it's hard to standardize on a lint-unfriendly order without some tool like isort to help back me up. PiperOrigin-RevId: 332093853
Ah, you're right! Anyway, I gave it a whirl, and the results are perfect. Thank you! |
This should, eventually (with e.g. instructions in the contribution section, CI, etc.) make it easier to contribute to refex without breaking any style things. It also resolves a bunch of bad imports that are bad for various reasons (e.g. not moving the raw_ast import when it was renamed to ast_matchers, or stuff about whether typing is a third-party library, or...) Pretty much everything looks good. Humongous thanks to @timothycrosley and everyone on [isort issue #1486](PyCQA/isort#1486). This also lets me ignore the internal linter that keeps bugging me about where refex imports go. One would imagine they should go in the first-party section -- internal linter doesn't agree (for reasons described in that issue), but it's hard to standardize on a lint-unfriendly order without some tool like isort to help back me up. PiperOrigin-RevId: 332093853
This should, eventually (with e.g. instructions in the contribution section, CI, etc.) make it easier to contribute to refex without breaking any style things. It also resolves a bunch of bad imports that are bad for various reasons (e.g. not moving the raw_ast import when it was renamed to ast_matchers, or stuff about whether typing is a third-party library, or...) Pretty much everything looks good. Humongous thanks to @timothycrosley and everyone on [isort issue #1486](PyCQA/isort#1486). This also lets me ignore the internal linter that keeps bugging me about where refex imports go. One would imagine they should go in the first-party section -- internal linter doesn't agree (for reasons described in that issue), but it's hard to standardize on a lint-unfriendly order without some tool like isort to help back me up. PiperOrigin-RevId: 336189960
The Google style support added in #969 has a couple of subtle differences from the actual Google style guide:
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:
but isort reorders to place the
cProfile
import beforecollections
: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)
The import sorting sorts by the wrong key. For example, the following is correct Google style:
but isort swaps the order:
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 thea
infrom 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
infrom 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.)
The text was updated successfully, but these errors were encountered: