diff options
Diffstat (limited to 'sphinx/pycode')
-rw-r--r-- | sphinx/pycode/__init__.py | 10 | ||||
-rw-r--r-- | sphinx/pycode/ast.py | 22 | ||||
-rw-r--r-- | sphinx/pycode/parser.py | 3 |
3 files changed, 17 insertions, 18 deletions
diff --git a/sphinx/pycode/__init__.py b/sphinx/pycode/__init__.py index c11579f96..19e478f57 100644 --- a/sphinx/pycode/__init__.py +++ b/sphinx/pycode/__init__.py @@ -76,11 +76,11 @@ class ModuleAnalyzer: @classmethod def for_string(cls, string: str, modname: str, srcname: str = '<string>' - ) -> "ModuleAnalyzer": + ) -> ModuleAnalyzer: return cls(string, modname, srcname) @classmethod - def for_file(cls, filename: str, modname: str) -> "ModuleAnalyzer": + def for_file(cls, filename: str, modname: str) -> ModuleAnalyzer: if ('file', filename) in cls.cache: return cls.cache['file', filename] try: @@ -96,7 +96,7 @@ class ModuleAnalyzer: return obj @classmethod - def for_egg(cls, filename: str, modname: str) -> "ModuleAnalyzer": + def for_egg(cls, filename: str, modname: str) -> ModuleAnalyzer: SEP = re.escape(path.sep) eggpath, relpath = re.split('(?<=\\.egg)' + SEP, filename) try: @@ -107,7 +107,7 @@ class ModuleAnalyzer: raise PycodeError('error opening %r' % filename, exc) from exc @classmethod - def for_module(cls, modname: str) -> "ModuleAnalyzer": + def for_module(cls, modname: str) -> ModuleAnalyzer: if ('module', modname) in cls.cache: entry = cls.cache['module', modname] if isinstance(entry, PycodeError): @@ -158,7 +158,7 @@ class ModuleAnalyzer: self.tagorder = parser.deforders self._analyzed = True except Exception as exc: - raise PycodeError('parsing %r failed: %r' % (self.srcname, exc)) from exc + raise PycodeError(f'parsing {self.srcname!r} failed: {exc!r}') from exc def find_attr_docs(self) -> dict[tuple[str, str], list[str]]: """Find class and module-level attributes and their documentation.""" diff --git a/sphinx/pycode/ast.py b/sphinx/pycode/ast.py index b85edb1b5..702006817 100644 --- a/sphinx/pycode/ast.py +++ b/sphinx/pycode/ast.py @@ -31,7 +31,7 @@ OPERATORS: dict[type[ast.AST], str] = { } -def parse(code: str, mode: str = 'exec') -> "ast.AST": +def parse(code: str, mode: str = 'exec') -> ast.AST: """Parse the *code* using the built-in ast module.""" warnings.warn( "'sphinx.pycode.ast.parse' is deprecated, use 'ast.parse' instead.", @@ -76,7 +76,7 @@ class _UnparseVisitor(ast.NodeVisitor): def visit_arg(self, node: ast.arg) -> str: if node.annotation: - return "%s: %s" % (node.arg, self.visit(node.annotation)) + return f"{node.arg}: {self.visit(node.annotation)}" else: return node.arg @@ -126,7 +126,7 @@ class _UnparseVisitor(ast.NodeVisitor): return ", ".join(args) def visit_Attribute(self, node: ast.Attribute) -> str: - return "%s.%s" % (self.visit(node.value), node.attr) + return f"{self.visit(node.value)}.{node.attr}" def visit_BinOp(self, node: ast.BinOp) -> str: # Special case ``**`` to not have surrounding spaces. @@ -139,9 +139,9 @@ class _UnparseVisitor(ast.NodeVisitor): return op.join(self.visit(e) for e in node.values) def visit_Call(self, node: ast.Call) -> str: - args = ([self.visit(e) for e in node.args] + - ["%s=%s" % (k.arg, self.visit(k.value)) for k in node.keywords]) - return "%s(%s)" % (self.visit(node.func), ", ".join(args)) + args = ', '.join([self.visit(e) for e in node.args] + + [f"{k.arg}={self.visit(k.value)}" for k in node.keywords]) + return f"{self.visit(node.func)}({args})" def visit_Constant(self, node: ast.Constant) -> str: if node.value is Ellipsis: @@ -185,19 +185,19 @@ class _UnparseVisitor(ast.NodeVisitor): if is_simple_tuple(node.slice): elts = ", ".join(self.visit(e) for e in node.slice.elts) # type: ignore - return "%s[%s]" % (self.visit(node.value), elts) + return f"{self.visit(node.value)}[{elts}]" elif isinstance(node.slice, ast.Index) and is_simple_tuple(node.slice.value): elts = ", ".join(self.visit(e) for e in node.slice.value.elts) # type: ignore - return "%s[%s]" % (self.visit(node.value), elts) + return f"{self.visit(node.value)}[{elts}]" else: - return "%s[%s]" % (self.visit(node.value), self.visit(node.slice)) + return f"{self.visit(node.value)}[{self.visit(node.slice)}]" def visit_UnaryOp(self, node: ast.UnaryOp) -> str: # UnaryOp is one of {UAdd, USub, Invert, Not}, which refer to ``+x``, # ``-x``, ``~x``, and ``not x``. Only Not needs a space. if isinstance(node.op, ast.Not): - return "%s %s" % (self.visit(node.op), self.visit(node.operand)) - return "%s%s" % (self.visit(node.op), self.visit(node.operand)) + return f"{self.visit(node.op)} {self.visit(node.operand)}" + return f"{self.visit(node.op)}{self.visit(node.operand)}" def visit_Tuple(self, node: ast.Tuple) -> str: if len(node.elts) == 0: diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py index 68a3523b3..7dd924822 100644 --- a/sphinx/pycode/parser.py +++ b/sphinx/pycode/parser.py @@ -118,8 +118,7 @@ class Token: return any(self == candidate for candidate in conditions) def __repr__(self) -> str: - return '<Token kind=%r value=%r>' % (tokenize.tok_name[self.kind], - self.value.strip()) + return f'<Token kind={tokenize.tok_name[self.kind]!r} value={self.value.strip()!r}>' class TokenProcessor: |