From 0bf661857db123bb30ec079de4bcac61bed58edd Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 9 Jan 2022 16:44:07 +0000 Subject: [PATCH 1/5] simplify dict key checks (SIM118) --- sphinx/domains/c.py | 2 +- sphinx/domains/cpp.py | 2 +- sphinx/util/cfamily.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sphinx/domains/c.py b/sphinx/domains/c.py index ea29b94b7..75581d71a 100644 --- a/sphinx/domains/c.py +++ b/sphinx/domains/c.py @@ -2531,7 +2531,7 @@ class DefinitionParser(BaseParser): while not self.eof: if (len(symbols) == 0 and self.current_char in end): break - if self.current_char in brackets.keys(): + if self.current_char in brackets: symbols.append(brackets[self.current_char]) elif len(symbols) > 0 and self.current_char == symbols[-1]: symbols.pop() diff --git a/sphinx/domains/cpp.py b/sphinx/domains/cpp.py index 98a89594f..980c4db5c 100644 --- a/sphinx/domains/cpp.py +++ b/sphinx/domains/cpp.py @@ -5754,7 +5754,7 @@ class DefinitionParser(BaseParser): while not self.eof: if (len(symbols) == 0 and self.current_char in end): break - if self.current_char in brackets.keys(): + if self.current_char in brackets: symbols.append(brackets[self.current_char]) elif len(symbols) > 0 and self.current_char == symbols[-1]: symbols.pop() diff --git a/sphinx/util/cfamily.py b/sphinx/util/cfamily.py index 2dbb02004..21ac9b79f 100644 --- a/sphinx/util/cfamily.py +++ b/sphinx/util/cfamily.py @@ -379,7 +379,7 @@ class BaseParser: while not self.eof: if len(symbols) == 0 and self.current_char in end: break - if self.current_char in brackets.keys(): + if self.current_char in brackets: symbols.append(brackets[self.current_char]) elif len(symbols) > 0 and self.current_char == symbols[-1]: symbols.pop() From 9e051dfb3467919459eb4e6fe30da2c6077aa950 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 9 Jan 2022 16:50:10 +0000 Subject: [PATCH 2/5] remove unnecessary list calls within 'sorted' (C414) --- sphinx/builders/html/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index b320b0df5..6ba92eb3e 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -1055,7 +1055,7 @@ class StandaloneHTMLBuilder(Builder): # sort JS/CSS before rendering HTML try: # Convert script_files to list to support non-list script_files (refs: #8889) - ctx['script_files'] = sorted(list(ctx['script_files']), key=lambda js: js.priority) + ctx['script_files'] = sorted(ctx['script_files'], key=lambda js: js.priority) except AttributeError: # Skip sorting if users modifies script_files directly (maybe via `html_context`). # refs: #8885 @@ -1064,7 +1064,7 @@ class StandaloneHTMLBuilder(Builder): pass try: - ctx['css_files'] = sorted(list(ctx['css_files']), key=lambda css: css.priority) + ctx['css_files'] = sorted(ctx['css_files'], key=lambda css: css.priority) except AttributeError: pass From 55a0143df790e65514faaec4c79cfbec103a2a35 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 9 Jan 2022 16:52:03 +0000 Subject: [PATCH 3/5] remove unnecessary list calls around 'sorted' (C413) --- sphinx/ext/autosummary/generate.py | 2 +- tests/test_domain_cpp.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sphinx/ext/autosummary/generate.py b/sphinx/ext/autosummary/generate.py index 3db7eb989..5d5e64b92 100644 --- a/sphinx/ext/autosummary/generate.py +++ b/sphinx/ext/autosummary/generate.py @@ -355,7 +355,7 @@ def generate_autosummary_docs(sources: List[str], output_dir: str = None, suffix: str = '.rst', base_path: str = None, imported_members: bool = False, app: Any = None, overwrite: bool = True, encoding: str = 'utf-8') -> None: - showed_sources = list(sorted(sources)) + showed_sources = sorted(sources) if len(showed_sources) > 20: showed_sources = showed_sources[:10] + ['...'] + showed_sources[-10:] logger.info(__('[autosummary] generating autosummary for: %s') % diff --git a/tests/test_domain_cpp.py b/tests/test_domain_cpp.py index 70876728d..765f9fd65 100644 --- a/tests/test_domain_cpp.py +++ b/tests/test_domain_cpp.py @@ -1113,11 +1113,11 @@ def test_domain_cpp_build_misuse_of_roles(app, status, warning): if targetType == 'templateParam': warn.append("WARNING: cpp:{} targets a {} (".format(r, txtTargetType)) warn.append("WARNING: cpp:{} targets a {} (".format(r, txtTargetType)) - warn = list(sorted(warn)) + warn = sorted(warn) for w in ws: assert "targets a" in w ws = [w[w.index("WARNING:"):] for w in ws] - ws = list(sorted(ws)) + ws = sorted(ws) print("Expected warnings:") for w in warn: print(w) From 89210fc2756eaf0fc1bbcf3eccb7a9f9f690b820 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 9 Jan 2022 16:55:12 +0000 Subject: [PATCH 4/5] remove unnecessary list comprehension (C416) --- tests/test_ext_napoleon_iterators.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_ext_napoleon_iterators.py b/tests/test_ext_napoleon_iterators.py index 6b80af6bf..f5fe541b6 100644 --- a/tests/test_ext_napoleon_iterators.py +++ b/tests/test_ext_napoleon_iterators.py @@ -55,19 +55,19 @@ class PeekIterTest(BaseIteratorsTest): self.assertTrue(it is it.__iter__()) a = [] - b = [i for i in peek_iter(a)] + b = list(peek_iter(a)) self.assertEqual([], b) a = ['1'] - b = [i for i in peek_iter(a)] + b = list(peek_iter(a)) self.assertEqual(['1'], b) a = ['1', '2'] - b = [i for i in peek_iter(a)] + b = list(peek_iter(a)) self.assertEqual(['1', '2'], b) a = ['1', '2', '3'] - b = [i for i in peek_iter(a)] + b = list(peek_iter(a)) self.assertEqual(['1', '2', '3'], b) def test_next_with_multi(self): @@ -303,7 +303,7 @@ class ModifyIterTest(BaseIteratorsTest): return next(a) it = modify_iter(get_next, sentinel, int) expected = [1, 2, 3] - self.assertEqual(expected, [i for i in it]) + self.assertEqual(expected, list(it)) def test_init_with_sentinel_kwargs(self): a = iter([1, 2, 3, 4]) @@ -313,13 +313,13 @@ class ModifyIterTest(BaseIteratorsTest): return next(a) it = modify_iter(get_next, sentinel, modifier=str) expected = ['1', '2', '3'] - self.assertEqual(expected, [i for i in it]) + self.assertEqual(expected, list(it)) def test_modifier_default(self): a = ['', ' ', ' a ', 'b ', ' c', ' ', ''] it = modify_iter(a) expected = ['', ' ', ' a ', 'b ', ' c', ' ', ''] - self.assertEqual(expected, [i for i in it]) + self.assertEqual(expected, list(it)) def test_modifier_not_callable(self): self.assertRaises(TypeError, modify_iter, [1], modifier='not_callable') @@ -328,10 +328,10 @@ class ModifyIterTest(BaseIteratorsTest): a = ['', ' ', ' a ', 'b ', ' c', ' ', ''] it = modify_iter(a, modifier=lambda s: s.rstrip()) expected = ['', '', ' a', 'b', ' c', '', ''] - self.assertEqual(expected, [i for i in it]) + self.assertEqual(expected, list(it)) def test_modifier_rstrip_unicode(self): a = ['', ' ', ' a ', 'b ', ' c', ' ', ''] it = modify_iter(a, modifier=lambda s: s.rstrip()) expected = ['', '', ' a', 'b', ' c', '', ''] - self.assertEqual(expected, [i for i in it]) + self.assertEqual(expected, list(it)) From 4e4813099bffde35b0c1d3f12eeac85420ad447b Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 9 Jan 2022 16:58:49 +0000 Subject: [PATCH 5/5] remove unnecessary generators (C400, C401) --- sphinx/ext/autosummary/__init__.py | 2 +- sphinx/ext/autosummary/generate.py | 4 ++-- sphinx/ext/napoleon/docstring.py | 4 ++-- sphinx/util/__init__.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py index fbf62c3cd..2b9055d3e 100644 --- a/sphinx/ext/autosummary/__init__.py +++ b/sphinx/ext/autosummary/__init__.py @@ -315,7 +315,7 @@ class Autosummary(SphinxDirective): try: real_name, obj, parent, modname = self.import_by_name(name, prefixes=prefixes) except ImportExceptionGroup as exc: - errors = list(set("* %s: %s" % (type(e).__name__, e) for e in exc.exceptions)) + errors = list({"* %s: %s" % (type(e).__name__, e) for e in exc.exceptions}) logger.warning(__('autosummary: failed to import %s.\nPossible hints:\n%s'), name, '\n'.join(errors), location=self.get_location()) continue diff --git a/sphinx/ext/autosummary/generate.py b/sphinx/ext/autosummary/generate.py index 5d5e64b92..0e1daf79a 100644 --- a/sphinx/ext/autosummary/generate.py +++ b/sphinx/ext/autosummary/generate.py @@ -404,7 +404,7 @@ def generate_autosummary_docs(sources: List[str], output_dir: str = None, else: exceptions = exc.exceptions + [exc2] - errors = list(set("* %s: %s" % (type(e).__name__, e) for e in exceptions)) + errors = list({"* %s: %s" % (type(e).__name__, e) for e in exceptions}) logger.warning(__('[autosummary] failed to import %s.\nPossible hints:\n%s'), entry.name, '\n'.join(errors)) continue @@ -468,7 +468,7 @@ def find_autosummary_in_docstring(name: str, filename: str = None) -> List[Autos except AttributeError: pass except ImportExceptionGroup as exc: - errors = list(set("* %s: %s" % (type(e).__name__, e) for e in exc.exceptions)) + errors = list({"* %s: %s" % (type(e).__name__, e) for e in exc.exceptions}) print('Failed to import %s.\nPossible hints:\n%s' % (name, '\n'.join(errors))) except SystemExit: print("Failed to import '%s'; the module executes module level " diff --git a/sphinx/ext/napoleon/docstring.py b/sphinx/ext/napoleon/docstring.py index a11424537..d866594a3 100644 --- a/sphinx/ext/napoleon/docstring.py +++ b/sphinx/ext/napoleon/docstring.py @@ -931,12 +931,12 @@ def _tokenize_type_spec(spec: str) -> List[str]: else: return [item] - tokens = list( + tokens = [ item for raw_token in _token_regex.split(spec) for item in postprocess(raw_token) if item - ) + ] return tokens diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index b5a49a9d1..c8e8c6b94 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -424,7 +424,7 @@ def encode_uri(uri: str) -> str: split = list(urlsplit(uri)) split[1] = split[1].encode('idna').decode('ascii') split[2] = quote_plus(split[2].encode(), '/') - query = list((q, v.encode()) for (q, v) in parse_qsl(split[3])) + query = [(q, v.encode()) for (q, v) in parse_qsl(split[3])] split[3] = urlencode(query) return urlunsplit(split)