Skip to content

Commit 32372b2

Browse files
committed
chore: use pre-commit configuration from Flask
1 parent f6cc1f5 commit 32372b2

37 files changed

+73
-106
lines changed

.pre-commit-config.yaml

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
1-
---
21
repos:
3-
- repo: https://github.com/asottile/pyupgrade
4-
rev: v3.13.0
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.6.9
54
hooks:
6-
- id: pyupgrade
7-
args: ["--py39-plus"]
8-
- repo: https://github.com/asottile/reorder_python_imports
9-
rev: v3.11.0
10-
hooks:
11-
- id: reorder-python-imports
12-
args: ["--application-directories", "src"]
13-
- repo: https://github.com/psf/black
14-
rev: 23.9.1
15-
hooks:
16-
- id: black
17-
- repo: https://github.com/pycqa/flake8
18-
rev: 6.1.0
19-
hooks:
20-
- id: flake8
21-
additional_dependencies:
22-
- flake8-bugbear
23-
- flake8-implicit-str-concat
24-
- flake8-pyproject
5+
- id: ruff
6+
- id: ruff-format
257
- repo: https://github.com/pre-commit/pre-commit-hooks
26-
rev: v4.4.0
8+
rev: v5.0.0
279
hooks:
28-
- id: check-byte-order-marker
10+
- id: check-merge-conflict
11+
- id: debug-statements
12+
- id: fix-byte-order-marker
2913
- id: trailing-whitespace
3014
- id: end-of-file-fixer

pyproject.toml

+18-26
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,22 @@ exclude_lines = [
7878
"pass",
7979
]
8080

81-
[tool.flake8]
82-
# B = bugbear
83-
# E = pycodestyle errors
84-
# F = flake8 pyflakes
85-
# W = pycodestyle warnings
86-
# B9 = bugbear opinions
87-
# ISC = implicit-str-concat
88-
select = ["B", "E", "F", "W", "B9", "ISC"]
89-
ignore = [
90-
# slice notation whitespace, invalid
91-
"E203",
92-
# line length, handled by bugbear B950
93-
"E501",
94-
# bare except, handled by bugbear B001
95-
"E722",
96-
# bin op line break, invalid
97-
"W503",
98-
# requires 'strict' argument for 'zip'
99-
# that needs python >= 3.10
100-
"B905",
101-
]
102-
# up to 88 allowed by bugbear B950
103-
max-line-length = 80
104-
per-file-ignores = [
105-
# __init__ modules export names
106-
"**/__init__.py: F401, F403",
81+
[tool.ruff]
82+
src = ["src"]
83+
fix = true
84+
show-fixes = true
85+
output-format = "full"
86+
87+
[tool.ruff.lint]
88+
select = [
89+
"B", # flake8-bugbear
90+
"E", # pycodestyle error
91+
"F", # pyflakes
92+
"I", # isort
93+
"UP", # pyupgrade
94+
"W", # pycodestyle warning
10795
]
96+
97+
[tool.ruff.lint.isort]
98+
force-single-line = true
99+
order-by-type = false

src/wtforms/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from wtforms.form import Form
3636
from wtforms.validators import ValidationError
3737

38-
3938
__version__ = "3.1.2"
4039

4140
__all__ = [

src/wtforms/csrf/session.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
validates with the hmac of the random value + expiration time, and the
1313
expiration time is not passed, the CSRF validation will pass.
1414
"""
15+
1516
import hmac
1617
import os
1718
from datetime import datetime

src/wtforms/fields/core.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ def check_validators(cls, validators):
181181
for validator in validators:
182182
if not callable(validator):
183183
raise TypeError(
184-
"{} is not a valid validator because it is not "
185-
"callable".format(validator)
184+
f"{validator} is not a valid validator because it is not "
185+
"callable"
186186
)
187187

188188
if inspect.isclass(validator):
189189
raise TypeError(
190-
"{} is not a valid validator because it is a class, "
191-
"it should be an instance".format(validator)
190+
f"{validator} is not a valid validator because it is a class, "
191+
"it should be an instance"
192192
)
193193

194194
def gettext(self, string):
@@ -399,8 +399,10 @@ def bind(self, form, name, prefix="", translations=None, **kwargs):
399399
return self.field_class(*self.args, **kw)
400400

401401
def __repr__(self):
402-
return "<UnboundField({}, {!r}, {!r})>".format(
403-
self.field_class.__name__, self.args, self.kwargs
402+
return (
403+
"<UnboundField("
404+
f"{self.field_class.__name__}, {self.args!r}, {self.kwargs!r}"
405+
")>"
404406
)
405407

406408

@@ -425,7 +427,8 @@ def __repr__(self):
425427
for name in dir(self)
426428
if not name.startswith("_")
427429
)
428-
return "<wtforms.fields.Flags: {%s}>" % ", ".join(flags)
430+
flags = ", ".join(flags)
431+
return f"<wtforms.fields.Flags: {{{flags}}}>"
429432

430433

431434
class Label:

src/wtforms/fields/form.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from wtforms.utils import unset_value
2+
13
from .. import widgets
24
from .core import Field
3-
from wtforms.utils import unset_value
45

56
__all__ = ("FormField",)
67

src/wtforms/fields/list.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import itertools
22

3+
from wtforms.utils import unset_value
4+
35
from .. import widgets
46
from .core import Field
57
from .core import UnboundField
6-
from wtforms.utils import unset_value
78

89
__all__ = ("FieldList",)
910

src/wtforms/form.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def process(self, formdata=None, obj=None, data=None, extra_filters=None, **kwar
113113
for name, field in self._fields.items():
114114
field_extra_filters = filters.get(name, [])
115115

116-
inline_filter = getattr(self, "filter_%s" % name, None)
116+
inline_filter = getattr(self, f"filter_{name}", None)
117117
if inline_filter is not None:
118118
field_extra_filters.append(inline_filter)
119119

src/wtforms/utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
22

3-
43
# https://docs.python.org/3/library/datetime.html#technical-detail (see NOTE #9)
54
_DATETIME_STRIP_ZERO_PADDING_FORMATS_RE = re.compile(
65
"%-["

src/wtforms/widgets/core.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def __call__(self, field, **kwargs):
111111
html.append(f"<li>{subfield.label} {subfield()}</li>")
112112
else:
113113
html.append(f"<li>{subfield()} {subfield.label}</li>")
114-
html.append("</%s>" % self.html_tag)
114+
html.append(f"</{self.html_tag}>")
115115
return Markup("".join(html))
116116

117117

@@ -134,15 +134,15 @@ def __call__(self, field, **kwargs):
134134
html = []
135135
if self.with_table_tag:
136136
kwargs.setdefault("id", field.id)
137-
html.append("<table %s>" % html_params(**kwargs))
137+
table_params = html_params(**kwargs)
138+
html.append(f"<table {table_params}>")
138139
hidden = ""
139140
for subfield in field:
140141
if subfield.type in ("HiddenField", "CSRFTokenField"):
141142
hidden += str(subfield)
142143
else:
143144
html.append(
144-
"<tr><th>%s</th><td>%s%s</td></tr>"
145-
% (str(subfield.label), hidden, str(subfield))
145+
f"<tr><th>{subfield.label}</th><td>{hidden}{subfield}</td></tr>"
146146
)
147147
hidden = ""
148148
if self.with_table_tag:
@@ -178,7 +178,8 @@ def __call__(self, field, **kwargs):
178178
for k in dir(flags):
179179
if k in self.validation_attrs and k not in kwargs:
180180
kwargs[k] = getattr(flags, k)
181-
return Markup("<input %s>" % self.html_params(name=field.name, **kwargs))
181+
input_params = self.html_params(name=field.name, **kwargs)
182+
return Markup(f"<input {input_params}>")
182183

183184

184185
class TextInput(Input):
@@ -321,9 +322,10 @@ def __call__(self, field, **kwargs):
321322
for k in dir(flags):
322323
if k in self.validation_attrs and k not in kwargs:
323324
kwargs[k] = getattr(flags, k)
325+
textarea_params = html_params(name=field.name, **kwargs)
326+
textarea_innerhtml = escape(field._value())
324327
return Markup(
325-
"<textarea %s>\r\n%s</textarea>"
326-
% (html_params(name=field.name, **kwargs), escape(field._value()))
328+
f"<textarea {textarea_params}>\r\n{textarea_innerhtml}</textarea>"
327329
)
328330

329331

@@ -356,10 +358,12 @@ def __call__(self, field, **kwargs):
356358
for k in dir(flags):
357359
if k in self.validation_attrs and k not in kwargs:
358360
kwargs[k] = getattr(flags, k)
359-
html = ["<select %s>" % html_params(name=field.name, **kwargs)]
361+
select_params = html_params(name=field.name, **kwargs)
362+
html = [f"<select {select_params}>"]
360363
if field.has_groups():
361364
for group, choices in field.iter_groups():
362-
html.append("<optgroup %s>" % html_params(label=group))
365+
optgroup_params = html_params(label=group)
366+
html.append(f"<optgroup {optgroup_params}>")
363367
for choice in choices:
364368
if len(choice) == 4:
365369
val, label, selected, render_kw = choice

tests/fields/test_boolean.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from tests.common import DummyPostData
2-
32
from wtforms.fields import BooleanField
43
from wtforms.form import Form
54

tests/fields/test_date.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import date
22

33
from tests.common import DummyPostData
4-
54
from wtforms.fields import DateField
65
from wtforms.form import Form
76

tests/fields/test_datetime.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import datetime
22

33
from tests.common import DummyPostData
4-
54
from wtforms.fields import DateTimeField
65
from wtforms.form import Form
76

tests/fields/test_datetimelocal.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import datetime
22

33
from tests.common import DummyPostData
4-
54
from wtforms.fields import DateTimeLocalField
65
from wtforms.form import Form
76

tests/fields/test_decimal.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from decimal import ROUND_UP
44

55
from tests.common import DummyPostData
6-
76
from wtforms.fields import DecimalField
87
from wtforms.form import Form
98

tests/fields/test_field.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from markupsafe import Markup
3-
from tests.common import DummyPostData
43

4+
from tests.common import DummyPostData
55
from wtforms import meta
66
from wtforms import validators
77
from wtforms.fields import Field
@@ -131,9 +131,9 @@ def test_check_validators():
131131

132132
with pytest.raises(
133133
TypeError,
134-
match=r"{} is not a valid validator because "
134+
match=rf"{v2} is not a valid validator because "
135135
"it is a class, it should be an "
136-
"instance".format(v2),
136+
"instance",
137137
):
138138
Field(validators=[v2])
139139

tests/fields/test_file.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from tests.common import DummyPostData
2-
32
from wtforms.fields import FileField
43
from wtforms.fields import MultipleFileField
54
from wtforms.form import Form

tests/fields/test_filters.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from tests.common import DummyPostData
2-
32
from wtforms.fields import StringField
43
from wtforms.form import Form
54

tests/fields/test_flags.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ def test_repr(flags):
3636

3737
def test_underscore_property(flags):
3838
with pytest.raises(AttributeError):
39-
flags._foo
39+
flags._foo # noqa: B018
4040
flags._foo = 42
4141
assert flags._foo == 42

tests/fields/test_float.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from tests.common import DummyPostData
2-
32
from wtforms.fields import FloatField
43
from wtforms.form import Form
54

tests/fields/test_form.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
2-
from tests.common import DummyPostData
32

3+
from tests.common import DummyPostData
44
from wtforms import validators
55
from wtforms.fields import FormField
66
from wtforms.fields import StringField

tests/fields/test_html5.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from decimal import Decimal
44

55
from tests.common import DummyPostData
6-
76
from wtforms import validators
87
from wtforms.fields import DateField
98
from wtforms.fields import DateTimeField
@@ -41,8 +40,8 @@ def _build_value(key, form_input, expected_html, data=unset_value):
4140
if data is unset_value:
4241
data = form_input
4342
if expected_html.startswith("type="):
44-
expected_html = '<input id="{}" name="{}" {} value="{}">'.format(
45-
key, key, expected_html, form_input
43+
expected_html = (
44+
f'<input id="{key}" name="{key}" {expected_html} value="{form_input}">'
4645
)
4746
return {
4847
"key": key,

tests/fields/test_integer.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from tests.common import DummyPostData
2-
32
from wtforms.fields import IntegerField
43
from wtforms.form import Form
54

tests/fields/test_list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from collections import namedtuple
22

33
import pytest
4-
from tests.common import DummyPostData
54

5+
from tests.common import DummyPostData
66
from wtforms import validators
77
from wtforms.fields import FieldList
88
from wtforms.fields import FormField

tests/fields/test_month.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import date
22

33
from tests.common import DummyPostData
4-
54
from wtforms.fields import MonthField
65
from wtforms.form import Form
76

tests/fields/test_radio.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from tests.common import DummyPostData
2-
32
from wtforms import validators
43
from wtforms.fields import RadioField
54
from wtforms.form import Form

tests/fields/test_select.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
2-
from tests.common import DummyPostData
32

3+
from tests.common import DummyPostData
44
from wtforms import validators
55
from wtforms import widgets
66
from wtforms.fields import SelectField

0 commit comments

Comments
 (0)