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

Add isort (for sorting imports) #227

Merged
merged 3 commits into from
Oct 31, 2022

Conversation

mdesmet
Copy link
Contributor

@mdesmet mdesmet commented Aug 19, 2022

Introduces isort (sorting imports)

  • Added isort as "test" dependencies
  • Run pre-commit run --all-files to standardize formatting and import sorting.

@cla-bot cla-bot bot added the cla-signed label Aug 19, 2022
@@ -16,7 +16,7 @@
import re
import textwrap

from setuptools import setup, find_packages
from setuptools import find_packages, setup
Copy link
Member

Choose a reason for hiding this comment

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

Formatting changes should be in a separate commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A separate commit would fail the build if ran by itself.

Copy link
Member

Choose a reason for hiding this comment

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

Why? Make it the first commit.

Copy link
Member

@hashhar hashhar left a comment

Choose a reason for hiding this comment

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

Reformatting all in one go loses quite a lot of history.

+1 to adding black and isort though.

I'll defer to others on suggestions.

.flake8 Outdated
Comment on lines 2 to 11
select =
E
W
F
ignore =
W503 # makes Flake8 work like black
W504
E203 # makes Flake8 work like black
E741
E501
Copy link
Member

Choose a reason for hiding this comment

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

ideally each line should have comments to explain what these do

README.md Outdated Show resolved Hide resolved
README.md Outdated

Following checks are performed:

- [`flake8`](https://flake8.pycqa.org/en/latest/) for code linting
Copy link
Member

Choose a reason for hiding this comment

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

linting should run on CI for now. We don't want to prevent from submitting PRs or pushing changes just when experimenting or when one needs help to fix whatever is being complained about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That can be disabled by git commit . -m 'quick fix' --no-verify. We can add this to the docs as well.

Note that in general only mypy might stop you. black and isort just autoformat your files.

IMHO in general it will be better to always run it on commit to prevent contributors from the experience of failing the build for easily fixable problems.

WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

I agreed with black and isort. I just meant to keep flake8 and mypy outside of pre-commit for now. We can iterate.

README.md Outdated
- [`flake8`](https://flake8.pycqa.org/en/latest/) for code linting
- [`black`](https://github.com/psf/black) for code formatting
- [`isort`](https://pycqa.github.io/isort/) for sorting imports
- [`mypy`](https://mypy.readthedocs.io/en/stable/) for static type checking
Copy link
Member

Choose a reason for hiding this comment

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

mypy on CI only for now as well.

user="test_fixture"
)
)
request = TrinoRequest(host=host, port=port, client_session=ClientSession(user="test_fixture"))
Copy link
Member

Choose a reason for hiding this comment

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

arguably less readable than before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems linked to the setting of the line length. It seems black wants to shove as much on the same line if permitted by the line length. No other knobs available. black is fairly opinionated.

Copy link
Contributor

@john-bodley john-bodley Aug 31, 2022

Choose a reason for hiding this comment

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

@mdesmet if you add a trailing comma after client_session=ClientSession(user="test_fixture"), Black will format the code is a similar way to how it was formatted previously.

Comment on lines 900 to 889
"""
)
Copy link
Member

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to one liner with single quotes

Comment on lines 181 to 184
and_(
customers.c.name.like("%12%"),
customers.c.nationkey == 15,
or_(customers.c.mktsegment == "AUTOMOBILE", customers.c.mktsegment == "HOUSEHOLD"),
not_(customers.c.acctbal < 0),
Copy link
Member

Choose a reason for hiding this comment

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

Does black reformat this away?

Suggested change
and_(
customers.c.name.like("%12%"),
customers.c.nationkey == 15,
or_(customers.c.mktsegment == "AUTOMOBILE", customers.c.mktsegment == "HOUSEHOLD"),
not_(customers.c.acctbal < 0),
and_(
customers.c.name.like("%12%"),
customers.c.nationkey == 15,
or_(customers.c.mktsegment == "AUTOMOBILE", customers.c.mktsegment == "HOUSEHOLD"),
not_(customers.c.acctbal < 0),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually the _or(...) is an argument to the _and function, so this seems correct.

@@ -194,8 +195,7 @@ def sample_get_error_response_data():
"errorType": "USER_ERROR",
"failureInfo": {
"errorLocation": {"columnNumber": 15, "lineNumber": 1},
"message": "line 1:15: Schema must be specified "
"when session schema is not set",
"message": "line 1:15: Schema must be specified " "when session schema is not set",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"message": "line 1:15: Schema must be specified " "when session schema is not set",
"message": "line 1:15: Schema must be specified when session schema is not set",

Comment on lines 51 to 53
"Www-Authenticate": f'Bearer x_redirect_server="{self.redirect_server}", '
f'x_token_server="{self.token_server}"',
"Basic realm": '"Trino"',
Copy link
Member

Choose a reason for hiding this comment

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

More readable and "safer" to have entire token on one line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed by increasing line length.

Comment on lines 38 to 41
assert (
str(query)
== 'SELECT "table".id, "table".name \nFROM "table"\nOFFSET :param_1\nLIMIT :param_2'
)
Copy link
Member

Choose a reason for hiding this comment

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

questionable redability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A multi line string would be more fitting here. However that triggers flake8 as it seems flake8 doesn't allow spaces at EOL, even within multi line strings.

hooks:
- id: black
args:
- "--line-length=99"
Copy link
Member

@hashhar hashhar Aug 19, 2022

Choose a reason for hiding this comment

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

enforcing a specific line length can hinder readability - line length limits don't provide any benefits IMO and sometimes discourage from good naming or formatting SQL queries in an idiomatic way.

Can we make this length infinite?

@mdesmet mdesmet force-pushed the feature/auto-formatting branch 3 times, most recently from 5ca3524 to d681e77 Compare August 22, 2022 09:55
Copy link
Member

@hovaesco hovaesco left a comment

Choose a reason for hiding this comment

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

LGTM % black complains in CI

@@ -4,6 +4,7 @@ repos:
hooks:
- id: "flake8"
name: "Python: analysis"
stages: [manual]
Copy link
Member

Choose a reason for hiding this comment

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

all might be more descriptive, since it runs all checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually we want to restrict flake8 and mypy to not be run on any stage except in CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

user="test_fixture"
)
)
request = TrinoRequest(host=host, port=port, client_session=ClientSession(user="test_fixture"))
Copy link
Contributor

@john-bodley john-bodley Aug 31, 2022

Choose a reason for hiding this comment

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

@mdesmet if you add a trailing comma after client_session=ClientSession(user="test_fixture"), Black will format the code is a similar way to how it was formatted previously.

rev: 5.6.4
hooks:
- id: isort
args: [ "--profile", "black", "--filter-files" ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are these args required? I've used the isort pre-commit successfully with a number of projects which haven't required any additional arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The filter-files can be omitted. I think it's just there to not change any unmodified files, even if explicitly asked.

The --profile "black" actually formats the imports differently

with the black profile

from trino.exceptions import (
    DatabaseError,
    DataError,
    Error,
    IntegrityError,
    InterfaceError,
    InternalError,
    NotSupportedError,
    OperationalError,
    ProgrammingError,
    Warning,
)

without the profile

from trino.exceptions import (DatabaseError, DataError, Error, IntegrityError,
                              InterfaceError, InternalError, NotSupportedError,
                              OperationalError, ProgrammingError, Warning)

I personally like the black profile more as it is faster to scan.

hooks:
- id: black
args:
- "--line-length=120"
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to override the default 88 character line length? I realize it's somewhat arbitrary what the line length is if you have an auto-formatter, but as far as I can tell most packages adhere to the default.

Copy link
Member

Choose a reason for hiding this comment

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

Because line length doesn't correlate to any reasonable thing. It's an arbitrary rule to be followed and if you'd take a look at the reformats black did with shorter line lengths have questionable readability.

@mdesmet
Copy link
Contributor Author

mdesmet commented Sep 1, 2022

Some flakyness in the tests, might be related to not having merged #220 but not totally sure.

@hashhar
Copy link
Member

hashhar commented Sep 12, 2022

@mdesmet See also cffd2b2#r83714277 - not sure if that's the cause.

@mdesmet mdesmet changed the title Added black (code formatting) and isort (sorting imports) Add isort (for sorting imports) Oct 27, 2022
@mdesmet
Copy link
Contributor Author

mdesmet commented Oct 27, 2022

@hashhar: As discussed I have removed black from this PR and only kept isort. PTAL

.flake8 Outdated
@@ -0,0 +1,12 @@
[flake8]
Copy link
Member

Choose a reason for hiding this comment

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

intended to be part of this PR or leftover? I think leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

@@ -25,6 +25,18 @@ With `-e` passed to `pip install` above pip can reference the code you are
modifying in the *virtual env*. That way, you do not need to run `pip install`
again to make your changes applied to the *virtual env*.

### `pre-commit` checks
Copy link
Member

Choose a reason for hiding this comment

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

What exact checks are run will change over time - doesn't seem as important.

Maybe we'd want to instead mention about pre-commit install at Code style header which already tells how to run these checks manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@mdesmet
Copy link
Contributor Author

mdesmet commented Oct 31, 2022

@hashhar: PTAL

@hashhar hashhar added the no-release-notes This pull request does not require release notes entry label Oct 31, 2022
@hashhar hashhar merged commit 0399b86 into trinodb:master Oct 31, 2022
@mdesmet mdesmet deleted the feature/auto-formatting branch October 31, 2022 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed no-release-notes This pull request does not require release notes entry
Development

Successfully merging this pull request may close these issues.

5 participants