Skip to content

Commit bb9821b

Browse files
committed
Resolve Ruff SIM114 violations
1 parent 6ffe7be commit bb9821b

25 files changed

Lines changed: 107 additions & 138 deletions

File tree

sphinx/builders/html/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,10 @@ def post_process_images(self, doctree: Node) -> None:
913913
# resizing options are not given. scaled image link is available
914914
# only for resized images.
915915
continue
916-
elif isinstance(node.parent, nodes.reference):
916+
if isinstance(node.parent, nodes.reference):
917917
# A image having hyperlink target
918918
continue
919-
elif 'no-scaled-link' in node['classes']:
919+
if 'no-scaled-link' in node['classes']:
920920
# scaled image link is disabled for this node
921921
continue
922922

@@ -1058,9 +1058,9 @@ def pathto(
10581058
def hasdoc(name: str) -> bool:
10591059
if name in self.env.all_docs:
10601060
return True
1061-
elif name == 'search' and self.search:
1061+
if name == 'search' and self.search:
10621062
return True
1063-
elif name == 'genindex' and self.get_builder_config('use_index', 'html'):
1063+
if name == 'genindex' and self.get_builder_config('use_index', 'html'):
10641064
return True
10651065
return False
10661066
ctx['hasdoc'] = hasdoc

sphinx/builders/latex/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,11 @@ def default_latex_engine(config: Config) -> str:
473473
""" Better default latex_engine settings for specific languages. """
474474
if config.language == 'ja':
475475
return 'uplatex'
476-
elif config.language.startswith('zh'):
476+
if config.language.startswith('zh'):
477477
return 'xelatex'
478-
elif config.language == 'el':
478+
if config.language == 'el':
479479
return 'xelatex'
480-
else:
481-
return 'pdflatex'
480+
return 'pdflatex'
482481

483482

484483
def default_latex_docclass(config: Config) -> dict[str, str]:

sphinx/domains/c.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,11 +1478,7 @@ def describe_signature(self, signode: TextElement, mode: str,
14781478
mainDeclNode['add_permalink'] = not self.symbol.isRedeclaration
14791479
signode += mainDeclNode
14801480

1481-
if self.objectType == 'member':
1482-
pass
1483-
elif self.objectType == 'function':
1484-
pass
1485-
elif self.objectType == 'macro':
1481+
if self.objectType in {'member', 'function', 'macro'}:
14861482
pass
14871483
elif self.objectType == 'struct':
14881484
mainDeclNode += addnodes.desc_sig_keyword('struct', 'struct')

sphinx/domains/cpp.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,9 +4104,7 @@ def describe_signature(self, signode: desc_signature, mode: str,
41044104
elif self.objectType == 'concept':
41054105
mainDeclNode += addnodes.desc_sig_keyword('concept', 'concept')
41064106
mainDeclNode += addnodes.desc_sig_space()
4107-
elif self.objectType == 'member':
4108-
pass
4109-
elif self.objectType == 'function':
4107+
elif self.objectType in {'member', 'function'}:
41104108
pass
41114109
elif self.objectType == 'class':
41124110
assert self.directiveType in ('class', 'struct')

sphinx/domains/python.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,35 @@ def _parse_annotation(annotation: str, env: BuildEnvironment | None) -> list[Nod
133133
def unparse(node: ast.AST) -> list[Node]:
134134
if isinstance(node, ast.Attribute):
135135
return [nodes.Text(f"{unparse(node.value)[0]}.{node.attr}")]
136-
elif isinstance(node, ast.BinOp):
136+
if isinstance(node, ast.BinOp):
137137
result: list[Node] = unparse(node.left)
138138
result.extend(unparse(node.op))
139139
result.extend(unparse(node.right))
140140
return result
141-
elif isinstance(node, ast.BitOr):
141+
if isinstance(node, ast.BitOr):
142142
return [addnodes.desc_sig_space(),
143143
addnodes.desc_sig_punctuation('', '|'),
144144
addnodes.desc_sig_space()]
145-
elif isinstance(node, ast.Constant):
145+
if isinstance(node, ast.Constant):
146146
if node.value is Ellipsis:
147147
return [addnodes.desc_sig_punctuation('', "...")]
148-
elif isinstance(node.value, bool):
148+
if isinstance(node.value, bool):
149149
return [addnodes.desc_sig_keyword('', repr(node.value))]
150-
elif isinstance(node.value, int):
150+
if isinstance(node.value, int):
151151
return [addnodes.desc_sig_literal_number('', repr(node.value))]
152-
elif isinstance(node.value, str):
152+
if isinstance(node.value, str):
153153
return [addnodes.desc_sig_literal_string('', repr(node.value))]
154154
else:
155155
# handles None, which is further handled by type_to_xref later
156156
# and fallback for other types that should be converted
157157
return [nodes.Text(repr(node.value))]
158-
elif isinstance(node, ast.Expr):
158+
if isinstance(node, ast.Expr):
159159
return unparse(node.value)
160-
elif isinstance(node, ast.Index):
160+
if isinstance(node, ast.Index):
161161
return unparse(node.value)
162-
elif isinstance(node, ast.Invert):
162+
if isinstance(node, ast.Invert):
163163
return [addnodes.desc_sig_punctuation('', '~')]
164-
elif isinstance(node, ast.List):
164+
if isinstance(node, ast.List):
165165
result = [addnodes.desc_sig_punctuation('', '[')]
166166
if node.elts:
167167
# check if there are elements in node.elts to only pop the
@@ -175,11 +175,11 @@ def unparse(node: ast.AST) -> list[Node]:
175175
result.pop()
176176
result.append(addnodes.desc_sig_punctuation('', ']'))
177177
return result
178-
elif isinstance(node, ast.Module):
178+
if isinstance(node, ast.Module):
179179
return sum((unparse(e) for e in node.body), [])
180-
elif isinstance(node, ast.Name):
180+
if isinstance(node, ast.Name):
181181
return [nodes.Text(node.id)]
182-
elif isinstance(node, ast.Subscript):
182+
if isinstance(node, ast.Subscript):
183183
if getattr(node.value, 'id', '') in {'Optional', 'Union'}:
184184
return _unparse_pep_604_annotation(node)
185185
result = unparse(node.value)
@@ -193,9 +193,9 @@ def unparse(node: ast.AST) -> list[Node]:
193193
if isinstance(subnode, nodes.Text):
194194
result[i] = nodes.literal('', '', subnode)
195195
return result
196-
elif isinstance(node, ast.UnaryOp):
196+
if isinstance(node, ast.UnaryOp):
197197
return unparse(node.op) + unparse(node.operand)
198-
elif isinstance(node, ast.Tuple):
198+
if isinstance(node, ast.Tuple):
199199
if node.elts:
200200
result = []
201201
for elem in node.elts:
@@ -209,8 +209,7 @@ def unparse(node: ast.AST) -> list[Node]:
209209
addnodes.desc_sig_punctuation('', ')')]
210210

211211
return result
212-
else:
213-
raise SyntaxError # unsupported syntax
212+
raise SyntaxError # unsupported syntax
214213

215214
def _unparse_pep_604_annotation(node: ast.Subscript) -> list[Node]:
216215
subscript = node.slice
@@ -1502,7 +1501,7 @@ def istyping(s: str) -> bool:
15021501
if inspect.isclass(getattr(builtins, reftarget, None)):
15031502
# built-in class
15041503
return contnode
1505-
elif istyping(reftarget):
1504+
if istyping(reftarget):
15061505
# typing class
15071506
return contnode
15081507

sphinx/environment/collectors/asset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def process_doc(self, app: Sphinx, doctree: nodes.document) -> None:
4848
if imguri.startswith('data:'):
4949
candidates['?'] = imguri
5050
continue
51-
elif imguri.find('://') != -1:
51+
if imguri.find('://') != -1:
5252
candidates['?'] = imguri
5353
continue
5454

sphinx/environment/collectors/toctree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _walk_toc(
181181
_walk_toc(subnode, secnums, depth - 1, titlenode)
182182
numstack.pop()
183183
titlenode = None
184-
elif isinstance(subnode, nodes.list_item):
184+
elif isinstance(subnode, nodes.list_item): # NoQA: SIM114
185185
_walk_toc(subnode, secnums, depth, titlenode)
186186
titlenode = None
187187
elif isinstance(subnode, addnodes.only):

sphinx/ext/apidoc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,10 @@ def is_skipped_module(filename: str, opts: Any, excludes: list[str]) -> bool:
187187
if not path.exists(filename):
188188
# skip if the file doesn't exist
189189
return True
190-
elif path.basename(filename).startswith('_') and not opts.includeprivate:
190+
if path.basename(filename).startswith('_') and not opts.includeprivate:
191191
# skip if the module has a "private" name
192192
return True
193-
else:
194-
return False
193+
return False
195194

196195

197196
def walk(rootpath: str, excludes: list[str], opts: Any

sphinx/ext/autodoc/__init__.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,11 @@ def is_filtered_inherited_member(name: str, obj: Any) -> bool:
657657
if cls.__name__ in inherited_members and cls != self.object:
658658
# given member is a member of specified *super class*
659659
return True
660-
elif name in cls.__dict__:
660+
if name in cls.__dict__:
661661
return False
662-
elif name in self.get_attr(cls, '__annotations__', {}):
662+
if name in self.get_attr(cls, '__annotations__', {}):
663663
return False
664-
elif isinstance(obj, ObjectMember) and obj.class_ is cls:
664+
if isinstance(obj, ObjectMember) and obj.class_ is cls:
665665
return False
666666

667667
return False
@@ -681,12 +681,7 @@ def is_filtered_inherited_member(name: str, obj: Any) -> bool:
681681
try:
682682
membername, member = obj
683683
# if isattr is True, the member is documented as an attribute
684-
if member is INSTANCEATTR:
685-
isattr = True
686-
elif (namespace, membername) in attr_docs:
687-
isattr = True
688-
else:
689-
isattr = False
684+
isattr = member is INSTANCEATTR or (namespace, membername) in attr_docs
690685

691686
doc = getdoc(member, self.get_attr, self.config.autodoc_inherit_docstrings,
692687
self.object, membername)
@@ -730,7 +725,7 @@ def is_filtered_inherited_member(name: str, obj: Any) -> bool:
730725
# special __methods__
731726
if (self.options.special_members and
732727
membername in self.options.special_members):
733-
if membername == '__doc__':
728+
if membername == '__doc__': # NoQA: SIM114
734729
keep = False
735730
elif is_filtered_inherited_member(membername, obj):
736731
keep = False
@@ -749,7 +744,7 @@ def is_filtered_inherited_member(name: str, obj: Any) -> bool:
749744
keep = True
750745
elif want_all and isprivate:
751746
if has_doc or self.options.undoc_members:
752-
if self.options.private_members is None:
747+
if self.options.private_members is None: # NoQA: SIM114
753748
keep = False
754749
elif is_filtered_inherited_member(membername, obj):
755750
keep = False
@@ -2407,10 +2402,9 @@ def is_runtime_instance_attribute(self, parent: Any) -> bool:
24072402
# An instance variable defined in __init__().
24082403
if self.get_attribute_comment(parent, self.objpath[-1]): # type: ignore
24092404
return True
2410-
elif self.is_runtime_instance_attribute_not_commented(parent):
2405+
if self.is_runtime_instance_attribute_not_commented(parent):
24112406
return True
2412-
else:
2413-
return False
2407+
return False
24142408

24152409
def is_runtime_instance_attribute_not_commented(self, parent: Any) -> bool:
24162410
"""Check the subject is an attribute defined in __init__() without comment."""
@@ -2546,12 +2540,11 @@ def can_document_member(cls, member: Any, membername: str, isattr: bool, parent:
25462540
) -> bool:
25472541
if isinstance(parent, ModuleDocumenter):
25482542
return False
2549-
elif inspect.isattributedescriptor(member):
2543+
if inspect.isattributedescriptor(member):
25502544
return True
2551-
elif not inspect.isroutine(member) and not isinstance(member, type):
2545+
if not inspect.isroutine(member) and not isinstance(member, type):
25522546
return True
2553-
else:
2554-
return False
2547+
return False
25552548

25562549
def document_members(self, all_members: bool = False) -> None:
25572550
pass

sphinx/ext/autodoc/type_comment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ def not_suppressed(argtypes: list[ast.AST] = []) -> bool:
1919
"""Check given *argtypes* is suppressed type_comment or not."""
2020
if len(argtypes) == 0: # no argtypees
2121
return False
22-
elif len(argtypes) == 1 and ast_unparse(argtypes[0]) == "...": # suppressed
22+
if len(argtypes) == 1 and ast_unparse(argtypes[0]) == "...": # suppressed
2323
# Note: To support multiple versions of python, this uses ``ast_unparse()`` for
2424
# comparison with Ellipsis. Since 3.8, ast.Constant has been used to represent
2525
# Ellipsis node instead of ast.Ellipsis.
2626
return False
27-
else: # not suppressed
28-
return True
27+
# not suppressed
28+
return True
2929

3030

3131
def signature_from_ast(node: ast.FunctionDef, bound_method: bool,

0 commit comments

Comments
 (0)