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`
# "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
# "RUF017", # Avoid quadratic list summation
"RUF017", # Avoid quadratic list summation
"RUF018", # Avoid assignment expressions in `assert` statements
"RUF019", # Unnecessary key check before dictionary access
"RUF020", # `{never_like} | T` is equivalent to `T`

View File

@ -5,7 +5,9 @@ from __future__ import annotations
import ast
import builtins
import contextlib
import functools
import inspect
import operator
import re
import token
import typing
@ -185,7 +187,7 @@ def _parse_annotation(annotation: str, env: BuildEnvironment) -> list[Node]:
result.append(addnodes.desc_sig_punctuation('', ']'))
return result
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):
return [nodes.Text(node.id)]
if isinstance(node, ast.Subscript):

View File

@ -7,6 +7,8 @@ for those who like elaborate docstrings.
from __future__ import annotations
import functools
import operator
import re
import sys
import warnings
@ -923,7 +925,7 @@ class Documenter:
except PycodeError:
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:
logger.warning(__('A mocked object is detected: %r'),
self.name, type='autodoc')
@ -2054,7 +2056,8 @@ class DataDocumenter(GenericAliasMixin,
return True
else:
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:
return True
@ -2625,7 +2628,8 @@ class AttributeDocumenter(GenericAliasMixin, SlotsMixin, # type: ignore[misc]
else:
doc = self.get_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:
return True

View File

@ -48,7 +48,9 @@ This can be used as the default role to make links 'smart'.
from __future__ import annotations
import functools
import inspect
import operator
import os
import posixpath
import re
@ -651,7 +653,8 @@ def import_by_name(
tried.append(prefixed_name)
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)

View File

@ -7,6 +7,8 @@ with a backlink to the original location.
from __future__ import annotations
import functools
import operator
from typing import TYPE_CHECKING, Any, cast
from docutils import nodes
@ -129,7 +131,8 @@ class TodoListProcessor:
self.process(doctree, docname)
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)):
if not self.config.todo_include_todos:
node.parent.remove(node)

View File

@ -4,8 +4,10 @@ from __future__ import annotations
import ast
import contextlib
import functools
import inspect
import itertools
import operator
import re
import tokenize
from inspect import Signature
@ -350,9 +352,8 @@ class VariableCommentPicker(ast.NodeVisitor):
"""Handles Assign node and pick up a variable comment."""
try:
targets = get_assign_targets(node)
varnames: list[str] = sum(
[get_lvar_names(t, self=self.get_self()) for t in targets], [],
)
varnames: list[str] = functools.reduce(
operator.iadd, [get_lvar_names(t, self=self.get_self()) for t in targets], [])
current_line = self.get_line(node.lineno)
except TypeError:
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.
"""
import functools
import operator
import sys
from types import SimpleNamespace
from unittest.mock import Mock
@ -330,7 +332,7 @@ def test_get_doc(app):
inst.format_signature() # handle docstring signatures!
ds = inst.get_doc()
# 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)
return res