-
Notifications
You must be signed in to change notification settings - Fork 175
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
Conversation
@@ -16,7 +16,7 @@ | |||
import re | |||
import textwrap | |||
|
|||
from setuptools import setup, find_packages | |||
from setuptools import find_packages, setup |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
073acb7
to
18757d9
Compare
There was a problem hiding this 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
select = | ||
E | ||
W | ||
F | ||
ignore = | ||
W503 # makes Flake8 work like black | ||
W504 | ||
E203 # makes Flake8 work like black | ||
E741 | ||
E501 |
There was a problem hiding this comment.
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
|
||
Following checks are performed: | ||
|
||
- [`flake8`](https://flake8.pycqa.org/en/latest/) for code linting |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
tests/integration/conftest.py
Outdated
user="test_fixture" | ||
) | ||
) | ||
request = TrinoRequest(host=host, port=port, client_session=ClientSession(user="test_fixture")) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
""" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
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
and_( | ||
customers.c.name.like("%12%"), | ||
customers.c.nationkey == 15, | ||
or_(customers.c.mktsegment == "AUTOMOBILE", customers.c.mktsegment == "HOUSEHOLD"), | ||
not_(customers.c.acctbal < 0), |
There was a problem hiding this comment.
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?
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), |
There was a problem hiding this comment.
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.
tests/unit/conftest.py
Outdated
@@ -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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"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", |
tests/unit/oauth_test_utils.py
Outdated
"Www-Authenticate": f'Bearer x_redirect_server="{self.redirect_server}", ' | ||
f'x_token_server="{self.token_server}"', | ||
"Basic realm": '"Trino"', |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
assert ( | ||
str(query) | ||
== 'SELECT "table".id, "table".name \nFROM "table"\nOFFSET :param_1\nLIMIT :param_2' | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
questionable redability
There was a problem hiding this comment.
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.
.pre-commit-config.yaml
Outdated
hooks: | ||
- id: black | ||
args: | ||
- "--line-length=99" |
There was a problem hiding this comment.
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?
5ca3524
to
d681e77
Compare
There was a problem hiding this 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
.pre-commit-config.yaml
Outdated
@@ -4,6 +4,7 @@ repos: | |||
hooks: | |||
- id: "flake8" | |||
name: "Python: analysis" | |||
stages: [manual] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d681e77
to
3ee643e
Compare
tests/integration/conftest.py
Outdated
user="test_fixture" | ||
) | ||
) | ||
request = TrinoRequest(host=host, port=port, client_session=ClientSession(user="test_fixture")) |
There was a problem hiding this comment.
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.
.pre-commit-config.yaml
Outdated
rev: 5.6.4 | ||
hooks: | ||
- id: isort | ||
args: [ "--profile", "black", "--filter-files" ] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
.pre-commit-config.yaml
Outdated
hooks: | ||
- id: black | ||
args: | ||
- "--line-length=120" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
3ee643e
to
e3e83b5
Compare
Some flakyness in the tests, might be related to not having merged #220 but not totally sure. |
@mdesmet See also cffd2b2#r83714277 - not sure if that's the cause. |
e3e83b5
to
4aec2b4
Compare
@hashhar: As discussed I have removed black from this PR and only kept isort. PTAL |
.flake8
Outdated
@@ -0,0 +1,12 @@ | |||
[flake8] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
.github/DEVELOPMENT.md
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
4aec2b4
to
bcadd77
Compare
@hashhar: PTAL |
Introduces isort (sorting imports)
pre-commit run --all-files
to standardize formatting and import sorting.