Skip to content

Commit 5d26ee1

Browse files
authored
Merge pull request kvesteri#648 from kvesteri/use-pre-commit
Use pre-commit
2 parents 1c3a3a1 + e03646d commit 5d26ee1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+153
-121
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ charset = utf-8
1212
trim_trailing_whitespace = true
1313
insert_final_newline = true
1414
indent_size = 4
15+
16+
[*.yaml]
17+
indent_size = 2

.github/workflows/lint.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
python-version: ['3.6', '3.10']
9+
python-version: ['3.11']
1010
steps:
11-
- uses: actions/checkout@v2
12-
- uses: actions/setup-python@v2
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-python@v4
1313
with:
1414
python-version: ${{ matrix.python-version }}
1515
- name: Install tox
1616
run: pip install tox
1717
- name: Run linting
18-
run: tox -e lint
18+
run: tox -e flake8,isort

.pre-commit-config.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Run `pre-commit autoupdate` to update the hook versions.
2+
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v4.3.0
6+
hooks:
7+
- id: check-yaml
8+
- id: end-of-file-fixer
9+
- id: trailing-whitespace
10+
11+
- repo: https://github.com/PyCQA/flake8
12+
rev: 5.0.4
13+
hooks:
14+
- id: flake8
15+
16+
- repo: https://github.com/pycqa/isort
17+
rev: 5.10.1
18+
hooks:
19+
- id: isort
20+
21+
# Allow tox to run isort as a linter.
22+
- id: isort
23+
alias: isort-check
24+
args: [--check]
25+
stages: [manual]
26+
27+
- repo: https://github.com/asottile/pyupgrade
28+
rev: v3.1.0
29+
hooks:
30+
- id: pyupgrade
31+
args: [--py36-plus]

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Unreleased
88
^^^^^^^^^^
99

1010
- Support Python 3.11.
11+
- Add pre-commit hooks for uniform text checks, isort, flake8, and pyupgrade.
1112
- Fix a crash that occurs if the ``colour-science`` package is installed,
1213
which shares the same import name as the ``colour`` package that sqlalchemy-utils supports.
1314
(`#637 <https://github.com/kvesteri/sqlalchemy-utils/pull/637>`_, courtesy of JayPalm)

conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def mysql_db_user():
7373
@pytest.fixture
7474
def postgresql_dsn(postgresql_db_user, postgresql_db_password, postgresql_db_host,
7575
db_name):
76-
return 'postgresql://{0}:{1}@{2}/{3}'.format(
76+
return 'postgresql://{}:{}@{}/{}'.format(
7777
postgresql_db_user,
7878
postgresql_db_password,
7979
postgresql_db_host,
@@ -83,7 +83,7 @@ def postgresql_dsn(postgresql_db_user, postgresql_db_password, postgresql_db_hos
8383

8484
@pytest.fixture
8585
def mysql_dsn(mysql_db_user, db_name):
86-
return 'mysql+pymysql://{0}@localhost/{1}'.format(mysql_db_user, db_name)
86+
return f'mysql+pymysql://{mysql_db_user}@localhost/{db_name}'
8787

8888

8989
@pytest.fixture
@@ -98,7 +98,7 @@ def sqlite_none_database_dsn():
9898

9999
@pytest.fixture
100100
def sqlite_file_dsn(db_name):
101-
return 'sqlite:///{0}.db'.format(db_name)
101+
return f'sqlite:///{db_name}.db'
102102

103103

104104
@pytest.fixture
@@ -121,7 +121,7 @@ def mssql_db_driver():
121121

122122
@pytest.fixture
123123
def mssql_dsn(mssql_db_user, mssql_db_password, mssql_db_driver, db_name):
124-
return 'mssql+pyodbc://{0}:{1}@localhost/{2}?driver={3}'\
124+
return 'mssql+pyodbc://{}:{}@localhost/{}?driver={}'\
125125
.format(mssql_db_user, mssql_db_password, db_name, mssql_db_driver)
126126

127127

@@ -177,7 +177,7 @@ class Category(Base):
177177

178178
@hybrid_property
179179
def full_name(self):
180-
return '%s %s' % (self.title, self.name)
180+
return f'{self.title} {self.name}'
181181

182182
@full_name.expression
183183
def full_name(self):

sqlalchemy_utils/aggregates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def __init__(
391391
*args,
392392
**kwargs
393393
):
394-
super(AggregatedAttribute, self).__init__(fget, *args, **kwargs)
394+
super().__init__(fget, *args, **kwargs)
395395
self.__doc__ = fget.__doc__
396396
self.column = column
397397
self.relationship = relationship

sqlalchemy_utils/expressions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def compile_array_get(element, compiler, **kw):
2424
raise Exception(
2525
"Second argument should be an integer."
2626
)
27-
return '(%s)[%s]' % (
27+
return '({})[{}]'.format(
2828
compiler.process(args[0]),
2929
sa.text(str(args[1].value + 1))
3030
)
@@ -37,7 +37,7 @@ class row_to_json(GenericFunction):
3737

3838
@compiles(row_to_json, 'postgresql')
3939
def compile_row_to_json(element, compiler, **kw):
40-
return "%s(%s)" % (element.name, compiler.process(element.clauses))
40+
return f"{element.name}({compiler.process(element.clauses)})"
4141

4242

4343
class json_array_length(GenericFunction):
@@ -47,7 +47,7 @@ class json_array_length(GenericFunction):
4747

4848
@compiles(json_array_length, 'postgresql')
4949
def compile_json_array_length(element, compiler, **kw):
50-
return "%s(%s)" % (element.name, compiler.process(element.clauses))
50+
return f"{element.name}({compiler.process(element.clauses)})"
5151

5252

5353
class Asterisk(ColumnElement):

sqlalchemy_utils/functions/database.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def scalar_convert(a):
9393
)
9494
)
9595
elif isinstance(value, str):
96-
return scalar_convert("'{0}'".format(value))
96+
return scalar_convert(f"'{value}'")
9797
elif isinstance(value, Sequence):
9898
return sa.func.json_build_array(
9999
*(
@@ -165,7 +165,7 @@ def scalar_convert(a):
165165
)
166166
)
167167
elif isinstance(value, str):
168-
return scalar_convert("'{0}'".format(value))
168+
return scalar_convert(f"'{value}'")
169169
elif isinstance(value, Sequence):
170170
return sa.func.jsonb_build_array(
171171
*(
@@ -565,7 +565,7 @@ def create_database(url, encoding='utf8', template=None):
565565
if not template:
566566
template = 'template1'
567567

568-
text = "CREATE DATABASE {0} ENCODING '{1}' TEMPLATE {2}".format(
568+
text = "CREATE DATABASE {} ENCODING '{}' TEMPLATE {}".format(
569569
quote(engine, database),
570570
encoding,
571571
quote(engine, template)
@@ -575,7 +575,7 @@ def create_database(url, encoding='utf8', template=None):
575575
connection.execute(sa.text(text))
576576

577577
elif dialect_name == 'mysql':
578-
text = "CREATE DATABASE {0} CHARACTER SET = '{1}'".format(
578+
text = "CREATE DATABASE {} CHARACTER SET = '{}'".format(
579579
quote(engine, database),
580580
encoding
581581
)
@@ -589,7 +589,7 @@ def create_database(url, encoding='utf8', template=None):
589589
connection.execute(sa.text("DROP TABLE DB;"))
590590

591591
else:
592-
text = 'CREATE DATABASE {0}'.format(quote(engine, database))
592+
text = f'CREATE DATABASE {quote(engine, database)}'
593593
with engine.begin() as connection:
594594
connection.execute(sa.text(text))
595595

@@ -642,18 +642,18 @@ def drop_database(url):
642642
'pid' if (version >= (9, 2)) else 'procpid'
643643
)
644644
text = '''
645-
SELECT pg_terminate_backend(pg_stat_activity.%(pid_column)s)
645+
SELECT pg_terminate_backend(pg_stat_activity.{pid_column})
646646
FROM pg_stat_activity
647-
WHERE pg_stat_activity.datname = '%(database)s'
648-
AND %(pid_column)s <> pg_backend_pid();
649-
''' % {'pid_column': pid_column, 'database': database}
647+
WHERE pg_stat_activity.datname = '{database}'
648+
AND {pid_column} <> pg_backend_pid();
649+
'''.format(pid_column=pid_column, database=database)
650650
connection.execute(sa.text(text))
651651

652652
# Drop the database.
653-
text = 'DROP DATABASE {0}'.format(quote(connection, database))
653+
text = f'DROP DATABASE {quote(connection, database)}'
654654
connection.execute(sa.text(text))
655655
else:
656-
text = 'DROP DATABASE {0}'.format(quote(engine, database))
656+
text = f'DROP DATABASE {quote(engine, database)}'
657657
with engine.begin() as connection:
658658
connection.execute(sa.text(text))
659659

sqlalchemy_utils/functions/foreign_keys.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@
1313

1414
def get_foreign_key_values(fk, obj):
1515
mapper = get_mapper(obj)
16-
return dict(
17-
(
18-
fk.constraint.columns.values()[index],
19-
getattr(obj, element.column.key)
20-
if hasattr(obj, element.column.key)
21-
else getattr(
22-
obj, mapper.get_property_by_column(element.column).key
23-
),
16+
return {
17+
fk.constraint.columns.values()[index]:
18+
getattr(obj, element.column.key)
19+
if hasattr(obj, element.column.key)
20+
else getattr(
21+
obj, mapper.get_property_by_column(element.column).key
2422
)
2523
for index, element in enumerate(fk.constraint.elements)
26-
)
24+
}
2725

2826

2927
def group_foreign_keys(foreign_keys):
@@ -177,7 +175,7 @@ class BlogPost(self.Base):
177175
fk.constraint.table.update()
178176
.where(sa.and_(*criteria))
179177
.values(
180-
dict((key.key, value) for key, value in new_values.items())
178+
{key.key: value for key, value in new_values.items()}
181179
)
182180
)
183181
session.execute(query)

sqlalchemy_utils/functions/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def render_literal_value(self, value, type_):
3737
elif isinstance(value, (datetime.date, datetime.datetime)):
3838
return "'%s'" % value
3939

40-
return super(Compiler, self).render_literal_value(
40+
return super().render_literal_value(
4141
value, type_)
4242

4343
text = str(Compiler(engine.dialect, sql).process(sql))

0 commit comments

Comments
 (0)