Skip to content

Commit 878f758

Browse files
committed
lint: fix flake8-pytest-style diagnostics
1 parent d509784 commit 878f758

18 files changed

+62
-67
lines changed

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ python_functions = ["it_", "they_", "and_it_", "but_it_"]
5858
exclude = []
5959
ignore = [
6060
"COM812", # -- over-aggressively insists on trailing commas where not desired --
61+
"PT001", # -- wants @pytest.fixture() instead of @pytest.fixture --
62+
"PT005", # -- wants @pytest.fixture() instead of @pytest.fixture --
6163
]
6264
select = [
6365
"C4", # -- flake8-comprehensions --
@@ -66,7 +68,7 @@ select = [
6668
"F", # -- pyflakes --
6769
"I", # -- isort (imports) --
6870
"PLR0402", # -- Name compared with itself like `foo == foo` --
69-
# "PT", # -- flake8-pytest-style --
71+
"PT", # -- flake8-pytest-style --
7072
# "SIM", # -- flake8-simplify --
7173
"UP015", # -- redundant `open()` mode parameter (like "r" is default) --
7274
"UP018", # -- Unnecessary {literal_type} call like `str("abc")`. (rewrite as a literal) --

tests/opc/test_package.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,16 @@ def part_related_by_(self, request):
233233
return method_mock(request, OpcPackage, "part_related_by")
234234

235235
@pytest.fixture
236-
def parts(self, request, parts_):
236+
def parts(self, parts_):
237237
"""
238238
Return a mock patching property OpcPackage.parts, reversing the
239239
patch after each use.
240240
"""
241-
_patch = patch.object(
241+
p = patch.object(
242242
OpcPackage, "parts", new_callable=PropertyMock, return_value=parts_
243243
)
244-
request.addfinalizer(_patch.stop)
245-
return _patch.start()
244+
yield p.start()
245+
p.stop()
246246

247247
@pytest.fixture
248248
def parts_(self, request):

tests/opc/test_packuri.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def it_can_construct_from_relative_ref(self):
2929
assert pack_uri == "/ppt/slideLayouts/slideLayout1.xml"
3030

3131
def it_should_raise_on_construct_with_bad_pack_uri_str(self):
32-
with pytest.raises(ValueError):
32+
with pytest.raises(ValueError, match="PackURI must begin with slash"):
3333
PackURI("foobar")
3434

3535
def it_can_calculate_baseURI(self):

tests/opc/test_part.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,24 +326,20 @@ def cls_method_fn_(self, request, cls_selector_fn_):
326326
@pytest.fixture
327327
def cls_selector_fixture(
328328
self,
329-
request,
330329
cls_selector_fn_,
331330
cls_method_fn_,
332331
part_load_params,
333332
CustomPartClass_,
334333
part_of_custom_type_,
335334
):
336-
def reset_part_class_selector():
337-
PartFactory.part_class_selector = original_part_class_selector
338-
339335
original_part_class_selector = PartFactory.part_class_selector
340-
request.addfinalizer(reset_part_class_selector)
341-
return (
336+
yield (
342337
cls_selector_fn_,
343338
part_load_params,
344339
CustomPartClass_,
345340
part_of_custom_type_,
346341
)
342+
PartFactory.part_class_selector = original_part_class_selector
347343

348344
@pytest.fixture
349345
def cls_selector_fn_(self, request, CustomPartClass_):

tests/opc/test_phys_pkg.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
"""Test suite for docx.opc.phys_pkg module."""
22

3-
try:
4-
from io import BytesIO # Python 3
5-
except ImportError:
6-
from StringIO import StringIO as BytesIO
7-
83
import hashlib
4+
import io
95
from zipfile import ZIP_DEFLATED, ZipFile
106

117
import pytest
@@ -119,10 +115,10 @@ def it_returns_none_when_part_has_no_rels_xml(self, phys_reader):
119115
# fixtures ---------------------------------------------
120116

121117
@pytest.fixture(scope="class")
122-
def phys_reader(self, request):
118+
def phys_reader(self):
123119
phys_reader = _ZipPkgReader(zip_pkg_path)
124-
request.addfinalizer(phys_reader.close)
125-
return phys_reader
120+
yield phys_reader
121+
phys_reader.close()
126122

127123
@pytest.fixture
128124
def pkg_file_(self, request):
@@ -167,10 +163,10 @@ def it_can_write_a_blob(self, pkg_file):
167163
# fixtures ---------------------------------------------
168164

169165
@pytest.fixture
170-
def pkg_file(self, request):
171-
pkg_file = BytesIO()
172-
request.addfinalizer(pkg_file.close)
173-
return pkg_file
166+
def pkg_file(self):
167+
pkg_file = io.BytesIO()
168+
yield pkg_file
169+
pkg_file.close()
174170

175171

176172
# fixtures -------------------------------------------------

tests/opc/test_pkgreader.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,10 @@ def partnames_(self, request):
231231
return partname_, partname_2_
232232

233233
@pytest.fixture
234-
def PhysPkgReader_(self, request):
235-
_patch = patch("docx.opc.pkgreader.PhysPkgReader", spec_set=_ZipPkgReader)
236-
request.addfinalizer(_patch.stop)
237-
return _patch.start()
234+
def PhysPkgReader_(self):
235+
p = patch("docx.opc.pkgreader.PhysPkgReader", spec_set=_ZipPkgReader)
236+
yield p.start()
237+
p.stop()
238238

239239
@pytest.fixture
240240
def reltypes_(self, request):
@@ -464,7 +464,7 @@ def it_raises_on_target_partname_when_external(self):
464464
target_mode=RTM.EXTERNAL,
465465
)
466466
srel = _SerializedRelationship("/", rel_elm)
467-
with pytest.raises(ValueError):
467+
with pytest.raises(ValueError, match="target_partname attribute on Relat"):
468468
srel.target_partname
469469

470470

tests/opc/test_pkgwriter.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ def parts_(self, request):
9292
return instance_mock(request, list)
9393

9494
@pytest.fixture
95-
def PhysPkgWriter_(self, request):
96-
_patch = patch("docx.opc.pkgwriter.PhysPkgWriter")
97-
request.addfinalizer(_patch.stop)
98-
return _patch.start()
95+
def PhysPkgWriter_(self):
96+
p = patch("docx.opc.pkgwriter.PhysPkgWriter")
97+
yield p.start()
98+
p.stop()
9999

100100
@pytest.fixture
101101
def phys_pkg_writer_(self, request):
@@ -106,7 +106,7 @@ def write_cti_fixture(self, _ContentTypesItem_, parts_, phys_pkg_writer_, blob_)
106106
return _ContentTypesItem_, parts_, phys_pkg_writer_, blob_
107107

108108
@pytest.fixture
109-
def _write_methods(self, request):
109+
def _write_methods(self):
110110
"""Mock that patches all the _write_* methods of PackageWriter"""
111111
root_mock = Mock(name="PackageWriter")
112112
patch1 = patch.object(PackageWriter, "_write_content_types_stream")
@@ -116,13 +116,11 @@ def _write_methods(self, request):
116116
root_mock.attach_mock(patch2.start(), "_write_pkg_rels")
117117
root_mock.attach_mock(patch3.start(), "_write_parts")
118118

119-
def fin():
120-
patch1.stop()
121-
patch2.stop()
122-
patch3.stop()
119+
yield root_mock
123120

124-
request.addfinalizer(fin)
125-
return root_mock
121+
patch1.stop()
122+
patch2.stop()
123+
patch3.stop()
126124

127125
@pytest.fixture
128126
def xml_for_(self, request):

tests/opc/test_rel.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# pyright: reportPrivateUsage=false
2+
13
"""Unit test suite for the docx.opc.rel module."""
24

35
import pytest
@@ -27,7 +29,7 @@ def it_remembers_construction_values(self):
2729

2830
def it_should_raise_on_target_part_access_on_external_rel(self):
2931
rel = _Relationship(None, None, None, None, external=True)
30-
with pytest.raises(ValueError):
32+
with pytest.raises(ValueError, match="target_part property on _Relat"):
3133
rel.target_part
3234

3335
def it_should_have_target_ref_for_external_rel(self):
@@ -172,7 +174,7 @@ def rels(self):
172174
return rels
173175

174176
@pytest.fixture
175-
def rels_elm(self, request):
177+
def rels_elm(self):
176178
"""
177179
Return a rels_elm mock that will be returned from
178180
CT_Relationships.new()
@@ -186,8 +188,8 @@ def rels_elm(self, request):
186188
# patch CT_Relationships to return that rels_elm
187189
patch_ = patch.object(CT_Relationships, "new", return_value=rels_elm)
188190
patch_.start()
189-
request.addfinalizer(patch_.stop)
190-
return rels_elm
191+
yield rels_elm
192+
patch_.stop()
191193

192194
@pytest.fixture
193195
def _rel_with_known_target_part(self, _rId, reltype, _target_part, _baseURI):

tests/oxml/test__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def it_accepts_unicode_providing_there_is_no_encoding_declaration(self):
6363
parse_xml(xml_text)
6464
# but adding encoding in the declaration raises ValueError
6565
xml_text = "%s\n%s" % (enc_decl, xml_body)
66-
with pytest.raises(ValueError):
66+
with pytest.raises(ValueError, match="Unicode strings with encoding declara"):
6767
parse_xml(xml_text)
6868

6969
def it_uses_registered_element_classes(self, xml_bytes):

tests/oxml/test_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def it_can_add_a_trPr(self, add_trPr_fixture):
1919

2020
def it_raises_on_tc_at_grid_col(self, tc_raise_fixture):
2121
tr, idx = tc_raise_fixture
22-
with pytest.raises(ValueError):
22+
with pytest.raises(ValueError): # noqa: PT011
2323
tr.tc_at_grid_col(idx)
2424

2525
# fixtures -------------------------------------------------------
@@ -123,7 +123,7 @@ def it_can_move_its_content_to_help_merge(self, move_fixture):
123123

124124
def it_raises_on_tr_above(self, tr_above_raise_fixture):
125125
tc = tr_above_raise_fixture
126-
with pytest.raises(ValueError):
126+
with pytest.raises(ValueError, match="no tr above topmost tr"):
127127
tc._tr_above
128128

129129
# fixtures -------------------------------------------------------

0 commit comments

Comments
 (0)