Skip to content

Commit c8aad1c

Browse files
committed
lint: fix flake8-simplify diagnostics
1 parent 878f758 commit c8aad1c

File tree

10 files changed

+24
-49
lines changed

10 files changed

+24
-49
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ select = [
6969
"I", # -- isort (imports) --
7070
"PLR0402", # -- Name compared with itself like `foo == foo` --
7171
"PT", # -- flake8-pytest-style --
72-
# "SIM", # -- flake8-simplify --
72+
"SIM", # -- flake8-simplify --
7373
"UP015", # -- redundant `open()` mode parameter (like "r" is default) --
7474
"UP018", # -- Unnecessary {literal_type} call like `str("abc")`. (rewrite as a literal) --
7575
"UP032", # -- Use f-string instead of `.format()` call --

src/docx/image/tiff.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,8 @@ def _IfdEntryFactory(stream_rdr, offset):
225225
TIFF_FLD.RATIONAL: _RationalIfdEntry,
226226
}
227227
field_type = stream_rdr.read_short(offset, 2)
228-
if field_type in ifd_entry_classes:
229-
entry_cls = ifd_entry_classes[field_type]
230-
else:
231-
entry_cls = _IfdEntry
232-
return entry_cls.from_stream(stream_rdr, offset)
228+
EntryCls = ifd_entry_classes.get(field_type, _IfdEntry)
229+
return EntryCls.from_stream(stream_rdr, offset)
233230

234231

235232
class _IfdEntry(object):

src/docx/opc/packuri.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# encoding: utf-8
1+
"""Provides the PackURI value type.
22
3-
"""
4-
Provides the PackURI value type along with some useful known pack URI strings
5-
such as PACKAGE_URI.
3+
Also some useful known pack URI strings such as PACKAGE_URI.
64
"""
75

86
import posixpath
@@ -18,7 +16,7 @@ class PackURI(str):
1816
_filename_re = re.compile("([a-zA-Z]+)([1-9][0-9]*)?")
1917

2018
def __new__(cls, pack_uri_str):
21-
if not pack_uri_str[0] == "/":
19+
if pack_uri_str[0] != "/":
2220
tmpl = "PackURI must begin with slash, got '%s'"
2321
raise ValueError(tmpl % pack_uri_str)
2422
return str.__new__(cls, pack_uri_str)
@@ -96,11 +94,7 @@ def relative_ref(self, baseURI):
9694
"""
9795
# workaround for posixpath bug in 2.6, doesn't generate correct
9896
# relative path when *start* (second) parameter is root ('/')
99-
if baseURI == "/":
100-
relpath = self[1:]
101-
else:
102-
relpath = posixpath.relpath(self, baseURI)
103-
return relpath
97+
return self[1:] if baseURI == "/" else posixpath.relpath(self, baseURI)
10498

10599
@property
106100
def rels_uri(self):

src/docx/oxml/document.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,10 @@ def add_section_break(self):
5252
return sentinel_sectPr
5353

5454
def clear_content(self):
55+
"""Remove all content child elements from this <w:body> element.
56+
57+
Leave the <w:sectPr> element if it is present.
5558
"""
56-
Remove all content child elements from this <w:body> element. Leave
57-
the <w:sectPr> element if it is present.
58-
"""
59-
if self.sectPr is not None:
60-
content_elms = self[:-1]
61-
else:
62-
content_elms = self[:]
59+
content_elms = self[:-1] if self.sectPr is not None else self[:]
6360
for content_elm in content_elms:
6461
self.remove(content_elm)

src/docx/oxml/table.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,12 @@ def alignment(self, value):
327327

328328
@property
329329
def autofit(self):
330-
"""
331-
Return |False| if there is a ``<w:tblLayout>`` child with ``w:type``
332-
attribute set to ``'fixed'``. Otherwise return |True|.
330+
"""|False| when there is a `w:tblLayout` child with `@w:type="fixed"`.
331+
332+
Otherwise |True|.
333333
"""
334334
tblLayout = self.tblLayout
335-
if tblLayout is None:
336-
return True
337-
return False if tblLayout.type == "fixed" else True
335+
return True if tblLayout is None else tblLayout.type != "fixed"
338336

339337
@autofit.setter
340338
def autofit(self, value):

src/docx/oxml/xmlchemy.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,8 @@ def get_or_change_to_child(obj):
482482

483483
@property
484484
def _prop_name(self):
485-
"""
486-
Calculate property name from tag name, e.g. a:schemeClr -> schemeClr.
487-
"""
488-
if ":" in self._nsptagname:
489-
start = self._nsptagname.index(":") + 1
490-
else:
491-
start = 0
485+
"""property name computed from tag name, e.g. a:schemeClr -> schemeClr."""
486+
start = self._nsptagname.index(":") + 1 if ":" in self._nsptagname else 0
492487
return self._nsptagname[start:]
493488

494489
@lazyproperty

src/docx/section.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ def _drop_definition(self):
383383
self._document_part.drop_rel(rId)
384384

385385
@property
386-
def _has_definition(self):
386+
def _has_definition(self) -> bool:
387387
"""True if a footer is defined for this section."""
388388
footerReference = self._sectPr.get_footerReference(self._hdrftr_index)
389-
return False if footerReference is None else True
389+
return footerReference is not None
390390

391391
@property
392392
def _prior_headerfooter(self):
@@ -427,10 +427,10 @@ def _drop_definition(self):
427427
self._document_part.drop_header_part(rId)
428428

429429
@property
430-
def _has_definition(self):
430+
def _has_definition(self) -> bool:
431431
"""True if a header is explicitly defined for this section."""
432432
headerReference = self._sectPr.get_headerReference(self._hdrftr_index)
433-
return False if headerReference is None else True
433+
return headerReference is not None
434434

435435
@property
436436
def _prior_headerfooter(self):

src/docx/styles/styles.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ def __contains__(self, name):
2626
Enables `in` operator on style name.
2727
"""
2828
internal_name = BabelFish.ui2internal(name)
29-
for style in self._element.style_lst:
30-
if style.name_val == internal_name:
31-
return True
32-
return False
29+
return any(style.name_val == internal_name for style in self._element.style_lst)
3330

3431
def __getitem__(self, key):
3532
"""

tests/image/test_png.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def it_can_construct_from_a_stream(
171171

172172
def it_provides_access_to_the_IHDR_chunk(self, IHDR_fixture):
173173
chunks, IHDR_chunk_ = IHDR_fixture
174-
assert chunks.IHDR == IHDR_chunk_
174+
assert IHDR_chunk_ == chunks.IHDR
175175

176176
def it_provides_access_to_the_pHYs_chunk(self, pHYs_fixture):
177177
chunks, expected_chunk = pHYs_fixture

tests/styles/test_style.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,7 @@ def next_get_fixture(self, request):
516516
style_elm = styles[style_names.index(style_name)]
517517
next_style_elm = styles[style_names.index(next_style_name)]
518518
style = _ParagraphStyle(style_elm)
519-
if style_name == "H1":
520-
next_style = _ParagraphStyle(next_style_elm)
521-
else:
522-
next_style = style
519+
next_style = _ParagraphStyle(next_style_elm) if style_name == "H1" else style
523520
return style, next_style
524521

525522
@pytest.fixture(

0 commit comments

Comments
 (0)