Skip to content

Commit c41c05a

Browse files
committed
Linting and configuring pre-commit
1 parent 8b87658 commit c41c05a

File tree

10 files changed

+72
-49
lines changed

10 files changed

+72
-49
lines changed

.pre-commit-config.yaml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ repos:
3030
- id: pyupgrade
3131
args: [--py36-plus]
3232

33-
- repo: https://github.com/asottile/add-trailing-comma
34-
rev: v2.0.1
35-
hooks:
36-
- id: add-trailing-comma
33+
#- repo: https://github.com/asottile/add-trailing-comma
34+
# rev: v2.0.1
35+
# hooks:
36+
# - id: add-trailing-comma
3737

38-
- repo: https://gitlab.com/pycqa/flake8
39-
rev: 3.8.2
40-
hooks:
41-
- id: flake8
38+
#- repo: https://gitlab.com/pycqa/flake8
39+
# rev: 3.8.2
40+
# hooks:
41+
# - id: flake8
4242

43-
- repo: https://github.com/pycqa/bandit
44-
rev: 1.6.2
43+
- repo: https://github.com/pre-commit/mirrors-yapf
44+
rev: v0.30.0
4545
hooks:
46-
- id: bandit
46+
- id: yapf
47+
args: [exclude *.pyi]

domdf_python_tools/dates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,5 @@ def check_date(month: Union[str, int], day: int, leap_year: bool = True) -> bool
255255
# stdlib
256256
import warnings
257257
warnings.warn(
258-
"'domdf_python_tools.dates' requires pytz (https://pypi.org/project/pytz/), but it is not installed."
258+
"'domdf_python_tools.dates' requires pytz (https://pypi.org/project/pytz/), but it is not installed.",
259259
)

repo_helper.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ extras_require:
7373

7474
keywords:
7575
- utilities
76+
77+
yapf_exclude:
78+
- "*.pyi"

tests/test_bases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# stdlib
1010
import copy
11-
import pickle
11+
import pickle # nosec: B101
1212
from collections import UserList
1313

1414
# 3rd party
@@ -84,7 +84,7 @@ def test_copy(self, alice):
8484
assert copy.copy(alice) == copy.copy(alice)
8585

8686
def test_pickle(self, alice):
87-
assert pickle.loads(pickle.dumps(alice)) == alice
87+
assert pickle.loads(pickle.dumps(alice)) == alice # nosec: B101
8888

8989
def test_vars(self, alice):
9090
assert vars(alice) == dict(alice)

tests/test_dates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_utc_offset():
4747
def test_converting_timezone():
4848
# No matter what timezone we convert to the timestamp should be the same
4949
for tz in pytz.all_timezones:
50-
assert test_date.astimezone(dates.get_timezone(tz, test_date)
50+
assert test_date.astimezone(dates.get_timezone(tz, test_date),
5151
).timestamp() == test_date.timestamp() == 845173200.0
5252

5353
if dates.get_utc_offset(tz, test_date): # otherwise the timezone stayed as UTC

tests/test_doctools.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,12 @@ def test_decorators():
184184

185185
# set_opening_hours and ceil should have extra text at the beginning
186186
assert SpamCafe.set_opening_hours.__doc__.startswith("\nI will not buy this record, it is scratched.")
187-
assert doctools.deindent_string(SpamCafe.set_opening_hours.__doc__
188-
).endswith(doctools.deindent_string(Cafe.set_opening_hours.__doc__))
187+
assert doctools.deindent_string(SpamCafe.set_opening_hours.__doc__, ).endswith(
188+
doctools.deindent_string(Cafe.set_opening_hours.__doc__)
189+
)
189190
# Dedented both strings to be sure of equivalence
190191
assert SpamCafe.ceil.__doc__.startswith(
191-
"\nI don't know why the cafe has a ceil function, but we'd better document it properly."
192+
"\nI don't know why the cafe has a ceil function, but we'd better document it properly.",
192193
)
193194
assert doctools.deindent_string(SpamCafe.ceil.__doc__).endswith(doctools.deindent_string(math.ceil.__doc__))
194195
# Dedented both strings to be sure of equivalence
@@ -198,10 +199,11 @@ def test_decorators():
198199
assert undocumented_function.__name__ == "undocumented_function"
199200
assert undocumented_function.__annotations__ == {'a': float, 'b': float, 'c': float, 'd': int, 'return': float}
200201
assert partially_documented_function.__doc__.startswith(
201-
"\nThis function works like ``documented_function`` except it returns the result telepathically."
202+
"\nThis function works like ``documented_function`` except it returns the result telepathically.",
203+
)
204+
assert doctools.deindent_string(partially_documented_function.__doc__, ).endswith(
205+
doctools.deindent_string(documented_function.__doc__)
202206
)
203-
assert doctools.deindent_string(partially_documented_function.__doc__
204-
).endswith(doctools.deindent_string(documented_function.__doc__))
205207
# Dedented both strings to be sure of equivalence
206208
assert DummyClass.function_in_class_with_same_args.__doc__ == documented_function.__doc__
207209
assert DummyClass.function_in_class_with_same_args.__name__ == "function_in_class_with_same_args"
@@ -265,7 +267,7 @@ def test_still_callable():
265267
"Lobster Thermidor au Crevette with a Mornay "
266268
"sauce served in a Provencale manner with "
267269
"shallots and aubergines garnished with truffle "
268-
"pate, brandy and with a fried egg on top and spam."
270+
"pate, brandy and with a fried egg on top and spam.",
269271
]
270272
assert spam_cafe.opening_hours == """Open Monday-Saturday 7am - 6pm
271273
Please note our opening hours may vary due to COVID-19"""

tests/test_paths.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_parent_path():
122122
(pathlib.Path("/home/username/Documents/games/chess.py"), "/home/username/Documents/letter.doc"),
123123
(pathlib.Path("/home/username/Documents"), "letter.doc"),
124124
(None, pathlib.Path("/home/username/Documents/letter.doc")),
125-
]
125+
],
126126
)
127127
def test_relpath(relto, relpath):
128128
path = "/home/username/Documents/letter.doc"
@@ -175,12 +175,12 @@ def test_clean_writer():
175175
tempfile = pathlib.Path(tmpdir) / "tmpfile.txt"
176176

177177
test_string = "\n".join([
178-
"Top line",
179-
" ",
180-
"Line with whitespace ",
181-
"Line with tabs ",
182-
"No newline at end of file",
183-
])
178+
"Top line",
179+
" ",
180+
"Line with whitespace ",
181+
"Line with tabs ",
182+
"No newline at end of file",
183+
])
184184

185185
with tempfile.open("w") as fp:
186186
clean_writer(test_string, fp)
@@ -193,12 +193,12 @@ def test_clean_writer():
193193
"""
194194
# Again with lots of newlines
195195
test_string = "\n".join([
196-
"Top line",
197-
" ",
198-
"Line with whitespace ",
199-
"Line with tabs ",
200-
"Too many newlines\n\n\n\n\n\n\n",
201-
])
196+
"Top line",
197+
" ",
198+
"Line with whitespace ",
199+
"Line with tabs ",
200+
"Too many newlines\n\n\n\n\n\n\n",
201+
])
202202

203203
with tempfile.open("w") as fp:
204204
clean_writer(test_string, fp)

tests/test_terminal_colours.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_fore_attributes(obj, expects, capsys):
9191
(Back.LIGHTMAGENTA_EX, "\033[105m"),
9292
(Back.LIGHTCYAN_EX, "\033[106m"),
9393
(Back.LIGHTWHITE_EX, "\033[107m"),
94-
]
94+
],
9595
)
9696
def test_back_attributes(obj, expects, capsys):
9797
assert obj == expects
@@ -114,11 +114,12 @@ def test_back_attributes(obj, expects, capsys):
114114

115115

116116
@pytest.mark.parametrize(
117-
"obj, expects", [
117+
"obj, expects",
118+
[
118119
(Style.DIM, "\033[2m"),
119120
(Style.NORMAL, "\033[22m"),
120121
(Style.BRIGHT, "\033[1m"),
121-
]
122+
],
122123
)
123124
def test_style_attributes(obj, expects, capsys):
124125
assert obj == expects

tests/test_utils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_chunks():
8888
(['a', 'b', 'c'], "a,b,c"),
8989
(['a', 'b', 1, 2], "a,b,1,2"),
9090
(['a', 2, pathlib.Path("foo.txt")], "a,2,foo.txt"),
91-
]
91+
],
9292
)
9393
def test_list2str(value, expects):
9494
str_representation = list2str(value)
@@ -107,7 +107,7 @@ def test_list2str(value, expects):
107107
(['a', 'b', 'c'], "a;b;c"),
108108
(['a', 'b', 1, 2], "a;b;1;2"),
109109
(['a', 2, pathlib.Path("foo.txt")], "a;2;foo.txt"),
110-
]
110+
],
111111
)
112112
def test_list2str_semicolon(value, expects):
113113
str_representation = list2str(value, sep=';')
@@ -207,7 +207,7 @@ def get_mem_addr(obj):
207207
(12.34, "12.34"),
208208
(CustomRepr(), "This is my custom __repr__!"),
209209
(no_repr_instance, f"<tests.test_utils.NoRepr object at {get_mem_addr(no_repr_instance)}>"),
210-
]
210+
],
211211
)
212212
def test_printr(obj, expects, capsys):
213213
utils.printr(obj)
@@ -226,7 +226,7 @@ def test_printr(obj, expects, capsys):
226226
(12.34, "<class 'float'>"),
227227
(CustomRepr(), "<class 'tests.test_utils.CustomRepr'>"),
228228
(no_repr_instance, "<class 'tests.test_utils.NoRepr'>"),
229-
]
229+
],
230230
)
231231
def test_printt(obj, expects, capsys):
232232
utils.printt(obj)
@@ -245,7 +245,7 @@ def test_printt(obj, expects, capsys):
245245
(12.34, "12.34"),
246246
(CustomRepr(), "This is my custom __repr__!"),
247247
(no_repr_instance, f"<tests.test_utils.NoRepr object at {get_mem_addr(no_repr_instance)}>"),
248-
]
248+
],
249249
)
250250
def test_stderr_writer(obj, expects, capsys):
251251
utils.stderr_writer(obj)
@@ -264,7 +264,7 @@ def test_split_len():
264264
[
265265
("1,2,3", (1, 2, 3)), # tests without spaces
266266
("1, 2, 3", (1, 2, 3)), # tests with spaces
267-
]
267+
],
268268
)
269269
def test_str2tuple(value, expects):
270270
assert isinstance(str2tuple(value), tuple)
@@ -276,7 +276,7 @@ def test_str2tuple(value, expects):
276276
[
277277
("1;2;3", (1, 2, 3)), # tests without semicolon
278278
("1; 2; 3", (1, 2, 3)), # tests with semicolon
279-
]
279+
],
280280
)
281281
def test_str2tuple_semicolon(value, expects):
282282
assert isinstance(str2tuple(value, sep=';'), tuple)
@@ -316,7 +316,7 @@ def test_str2tuple_semicolon(value, expects):
316316
("oFF", False),
317317
('0', False),
318318
(0, False),
319-
]
319+
],
320320
)
321321
def test_strtobool(obj, expects):
322322
assert utils.strtobool(obj) == expects
@@ -331,7 +331,7 @@ def test_strtobool(obj, expects):
331331
(None, AttributeError),
332332
(1.0, AttributeError),
333333
(0.0, AttributeError),
334-
]
334+
],
335335
)
336336
def test_strtobool_errors(obj, expects):
337337
with pytest.raises(expects):
@@ -357,7 +357,7 @@ def test_strtobool_errors(obj, expects):
357357
("False", "False"),
358358
("false", "'false'"),
359359
("Hello World", "'Hello World'"),
360-
]
360+
],
361361
)
362362
def test_enquote_value(obj, expects):
363363
assert utils.enquote_value(obj) == expects

tox.ini

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ changedir = {toxinidir}
9595
skip_install = true
9696
ignore_errors = true
9797
deps = yapf
98-
commands = yapf -i --recursive domdf_python_tools tests
98+
commands = yapf -i --recursive domdf_python_tools tests --exclude "*.pyi"
9999

100100

101101
[testenv:isort]
@@ -119,6 +119,22 @@ deps =
119119
commands = mypy domdf_python_tools tests
120120

121121

122+
123+
[testenv:bandit]
124+
basepython = python3.6
125+
126+
ignore_errors = true
127+
changedir = {toxinidir}
128+
deps =
129+
bandit
130+
whitelist_externals = /usr/bin/printf
131+
commands =
132+
printf "===== Running Bandit on domdf_python_tools ====="
133+
bandit domdf_python_tools -r
134+
printf "===== Running Bandit on tests ====="
135+
bandit tests -r -s B101
136+
137+
122138
[testenv:pyup]
123139
basepython = python3.6
124140

0 commit comments

Comments
 (0)