cleanup: Improve style, formatting; use flake8, isort #254
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a style cleanup, to increase code clarity and consistency, supported by manual inspection,
flake8
, andisort
. Most, but not all, of these changes are to formatting, or to the way imports are sorted and grouped (including removing unused imports and placing groups in the PEP-8 recommended order). I've tried to include only Python code changes that I think can be considered improvements whether or notflake8
andisort
continue to be used (though I recommend them). This fully retainsblack
style, as well as the central role ofblack
as the main tool for enforcing and checking style in the project.This adds
flake8
, which had a configuration insetup.cfg
(and had previously been used with Hound), as well asisort
. It configures them for consistency withblack
and with the overall preexisting style. This includes changing the line length to 88, whichblack
uses. The oldpep8
configuration is removed, becausepep8
was renamed topycodestyle
, and the oldpycodestyle
is removed, because it doesn't need to be run directly, sinceflake8
runs it and passes it an appropriate configuration derived from its own. This sets upflake8
andisort
for local use, but it does not add them to CI.Although
flake8
detects many thingsblack
does not, they partially overlap in their detection of overly long lines. Theblack
rule is more nuanced, rightly skipping over long strings in test data that would be less clear if split across multiple lines. However,flake8
detects comments and docstrings that should be wrapped, so I didn't turn off thatflake8
rule. (Such long strings in tests are the only cases where an inlinenoqa:
suppression was needed.)Besides tool-recommended changes, these other changes are included:
venv
and.venv
to.gitignore
.__all__
above other code (as PEP-8 recommends).__all__
from tuple to list.__all__
can be any sequence of strings, but the other, more prominent__all__
was a list (and using a list is a much more common practice).import X.Y as Y
tofrom X import Y
, which was sometimes also used nearby and which sometimes allowed imports to be consolidated.import X.Y
appeared inX
, changed tofrom X import Y
. (Importing a submodule makes it accessible via its parent, so this has a similar effect. The main relevant differences, in this codebase, are to omit the unnecessaryX.X
bindings, and to make it easier for editors/IDEs and less experienced human programmers to understand what the code is doing.)import X.Y
(outsideX
) intofrom X import Y
and shortened the code usingY
accordingly. (Mostly only when other importsfrom X
were present and being used similarly toY
.)noqa
(and applied tool-recommended fixes) in cases wherenoqa
seems to have been added for expediency or to work around a combination of limitations in the oldflake8
configuration and old behavior ofblack
(rather than to express a preference that the code be kept exactly as it was).Notable omissions:
flake8
andisort
are configured to ignoreversioneer.py
,black
must still be called with--extend-exclude=versioneer.py
. Customizingblack
's configuration would require apyproject.toml
file to be added, which I think would be better done separately from the changes here.flake8
andisort
linting is desired, there are choices about how, and if, adding CI jobs for them should be done, which probably don't need to be considered in order to evaluate the changes in this PR.conftest.py
, since it may make sense to make deprecation-related non-style changes at the same time. This is to avoid an unnecessary conflict with tests: Migrate from pkg_resources to importlib.resources #252.test_regression_143
, the unusedvalidate
variables are kept, because non-stylistic considerations should determine what changes, if any, are to be made there. I've opened cleanup: Remove unused test_regression_143 "validate" mocks #253 separately for that.