Skip to content

Commit

Permalink
Bump Ruff to 0.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Nov 29, 2024
1 parent d9a2821 commit b458850
Show file tree
Hide file tree
Showing 35 changed files with 111 additions and 102 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ docs = [
]
lint = [
"flake8>=6.0",
"ruff==0.8.0",
"ruff==0.8.1",
"mypy==1.13.0",
"sphinx-lint>=0.9",
"types-colorama==0.4.15.20240311",
Expand Down
2 changes: 1 addition & 1 deletion sphinx/builders/latex/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def renumber_footnotes(self) -> None:
break

# assign new footnote number
old_label = cast(nodes.label, footnote[0])
old_label = cast('nodes.label', footnote[0])
old_label.replace_self(nodes.label('', str(num)))
if old_label in footnote['names']:
footnote['names'].remove(old_label.astext())
Expand Down
2 changes: 1 addition & 1 deletion sphinx/builders/linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _add_uri(self, uri: str, node: nodes.Element) -> None:
:param uri: URI to add
:param node: A node class where the URI was found
"""
builder = cast(CheckExternalLinksBuilder, self.app.builder)
builder = cast('CheckExternalLinksBuilder', self.app.builder)
hyperlinks = builder.hyperlinks
docname = self.env.docname

Expand Down
4 changes: 2 additions & 2 deletions sphinx/directives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_field_type_map(self) -> dict[str, tuple[Field, bool]]:
self._doc_field_type_map[name] = (field, False)

if field.is_typed:
typed_field = cast(TypedField, field)
typed_field = cast('TypedField', field)
for name in typed_field.typenames:
self._doc_field_type_map[name] = (field, True)

Expand Down Expand Up @@ -346,7 +346,7 @@ def run(self) -> list[Node]:
)
messages += [error]

return cast(list[nodes.Node], messages)
return cast('list[nodes.Node]', messages)


class DefaultDomain(SphinxDirective):
Expand Down
2 changes: 1 addition & 1 deletion sphinx/directives/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def run(self) -> list[Node]:
# Use these depths to determine where the nested sections should
# be placed in the doctree.
n_sects_to_raise = current_depth - nested_depth + 1
parent = cast(nodes.Element, self.state.parent)
parent = cast('nodes.Element', self.state.parent)
for _i in range(n_sects_to_raise):
if parent.parent:
parent = parent.parent
Expand Down
6 changes: 3 additions & 3 deletions sphinx/directives/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def run(self) -> list[Node]:
return result

assert len(result) == 1
figure_node = cast(nodes.figure, result[0])
figure_node = cast('nodes.figure', result[0])
if name:
# set ``name`` to figure_node if given
self.options['name'] = name
self.add_name(figure_node)

# copy lineno from image node
if figure_node.line is None and len(figure_node) == 2:
caption = cast(nodes.caption, figure_node[1])
caption = cast('nodes.caption', figure_node[1])
figure_node.line = caption.line

return [figure_node]
Expand Down Expand Up @@ -163,7 +163,7 @@ def run(self) -> list[Node]:
return ret

def add_target(self, ret: list[Node]) -> None:
node = cast(nodes.math_block, ret[0])
node = cast('nodes.math_block', ret[0])

# assign label automatically if math_number_all enabled
if node['label'] == '' or (self.config.math_number_all and not node['label']): # NoQA: PLC1901
Expand Down
6 changes: 3 additions & 3 deletions sphinx/domains/c/_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1793,14 +1793,14 @@ def clone(self) -> ASTDeclaration:

@property
def name(self) -> ASTNestedName:
decl = cast(DeclarationType, self.declaration)
decl = cast('DeclarationType', self.declaration)
return decl.name

@property
def function_params(self) -> list[ASTFunctionParameter] | None:
if self.objectType != 'function':
return None
decl = cast(ASTType, self.declaration)
decl = cast('ASTType', self.declaration)
return decl.function_params

def get_id(self, version: int, prefixed: bool = True) -> str:
Expand Down Expand Up @@ -1851,7 +1851,7 @@ def describe_signature(self, signode: TextElement, mode: str,
mainDeclNode += addnodes.desc_sig_keyword('enumerator', 'enumerator')
mainDeclNode += addnodes.desc_sig_space()
elif self.objectType == 'type':
decl = cast(ASTType, self.declaration)
decl = cast('ASTType', self.declaration)
prefix = decl.get_type_declaration_prefix()
mainDeclNode += addnodes.desc_sig_keyword(prefix, prefix)
mainDeclNode += addnodes.desc_sig_space()
Expand Down
2 changes: 1 addition & 1 deletion sphinx/domains/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def apply(self, **kwargs: Any) -> None:
domain.note_citation(node)

# mark citation labels as not smartquoted
label = cast(nodes.label, node[0])
label = cast('nodes.label', node[0])
label['support_smartquotes'] = False


Expand Down
4 changes: 2 additions & 2 deletions sphinx/domains/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,10 @@ def filter_meta_fields(app: Sphinx, domain: str, objtype: str, content: Element)

for node in content:
if isinstance(node, nodes.field_list):
fields = cast(list[nodes.field], node)
fields = cast('list[nodes.field]', node)
# removing list items while iterating the list needs reversed()
for field in reversed(fields):
field_name = cast(nodes.field_body, field[0]).astext().strip()
field_name = cast('nodes.field_body', field[0]).astext().strip()
if field_name == 'meta' or field_name.startswith('meta '):
node.remove(field)

Expand Down
11 changes: 6 additions & 5 deletions sphinx/domains/std/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import TYPE_CHECKING, Any, ClassVar, Final, cast

from docutils import nodes
from docutils.nodes import Element, Node, system_message
from docutils.parsers.rst import Directive, directives
from docutils.statemachine import StringList

Expand All @@ -25,6 +24,8 @@
if TYPE_CHECKING:
from collections.abc import Callable, Iterable, Iterator, Set

from docutils.nodes import Element, Node, system_message

from sphinx.application import Sphinx
from sphinx.builders import Builder
from sphinx.environment import BuildEnvironment
Expand Down Expand Up @@ -813,7 +814,7 @@ def process_doc(
location=node)
self.anonlabels[name] = docname, labelid
if node.tagname == 'section':
title = cast(nodes.title, node[0])
title = cast('nodes.title', node[0])
sectname = clean_astext(title)
elif node.tagname == 'rubric':
sectname = clean_astext(node)
Expand All @@ -824,9 +825,9 @@ def process_doc(
else:
if (isinstance(node, nodes.definition_list | nodes.field_list) and
node.children):
node = cast(nodes.Element, node.children[0])
node = cast('nodes.Element', node.children[0])
if isinstance(node, nodes.field | nodes.definition_list_item):
node = cast(nodes.Element, node.children[0])
node = cast('nodes.Element', node.children[0])
if isinstance(node, nodes.term | nodes.field_name):
sectname = clean_astext(node)
else:
Expand Down Expand Up @@ -1117,7 +1118,7 @@ def is_enumerable_node(self, node: Node) -> bool:
def get_numfig_title(self, node: Node) -> str | None:
"""Get the title of enumerable nodes to refer them using its title"""
if self.is_enumerable_node(node):
elem = cast(Element, node)
elem = cast('Element', node)
_, title_getter = self.enumerable_nodes.get(elem.__class__, (None, None))
if title_getter:
return title_getter(elem)
Expand Down
6 changes: 3 additions & 3 deletions sphinx/environment/collectors/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def process_doc(self, app: Sphinx, doctree: nodes.document) -> None:
for node in doctree[index]: # type: ignore[attr-defined]
# nodes are multiply inherited...
if isinstance(node, nodes.authors):
authors = cast(list[nodes.author], node)
authors = cast('list[nodes.author]', node)
md['authors'] = [author.astext() for author in authors]
elif isinstance(node, nodes.field):
assert len(node) == 2
field_name = cast(nodes.field_name, node[0])
field_body = cast(nodes.field_body, node[1])
field_name = cast('nodes.field_name', node[0])
field_body = cast('nodes.field_body', node[1])
md[field_name.astext()] = field_body.astext()
elif isinstance(node, nodes.TextElement):
# other children must be TextElement
Expand Down
2 changes: 1 addition & 1 deletion sphinx/environment/collectors/toctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _walk_toc(
if 'skip_section_number' in subnode:
continue
numstack[-1] += 1
reference = cast(nodes.reference, subnode[0])
reference = cast('nodes.reference', subnode[0])
if depth > 0:
number = numstack.copy()
secnums[reference['anchorname']] = tuple(numstack)
Expand Down
4 changes: 2 additions & 2 deletions sphinx/ext/autodoc/type_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def get_type_comment(obj: Any, bound_method: bool = False) -> Signature | None:
# this adds if-block before the declaration.
module = ast.parse('if True:\n' + source, type_comments=True)
subject = cast(
ast.FunctionDef, module.body[0].body[0], # type: ignore[attr-defined]
'ast.FunctionDef', module.body[0].body[0], # type: ignore[attr-defined]
)
else:
module = ast.parse(source, type_comments=True)
subject = cast(ast.FunctionDef, module.body[0])
subject = cast('ast.FunctionDef', module.body[0])

type_comment = getattr(subject, "type_comment", None)
if type_comment:
Expand Down
9 changes: 5 additions & 4 deletions sphinx/ext/autodoc/typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import re
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, cast

from docutils import nodes
Expand All @@ -14,6 +13,8 @@
from sphinx.util.typing import ExtensionMetadata, stringify_annotation

if TYPE_CHECKING:
from collections.abc import Iterable

from docutils.nodes import Element

from sphinx.application import Sphinx
Expand Down Expand Up @@ -49,7 +50,7 @@ def merge_typehints(app: Sphinx, domain: str, objtype: str, contentnode: Element
return

try:
signature = cast(addnodes.desc_signature, contentnode.parent[0])
signature = cast('addnodes.desc_signature', contentnode.parent[0])
if signature['module']:
fullname = f'{signature["module"]}.{signature["fullname"]}'
else:
Expand Down Expand Up @@ -97,7 +98,7 @@ def insert_field_list(node: Element) -> nodes.field_list:
def modify_field_list(node: nodes.field_list, annotations: dict[str, str],
suppress_rtype: bool = False) -> None:
arguments: dict[str, dict[str, bool]] = {}
fields = cast(Iterable[nodes.field], node)
fields = cast('Iterable[nodes.field]', node)
for field in fields:
field_name = field[0].astext()
parts = re.split(' +', field_name)
Expand Down Expand Up @@ -159,7 +160,7 @@ def augment_descriptions_with_types(
annotations: dict[str, str],
force_rtype: bool,
) -> None:
fields = cast(Iterable[nodes.field], node)
fields = cast('Iterable[nodes.field]', node)
has_description: set[str] = set()
has_type: set[str] = set()
for field in fields:
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/autosectionlabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def register_sections_as_label(app: Sphinx, document: Node) -> None:
continue
labelid = node['ids'][0]
docname = app.env.docname
title = cast(nodes.title, node[0])
title = cast('nodes.title', node[0])
ref_name = getattr(title, 'rawsource', title.astext())
if app.config.autosectionlabel_prefix_document:
name = nodes.fully_normalize_name(docname + ':' + ref_name)
Expand Down
16 changes: 8 additions & 8 deletions sphinx/ext/autosummary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ class autosummary_table(nodes.comment):
def autosummary_table_visit_html(self: HTML5Translator, node: autosummary_table) -> None:
"""Make the first column of the table non-breaking."""
try:
table = cast(nodes.table, node[0])
tgroup = cast(nodes.tgroup, table[0])
tbody = cast(nodes.tbody, tgroup[-1])
rows = cast(list[nodes.row], tbody)
table = cast('nodes.table', node[0])
tgroup = cast('nodes.tgroup', table[0])
tbody = cast('nodes.tbody', tgroup[-1])
rows = cast('list[nodes.row]', tbody)
for row in rows:
col1_entry = cast(nodes.entry, row[0])
par = cast(nodes.paragraph, col1_entry[0])
col1_entry = cast('nodes.entry', row[0])
par = cast('nodes.paragraph', col1_entry[0])
for j, subnode in enumerate(list(par)):
if isinstance(subnode, nodes.Text):
new_text = subnode.astext().replace(" ", "\u00a0")
Expand Down Expand Up @@ -765,7 +765,7 @@ def run(self) -> tuple[list[Node], list[system_message]]:
return objects, errors

assert len(objects) == 1
pending_xref = cast(addnodes.pending_xref, objects[0])
pending_xref = cast('addnodes.pending_xref', objects[0])
try:
# try to import object by name
prefixes = get_import_prefixes_from_env(self.env)
Expand All @@ -778,7 +778,7 @@ def run(self) -> tuple[list[Node], list[system_message]]:
]
import_by_name(name, prefixes)
except ImportExceptionGroup:
literal = cast(nodes.literal, pending_xref[0])
literal = cast('nodes.literal', pending_xref[0])
objects[0] = nodes.emphasis(self.rawtext, literal.astext(),
classes=literal['classes'])

Expand Down
5 changes: 3 additions & 2 deletions sphinx/ext/inheritance_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class E(B): pass
import inspect
import os.path
import re
from collections.abc import Iterable, Sequence
from importlib import import_module
from typing import TYPE_CHECKING, Any, ClassVar, cast

Expand All @@ -54,6 +53,8 @@ class E(B): pass
from sphinx.util.docutils import SphinxDirective

if TYPE_CHECKING:
from collections.abc import Iterable, Sequence

from docutils.nodes import Node

from sphinx.application import Sphinx
Expand Down Expand Up @@ -424,7 +425,7 @@ def html_visit_inheritance_diagram(self: HTML5Translator, node: inheritance_diag
graphviz_output_format = self.builder.env.config.graphviz_output_format.upper()
current_filename = os.path.basename(self.builder.current_docname + self.builder.out_suffix)
urls = {}
pending_xrefs = cast(Iterable[addnodes.pending_xref], node)
pending_xrefs = cast('Iterable[addnodes.pending_xref]', node)
for child in pending_xrefs:
if child.get('refuri') is not None:
# Construct the name from the URI if the reference is external via intersphinx
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/intersphinx/_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def run(self, **kwargs: Any) -> None:
for node in self.document.findall(pending_xref):
if 'intersphinx' not in node:
continue
contnode = cast(nodes.TextElement, node[0].deepcopy())
contnode = cast('nodes.TextElement', node[0].deepcopy())
inv_name = node['inventory']
if inv_name is not None:
assert inventory_exists(self.env, inv_name)
Expand Down
4 changes: 2 additions & 2 deletions sphinx/ext/mathjax.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
from docutils import nodes

import sphinx
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.errors import ExtensionError
from sphinx.locale import _
from sphinx.util.math import get_node_equation_number

if TYPE_CHECKING:
from sphinx.application import Sphinx
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.util.typing import ExtensionMetadata
from sphinx.writers.html5 import HTML5Translator

Expand Down Expand Up @@ -82,7 +82,7 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict
raise ExtensionError(msg)

domain = app.env.domains.math_domain
builder = cast(StandaloneHTMLBuilder, app.builder)
builder = cast('StandaloneHTMLBuilder', app.builder)
if app.registry.html_assets_policy == 'always' or domain.has_equations(pagename):
# Enable mathjax only if equations exists
if app.config.mathjax2_config:
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def latex_visit_todo_node(self: LaTeXTranslator, node: todo_node) -> None:
self.body.append('\n\\begin{sphinxtodo}{')
self.body.append(self.hypertarget_to(node))

title_node = cast(nodes.title, node[0])
title_node = cast('nodes.title', node[0])
title = texescape.escape(title_node.astext(), self.config.latex_engine)
self.body.append('%s:}' % title)
self.no_latex_floats += 1
Expand Down
4 changes: 2 additions & 2 deletions sphinx/ext/viewcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import sphinx
from sphinx import addnodes
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.locale import _, __
from sphinx.pycode import ModuleAnalyzer
from sphinx.transforms.post_transforms import SphinxPostTransform
Expand All @@ -28,6 +27,7 @@

from sphinx.application import Sphinx
from sphinx.builders import Builder
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.environment import BuildEnvironment
from sphinx.util._pathlib import _StrPath
from sphinx.util.typing import ExtensionMetadata
Expand Down Expand Up @@ -228,7 +228,7 @@ def should_generate_module_page(app: Sphinx, modname: str) -> bool:
# Always (re-)generate module page when module filename is not found.
return True

builder = cast(StandaloneHTMLBuilder, app.builder)
builder = cast('StandaloneHTMLBuilder', app.builder)
basename = modname.replace('.', '/') + builder.out_suffix
page_filename = os.path.join(app.outdir, '_modules/', basename)

Expand Down
Loading

0 comments on commit b458850

Please sign in to comment.