Enable RUF017

This commit is contained in:
Adam Turner 2024-01-04 04:11:30 +00:00
parent 9a9599450c
commit 1b4177540e
7 changed files with 26 additions and 11 deletions

View File

@ -243,7 +243,7 @@ select = [
"RUF013", # PEP 484 prohibits implicit `Optional` "RUF013", # PEP 484 prohibits implicit `Optional`
# "RUF015", # Prefer `next({iterable})` over single element slice # "RUF015", # Prefer `next({iterable})` over single element slice
"RUF016", # Slice in indexed access to type `{value_type}` uses type `{index_type}` instead of an integer "RUF016", # Slice in indexed access to type `{value_type}` uses type `{index_type}` instead of an integer
# "RUF017", # Avoid quadratic list summation "RUF017", # Avoid quadratic list summation
"RUF018", # Avoid assignment expressions in `assert` statements "RUF018", # Avoid assignment expressions in `assert` statements
"RUF019", # Unnecessary key check before dictionary access "RUF019", # Unnecessary key check before dictionary access
"RUF020", # `{never_like} | T` is equivalent to `T` "RUF020", # `{never_like} | T` is equivalent to `T`

View File

@ -5,7 +5,9 @@ from __future__ import annotations
import ast import ast
import builtins import builtins
import contextlib import contextlib
import functools
import inspect import inspect
import operator
import re import re
import token import token
import typing import typing
@ -185,7 +187,7 @@ def _parse_annotation(annotation: str, env: BuildEnvironment) -> list[Node]:
result.append(addnodes.desc_sig_punctuation('', ']')) result.append(addnodes.desc_sig_punctuation('', ']'))
return result return result
if isinstance(node, ast.Module): if isinstance(node, ast.Module):
return sum((unparse(e) for e in node.body), []) return functools.reduce(operator.iadd, (unparse(e) for e in node.body), [])
if isinstance(node, ast.Name): if isinstance(node, ast.Name):
return [nodes.Text(node.id)] return [nodes.Text(node.id)]
if isinstance(node, ast.Subscript): if isinstance(node, ast.Subscript):

View File

@ -7,6 +7,8 @@ for those who like elaborate docstrings.
from __future__ import annotations from __future__ import annotations
import functools
import operator
import re import re
import sys import sys
import warnings import warnings
@ -923,7 +925,7 @@ class Documenter:
except PycodeError: except PycodeError:
pass pass
docstrings: list[str] = sum(self.get_doc() or [], []) docstrings: list[str] = functools.reduce(operator.iadd, self.get_doc() or [], [])
if ismock(self.object) and not docstrings: if ismock(self.object) and not docstrings:
logger.warning(__('A mocked object is detected: %r'), logger.warning(__('A mocked object is detected: %r'),
self.name, type='autodoc') self.name, type='autodoc')
@ -2054,7 +2056,8 @@ class DataDocumenter(GenericAliasMixin,
return True return True
else: else:
doc = self.get_doc() or [] doc = self.get_doc() or []
docstring, metadata = separate_metadata('\n'.join(sum(doc, []))) docstring, metadata = separate_metadata(
'\n'.join(functools.reduce(operator.iadd, doc, [])))
if 'hide-value' in metadata: if 'hide-value' in metadata:
return True return True
@ -2625,7 +2628,8 @@ class AttributeDocumenter(GenericAliasMixin, SlotsMixin, # type: ignore[misc]
else: else:
doc = self.get_doc() doc = self.get_doc()
if doc: if doc:
docstring, metadata = separate_metadata('\n'.join(sum(doc, []))) docstring, metadata = separate_metadata(
'\n'.join(functools.reduce(operator.iadd, doc, [])))
if 'hide-value' in metadata: if 'hide-value' in metadata:
return True return True

View File

@ -48,7 +48,9 @@ This can be used as the default role to make links 'smart'.
from __future__ import annotations from __future__ import annotations
import functools
import inspect import inspect
import operator
import os import os
import posixpath import posixpath
import re import re
@ -651,7 +653,8 @@ def import_by_name(
tried.append(prefixed_name) tried.append(prefixed_name)
errors.append(exc) errors.append(exc)
exceptions: list[BaseException] = sum((e.exceptions for e in errors), []) exceptions: list[BaseException] = functools.reduce(
operator.iadd, (e.exceptions for e in errors), [])
raise ImportExceptionGroup('no module named %s' % ' or '.join(tried), exceptions) raise ImportExceptionGroup('no module named %s' % ' or '.join(tried), exceptions)

View File

@ -7,6 +7,8 @@ with a backlink to the original location.
from __future__ import annotations from __future__ import annotations
import functools
import operator
from typing import TYPE_CHECKING, Any, cast from typing import TYPE_CHECKING, Any, cast
from docutils import nodes from docutils import nodes
@ -129,7 +131,8 @@ class TodoListProcessor:
self.process(doctree, docname) self.process(doctree, docname)
def process(self, doctree: nodes.document, docname: str) -> None: def process(self, doctree: nodes.document, docname: str) -> None:
todos: list[todo_node] = sum(self.domain.todos.values(), []) todos: list[todo_node] = functools.reduce(
operator.iadd, self.domain.todos.values(), [])
for node in list(doctree.findall(todolist)): for node in list(doctree.findall(todolist)):
if not self.config.todo_include_todos: if not self.config.todo_include_todos:
node.parent.remove(node) node.parent.remove(node)

View File

@ -4,8 +4,10 @@ from __future__ import annotations
import ast import ast
import contextlib import contextlib
import functools
import inspect import inspect
import itertools import itertools
import operator
import re import re
import tokenize import tokenize
from inspect import Signature from inspect import Signature
@ -350,9 +352,8 @@ class VariableCommentPicker(ast.NodeVisitor):
"""Handles Assign node and pick up a variable comment.""" """Handles Assign node and pick up a variable comment."""
try: try:
targets = get_assign_targets(node) targets = get_assign_targets(node)
varnames: list[str] = sum( varnames: list[str] = functools.reduce(
[get_lvar_names(t, self=self.get_self()) for t in targets], [], operator.iadd, [get_lvar_names(t, self=self.get_self()) for t in targets], [])
)
current_line = self.get_line(node.lineno) current_line = self.get_line(node.lineno)
except TypeError: except TypeError:
return # this assignment is not new definition! return # this assignment is not new definition!

View File

@ -4,6 +4,8 @@ This tests mainly the Documenters; the auto directives are tested in a test
source file translated by test_build. source file translated by test_build.
""" """
import functools
import operator
import sys import sys
from types import SimpleNamespace from types import SimpleNamespace
from unittest.mock import Mock from unittest.mock import Mock
@ -330,7 +332,7 @@ def test_get_doc(app):
inst.format_signature() # handle docstring signatures! inst.format_signature() # handle docstring signatures!
ds = inst.get_doc() ds = inst.get_doc()
# for testing purposes, concat them and strip the empty line at the end # for testing purposes, concat them and strip the empty line at the end
res = sum(ds, [])[:-1] res = functools.reduce(operator.iadd, ds, [])[:-1]
print(res) print(res)
return res return res