Skip to content

Commit

Permalink
Enable SIM105 (use contextlib.suppress)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Aug 13, 2023
1 parent 3a5e1f1 commit 6cb783c
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 58 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ ignore = [
# flake8-simplify
"SIM102", # nested 'if' statements
"SIM103", # return condition directly
"SIM105", # use contextlib.suppress
"SIM108", # use ternary operator
# flake8-self
"SLF001", # private member accessed
Expand Down
12 changes: 5 additions & 7 deletions sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import contextlib
import hashlib
import html
import os
Expand Down Expand Up @@ -578,12 +579,11 @@ def get_doc_context(self, docname: str, body: str, metatags: str) -> dict[str, A
# that gracefully
prev = None
while related and related[0]:
try:
with contextlib.suppress(KeyError):
parents.append(
{'link': self.get_relative_uri(docname, related[0]),
'title': self.render_partial(titles[related[0]])['title']})
except KeyError:
pass

related = self.relations.get(related[0])
if parents:
# remove link to the master file; we have a generic
Expand Down Expand Up @@ -1102,7 +1102,7 @@ def js_tag(js: _JavaScript | str) -> str:
templatename = newtmpl

# sort JS/CSS before rendering HTML
try:
try: # NoQA: SIM105
# Convert script_files to list to support non-list script_files (refs: #8889)
ctx['script_files'] = sorted(ctx['script_files'], key=lambda js: js.priority)
except AttributeError:
Expand All @@ -1112,10 +1112,8 @@ def js_tag(js: _JavaScript | str) -> str:
# Note: priority sorting feature will not work in this case.
pass

try:
with contextlib.suppress(AttributeError):
ctx['css_files'] = sorted(ctx['css_files'], key=lambda css: css.priority)
except AttributeError:
pass

try:
output = self.templates.render(templatename, ctx)
Expand Down
5 changes: 2 additions & 3 deletions sphinx/builders/linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import contextlib
import json
import re
import socket
Expand Down Expand Up @@ -304,14 +305,12 @@ def run(self) -> None:
break

netloc = urlsplit(uri).netloc
try:
with contextlib.suppress(KeyError):
# Refresh rate limit.
# When there are many links in the queue, workers are all stuck waiting
# for responses, but the builder keeps queuing. Links in the queue may
# have been queued before rate limits were discovered.
next_check = self.rate_limits[netloc].next_check
except KeyError:
pass
if next_check > time.time():
# Sleep before putting message back in the queue to avoid
# waking up other threads.
Expand Down
6 changes: 3 additions & 3 deletions sphinx/cmd/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import argparse
import bdb
import contextlib
import locale
import multiprocessing
import os
Expand Down Expand Up @@ -265,10 +266,9 @@ def _parse_arguments(argv: list[str] = sys.argv[1:]) -> argparse.Namespace:
key, val = val.split('=')
except ValueError:
parser.error(__('-A option argument must be in the form name=value'))
try:
with contextlib.suppress(ValueError):
val = int(val)
except ValueError:
pass

confoverrides['html_context.%s' % key] = val

if args.nitpicky:
Expand Down
6 changes: 3 additions & 3 deletions sphinx/domains/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import contextlib
from typing import TYPE_CHECKING, Any, cast

from docutils import nodes
Expand Down Expand Up @@ -218,10 +219,9 @@ def after_content(self) -> None:
"""
objects = self.env.ref_context.setdefault('js:objects', [])
if self.allow_nesting:
try:
with contextlib.suppress(IndexError):
objects.pop()
except IndexError:
pass

self.env.ref_context['js:object'] = (objects[-1] if len(objects) > 0
else None)

Expand Down
6 changes: 3 additions & 3 deletions sphinx/domains/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import ast
import builtins
import contextlib
import inspect
import re
import token
Expand Down Expand Up @@ -912,10 +913,9 @@ def after_content(self) -> None:
"""
classes = self.env.ref_context.setdefault('py:classes', [])
if self.allow_nesting:
try:
with contextlib.suppress(IndexError):
classes.pop()
except IndexError:
pass

self.env.ref_context['py:class'] = (classes[-1] if len(classes) > 0
else None)
if 'module' in self.options:
Expand Down
10 changes: 5 additions & 5 deletions sphinx/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations

import contextlib
from collections import defaultdict
from operator import attrgetter
from typing import TYPE_CHECKING, Any, Callable, NamedTuple
Expand Down Expand Up @@ -82,12 +83,11 @@ def disconnect(self, listener_id: int) -> None:
def emit(self, name: str, *args: Any,
allowed_exceptions: tuple[type[Exception], ...] = ()) -> list:
"""Emit a Sphinx event."""
try:

# not every object likes to be repr()'d (think
# random stuff coming via autodoc)
with contextlib.suppress(Exception):
logger.debug('[app] emitting event: %r%s', name, repr(args)[:100])
except Exception:
# not every object likes to be repr()'d (think
# random stuff coming via autodoc)
pass

results = []
listeners = sorted(self.listeners[name], key=attrgetter("priority"))
Expand Down
5 changes: 2 additions & 3 deletions sphinx/ext/githubpages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import contextlib
import os
import urllib.parse
from typing import TYPE_CHECKING, Any
Expand Down Expand Up @@ -47,10 +48,8 @@ def create_nojekyll_and_cname(app: Sphinx, env: BuildEnvironment) -> None:
# auto-generated by the GitHub UI doesn't have one.
f.write(domain)
else:
try:
with contextlib.suppress(FileNotFoundError):
os.unlink(cname_path)
except FileNotFoundError:
pass


def setup(app: Sphinx) -> dict[str, Any]:
Expand Down
9 changes: 3 additions & 6 deletions sphinx/ext/imgmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import base64
import contextlib
import re
import shutil
import subprocess
Expand Down Expand Up @@ -298,18 +299,14 @@ def clean_up_files(app: Sphinx, exc: Exception) -> None:
return

if hasattr(app.builder, '_imgmath_tempdir'):
try:
with contextlib.suppress(Exception):
shutil.rmtree(app.builder._imgmath_tempdir)
except Exception:
pass

if app.builder.config.imgmath_embed:
# in embed mode, the images are still generated in the math output dir
# to be shared across workers, but are not useful to the final document
try:
with contextlib.suppress(Exception):
shutil.rmtree(path.join(app.builder.outdir, app.builder.imagedir, 'math'))
except Exception:
pass


def get_tooltip(self: HTML5Translator, node: Element) -> str:
Expand Down
6 changes: 3 additions & 3 deletions sphinx/ext/napoleon/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import collections
import contextlib
import inspect
import re
from functools import partial
Expand Down Expand Up @@ -609,10 +610,9 @@ def _parse(self) -> None:

if self._name and self._what in ('attribute', 'data', 'property'):
res: list[str] = []
try:
with contextlib.suppress(StopIteration):
res = self._parse_attribute_docstring()
except StopIteration:
pass

self._parsed_lines.extend(res)
return

Expand Down
6 changes: 3 additions & 3 deletions sphinx/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import contextlib
import os
from glob import glob
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -84,10 +85,9 @@ def path2doc(self, filename: str | os.PathLike[str]) -> str | None:
return self._path_to_docname[filename] # type: ignore[index]
except KeyError:
if os.path.isabs(filename):
try:
with contextlib.suppress(ValueError):
filename = os.path.relpath(filename, self.srcdir)
except ValueError:
pass

for suffix in self.source_suffix:
if os.path.basename(filename).endswith(suffix):
return path_stabilize(filename).removesuffix(suffix)
Expand Down
6 changes: 3 additions & 3 deletions sphinx/pycode/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import ast
import contextlib
import inspect
import itertools
import re
Expand Down Expand Up @@ -54,10 +55,9 @@ def get_lvar_names(node: ast.AST, self: ast.arg | None = None) -> list[str]:
elif node_name in ('Tuple', 'List'):
members = []
for elt in node.elts: # type: ignore[attr-defined]
try:
with contextlib.suppress(TypeError):
members.extend(get_lvar_names(elt, self))
except TypeError:
pass

return members
elif node_name == 'Attribute':
if (
Expand Down
5 changes: 2 additions & 3 deletions sphinx/testing/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sphinx test suite utilities"""
from __future__ import annotations

import contextlib
import os
import re
import sys
Expand Down Expand Up @@ -135,10 +136,8 @@ def cleanup(self, doctrees: bool = False) -> None:
method not in self._saved_nodeclasses:
delattr(nodes.GenericNodeVisitor, 'visit_' + method[6:])
delattr(nodes.GenericNodeVisitor, 'depart_' + method[6:])
try:
with contextlib.suppress(FileNotFoundError):
os.remove(self.docutils_conf_path)
except FileNotFoundError:
pass

def __repr__(self) -> str:
return f'<{self.__class__.__name__} buildername={self.builder.name!r}>'
Expand Down
11 changes: 5 additions & 6 deletions sphinx/theming.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
else:
from importlib_metadata import entry_points

import contextlib

from sphinx import package_dir
from sphinx.errors import ThemeError
from sphinx.locale import __
Expand Down Expand Up @@ -117,10 +119,8 @@ def get_options(self, overrides: dict[str, Any] | None = None) -> dict[str, Any]
else:
options = {}

try:
with contextlib.suppress(configparser.NoSectionError):
options.update(self.config.items('options'))
except configparser.NoSectionError:
pass

for option, value in overrides.items():
if option not in options:
Expand All @@ -133,10 +133,9 @@ def get_options(self, overrides: dict[str, Any] | None = None) -> dict[str, Any]
def cleanup(self) -> None:
"""Remove temporary directories."""
if self.rootdir:
try:
with contextlib.suppress(Exception):
shutil.rmtree(self.rootdir)
except Exception:
pass

if self.base:
self.base.cleanup()

Expand Down
8 changes: 2 additions & 6 deletions sphinx/util/osutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ def mtimes_of_files(dirnames: list[str], suffix: str) -> Iterator[float]:
for root, _dirs, files in os.walk(dirname):
for sfile in files:
if sfile.endswith(suffix):
try:
with contextlib.suppress(OSError):
yield path.getmtime(path.join(root, sfile))
except OSError:
pass


def copytimes(source: str | os.PathLike[str], dest: str | os.PathLike[str]) -> None:
Expand All @@ -93,11 +91,9 @@ def copyfile(source: str | os.PathLike[str], dest: str | os.PathLike[str]) -> No
Note: ``copyfile`` skips copying if the file has not been changed"""
if not path.exists(dest) or not filecmp.cmp(source, dest):
shutil.copyfile(source, dest)
try:
with contextlib.suppress(OSError):
# don't do full copystat because the source may be read-only
copytimes(source, dest)
except OSError:
pass


no_fn_re = re.compile(r'[^a-zA-Z0-9_-]')
Expand Down

0 comments on commit 6cb783c

Please sign in to comment.