From d6f49f9c090af33b4a53551a511aed7636d70402 Mon Sep 17 00:00:00 2001 From: danieleades <33452915+danieleades@users.noreply.github.com> Date: Thu, 14 Mar 2024 10:37:15 +0000 Subject: [PATCH] [lint] simplify lint config for flake8-annotations (#12073) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: daniel.eades Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .ruff.toml | 13 ++----- sphinx/addnodes.py | 2 +- sphinx/builders/gettext.py | 2 +- sphinx/config.py | 2 +- sphinx/domains/c/_ast.py | 22 ++++++------ sphinx/domains/c/_symbol.py | 4 ++- sphinx/domains/cpp/_ast.py | 54 +++++++++++++++--------------- sphinx/environment/__init__.py | 2 +- sphinx/ext/autosummary/__init__.py | 2 +- 9 files changed, 49 insertions(+), 54 deletions(-) diff --git a/.ruff.toml b/.ruff.toml index 52ea0155e..dd9d21352 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -15,6 +15,8 @@ exclude = [ "doc/usage/extensions/example*.py", ] ignore = [ + # flake8-annotations + "ANN401", # Dynamically typed expressions (typing.Any) are disallowed in `{name}` # pycodestyle "E741", # Ambiguous variable name: `{name}` # pyflakes @@ -31,16 +33,7 @@ select = [ # NOT YET USED # airflow ('AIR') # Airflow is not used in Sphinx - # flake8-annotations ('ANN') - "ANN001", # Missing type annotation for function argument `{name}` - "ANN002", # Missing type annotation for `*{name}` - "ANN003", # Missing type annotation for `**{name}` - "ANN201", # Missing return type annotation for public function `{name}` - "ANN202", # Missing return type annotation for private function `{name}` -# "ANN204", # Missing return type annotation for special method `{name}` - "ANN205", # Missing return type annotation for staticmethod `{name}` - "ANN206", # Missing return type annotation for classmethod `{name}` -# "ANN401", # Dynamically typed expressions (typing.Any) are disallowed in `{name}` + "ANN", # flake8-annotations ('ANN') # flake8-unused-arguments ('ARG') "ARG004", # Unused static method argument: `{name}` # flake8-async ('ASYNC') diff --git a/sphinx/addnodes.py b/sphinx/addnodes.py index 397d73fb6..12ed0dc5c 100644 --- a/sphinx/addnodes.py +++ b/sphinx/addnodes.py @@ -332,7 +332,7 @@ class desc_sig_element(nodes.inline, _desc_classes_injector): super().__init__(rawsource, text, *children, **attributes) self['classes'].extend(self.classes) - def __init_subclass__(cls, *, _sig_element: bool = False, **kwargs: Any): + def __init_subclass__(cls, *, _sig_element: bool = False, **kwargs: Any) -> None: super().__init_subclass__(**kwargs) if _sig_element: # add the class to the SIG_ELEMENTS set if asked diff --git a/sphinx/builders/gettext.py b/sphinx/builders/gettext.py index 71a700f40..0f29abe5d 100644 --- a/sphinx/builders/gettext.py +++ b/sphinx/builders/gettext.py @@ -40,7 +40,7 @@ logger = logging.getLogger(__name__) class Message: """An entry of translatable message.""" - def __init__(self, text: str, locations: list[tuple[str, int]], uuids: list[str]): + def __init__(self, text: str, locations: list[tuple[str, int]], uuids: list[str]) -> None: self.text = text self.locations = locations self.uuids = uuids diff --git a/sphinx/config.py b/sphinx/config.py index 5235c166e..e90c7ebfc 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -363,7 +363,7 @@ class Config: if name not in self._options: logger.warning(__('unknown config value %r in override, ignoring'), name) - def __repr__(self): + def __repr__(self) -> str: values = [] for opt_name in self._options: try: diff --git a/sphinx/domains/c/_ast.py b/sphinx/domains/c/_ast.py index 653977592..3a8e2a2a4 100644 --- a/sphinx/domains/c/_ast.py +++ b/sphinx/domains/c/_ast.py @@ -247,7 +247,7 @@ class ASTStringLiteral(ASTLiteral): class ASTIdExpression(ASTExpression): - def __init__(self, name: ASTNestedName): + def __init__(self, name: ASTNestedName) -> None: # note: this class is basically to cast a nested name as an expression self.name = name @@ -344,7 +344,7 @@ class ASTPostfixMemberOfPointer(ASTPostfixOp): class ASTPostfixExpr(ASTExpression): - def __init__(self, prefix: ASTExpression, postFixes: list[ASTPostfixOp]): + def __init__(self, prefix: ASTExpression, postFixes: list[ASTPostfixOp]) -> None: self.prefix = prefix self.postFixes = postFixes @@ -362,7 +362,7 @@ class ASTPostfixExpr(ASTExpression): ################################################################################ class ASTUnaryOpExpr(ASTExpression): - def __init__(self, op: str, expr: ASTExpression): + def __init__(self, op: str, expr: ASTExpression) -> None: self.op = op self.expr = expr @@ -398,7 +398,7 @@ class ASTSizeofType(ASTExpression): class ASTSizeofExpr(ASTExpression): - def __init__(self, expr: ASTExpression): + def __init__(self, expr: ASTExpression) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -412,7 +412,7 @@ class ASTSizeofExpr(ASTExpression): class ASTAlignofExpr(ASTExpression): - def __init__(self, typ: ASTType): + def __init__(self, typ: ASTType) -> None: self.typ = typ def _stringify(self, transform: StringifyTransform) -> str: @@ -430,7 +430,7 @@ class ASTAlignofExpr(ASTExpression): ################################################################################ class ASTCastExpr(ASTExpression): - def __init__(self, typ: ASTType, expr: ASTExpression): + def __init__(self, typ: ASTType, expr: ASTExpression) -> None: self.typ = typ self.expr = expr @@ -450,7 +450,7 @@ class ASTCastExpr(ASTExpression): class ASTBinOpExpr(ASTBase): - def __init__(self, exprs: list[ASTExpression], ops: list[str]): + def __init__(self, exprs: list[ASTExpression], ops: list[str]) -> None: assert len(exprs) > 0 assert len(exprs) == len(ops) + 1 self.exprs = exprs @@ -481,7 +481,7 @@ class ASTBinOpExpr(ASTBase): class ASTAssignmentExpr(ASTExpression): - def __init__(self, exprs: list[ASTExpression], ops: list[str]): + def __init__(self, exprs: list[ASTExpression], ops: list[str]) -> None: assert len(exprs) > 0 assert len(exprs) == len(ops) + 1 self.exprs = exprs @@ -512,7 +512,7 @@ class ASTAssignmentExpr(ASTExpression): class ASTFallbackExpr(ASTExpression): - def __init__(self, expr: str): + def __init__(self, expr: str) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -784,7 +784,7 @@ class ASTDeclSpecs(ASTBase): class ASTArray(ASTBase): def __init__(self, static: bool, const: bool, volatile: bool, restrict: bool, - vla: bool, size: ASTExpression): + vla: bool, size: ASTExpression) -> None: self.static = static self.const = const self.volatile = volatile @@ -895,7 +895,7 @@ class ASTDeclaratorNameParam(ASTDeclarator): class ASTDeclaratorNameBitField(ASTDeclarator): - def __init__(self, declId: ASTNestedName, size: ASTExpression): + def __init__(self, declId: ASTNestedName, size: ASTExpression) -> None: self.declId = declId self.size = size diff --git a/sphinx/domains/c/_symbol.py b/sphinx/domains/c/_symbol.py index b214882dd..c70130082 100644 --- a/sphinx/domains/c/_symbol.py +++ b/sphinx/domains/c/_symbol.py @@ -13,6 +13,8 @@ from sphinx.util import logging if TYPE_CHECKING: from collections.abc import Generator, Iterator + from typing_extensions import Self + from sphinx.environment import BuildEnvironment logger = logging.getLogger(__name__) @@ -52,7 +54,7 @@ class Symbol: debug_lookup = False debug_show_tree = False - def __copy__(self): + def __copy__(self) -> Self: raise AssertionError # shouldn't happen def __deepcopy__(self, memo: Any) -> Symbol: diff --git a/sphinx/domains/cpp/_ast.py b/sphinx/domains/cpp/_ast.py index 145db972f..ad57695d1 100644 --- a/sphinx/domains/cpp/_ast.py +++ b/sphinx/domains/cpp/_ast.py @@ -411,7 +411,7 @@ class ASTCharLiteral(ASTLiteral): class ASTUserDefinedLiteral(ASTLiteral): - def __init__(self, literal: ASTLiteral, ident: ASTIdentifier): + def __init__(self, literal: ASTLiteral, ident: ASTIdentifier) -> None: self.literal = literal self.ident = ident @@ -505,7 +505,7 @@ class ASTFoldExpr(ASTExpression): class ASTParenExpr(ASTExpression): - def __init__(self, expr: ASTExpression): + def __init__(self, expr: ASTExpression) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -522,7 +522,7 @@ class ASTParenExpr(ASTExpression): class ASTIdExpression(ASTExpression): - def __init__(self, name: ASTNestedName): + def __init__(self, name: ASTNestedName) -> None: # note: this class is basically to cast a nested name as an expression self.name = name @@ -550,7 +550,7 @@ class ASTPostfixOp(ASTBase): class ASTPostfixArray(ASTPostfixOp): - def __init__(self, expr: ASTExpression): + def __init__(self, expr: ASTExpression) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -567,7 +567,7 @@ class ASTPostfixArray(ASTPostfixOp): class ASTPostfixMember(ASTPostfixOp): - def __init__(self, name: ASTNestedName): + def __init__(self, name: ASTNestedName) -> None: self.name = name def _stringify(self, transform: StringifyTransform) -> str: @@ -583,7 +583,7 @@ class ASTPostfixMember(ASTPostfixOp): class ASTPostfixMemberOfPointer(ASTPostfixOp): - def __init__(self, name: ASTNestedName): + def __init__(self, name: ASTNestedName) -> None: self.name = name def _stringify(self, transform: StringifyTransform) -> str: @@ -643,7 +643,7 @@ class ASTPostfixCallExpr(ASTPostfixOp): class ASTPostfixExpr(ASTExpression): - def __init__(self, prefix: ASTType, postFixes: list[ASTPostfixOp]): + def __init__(self, prefix: ASTType, postFixes: list[ASTPostfixOp]) -> None: self.prefix = prefix self.postFixes = postFixes @@ -664,7 +664,7 @@ class ASTPostfixExpr(ASTExpression): class ASTExplicitCast(ASTExpression): - def __init__(self, cast: str, typ: ASTType, expr: ASTExpression): + def __init__(self, cast: str, typ: ASTType, expr: ASTExpression) -> None: assert cast in _id_explicit_cast self.cast = cast self.typ = typ @@ -696,7 +696,7 @@ class ASTExplicitCast(ASTExpression): class ASTTypeId(ASTExpression): - def __init__(self, typeOrExpr: ASTType | ASTExpression, isType: bool): + def __init__(self, typeOrExpr: ASTType | ASTExpression, isType: bool) -> None: self.typeOrExpr = typeOrExpr self.isType = isType @@ -719,7 +719,7 @@ class ASTTypeId(ASTExpression): ################################################################################ class ASTUnaryOpExpr(ASTExpression): - def __init__(self, op: str, expr: ASTExpression): + def __init__(self, op: str, expr: ASTExpression) -> None: self.op = op self.expr = expr @@ -743,7 +743,7 @@ class ASTUnaryOpExpr(ASTExpression): class ASTSizeofParamPack(ASTExpression): - def __init__(self, identifier: ASTIdentifier): + def __init__(self, identifier: ASTIdentifier) -> None: self.identifier = identifier def _stringify(self, transform: StringifyTransform) -> str: @@ -763,7 +763,7 @@ class ASTSizeofParamPack(ASTExpression): class ASTSizeofType(ASTExpression): - def __init__(self, typ: ASTType): + def __init__(self, typ: ASTType) -> None: self.typ = typ def _stringify(self, transform: StringifyTransform) -> str: @@ -781,7 +781,7 @@ class ASTSizeofType(ASTExpression): class ASTSizeofExpr(ASTExpression): - def __init__(self, expr: ASTExpression): + def __init__(self, expr: ASTExpression) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -798,7 +798,7 @@ class ASTSizeofExpr(ASTExpression): class ASTAlignofExpr(ASTExpression): - def __init__(self, typ: ASTType): + def __init__(self, typ: ASTType) -> None: self.typ = typ def _stringify(self, transform: StringifyTransform) -> str: @@ -816,7 +816,7 @@ class ASTAlignofExpr(ASTExpression): class ASTNoexceptExpr(ASTExpression): - def __init__(self, expr: ASTExpression): + def __init__(self, expr: ASTExpression) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -883,7 +883,7 @@ class ASTNewExpr(ASTExpression): class ASTDeleteExpr(ASTExpression): - def __init__(self, rooted: bool, array: bool, expr: ASTExpression): + def __init__(self, rooted: bool, array: bool, expr: ASTExpression) -> None: self.rooted = rooted self.array = array self.expr = expr @@ -921,7 +921,7 @@ class ASTDeleteExpr(ASTExpression): ################################################################################ class ASTCastExpr(ASTExpression): - def __init__(self, typ: ASTType, expr: ASTExpression): + def __init__(self, typ: ASTType, expr: ASTExpression) -> None: self.typ = typ self.expr = expr @@ -944,7 +944,7 @@ class ASTCastExpr(ASTExpression): class ASTBinOpExpr(ASTExpression): - def __init__(self, exprs: list[ASTExpression], ops: list[str]): + def __init__(self, exprs: list[ASTExpression], ops: list[str]) -> None: assert len(exprs) > 0 assert len(exprs) == len(ops) + 1 self.exprs = exprs @@ -985,7 +985,7 @@ class ASTBinOpExpr(ASTExpression): class ASTConditionalExpr(ASTExpression): def __init__(self, ifExpr: ASTExpression, thenExpr: ASTExpression, - elseExpr: ASTExpression): + elseExpr: ASTExpression) -> None: self.ifExpr = ifExpr self.thenExpr = thenExpr self.elseExpr = elseExpr @@ -1054,7 +1054,7 @@ class ASTBracedInitList(ASTBase): class ASTAssignmentExpr(ASTExpression): def __init__(self, leftExpr: ASTExpression, op: str, - rightExpr: ASTExpression | ASTBracedInitList): + rightExpr: ASTExpression | ASTBracedInitList) -> None: self.leftExpr = leftExpr self.op = op self.rightExpr = rightExpr @@ -1089,7 +1089,7 @@ class ASTAssignmentExpr(ASTExpression): class ASTCommaExpr(ASTExpression): - def __init__(self, exprs: list[ASTExpression]): + def __init__(self, exprs: list[ASTExpression]) -> None: assert len(exprs) > 0 self.exprs = exprs @@ -1115,7 +1115,7 @@ class ASTCommaExpr(ASTExpression): class ASTFallbackExpr(ASTExpression): - def __init__(self, expr: str): + def __init__(self, expr: str) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -1411,7 +1411,7 @@ class ASTTrailingTypeSpecDecltypeAuto(ASTTrailingTypeSpec): class ASTTrailingTypeSpecDecltype(ASTTrailingTypeSpec): - def __init__(self, expr: ASTExpression): + def __init__(self, expr: ASTExpression) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -1509,7 +1509,7 @@ class ASTFunctionParameter(ASTBase): class ASTNoexceptSpec(ASTBase): - def __init__(self, expr: ASTExpression | None): + def __init__(self, expr: ASTExpression | None) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: @@ -1870,7 +1870,7 @@ class ASTDeclSpecs(ASTBase): ################################################################################ class ASTArray(ASTBase): - def __init__(self, size: ASTExpression): + def __init__(self, size: ASTExpression) -> None: self.size = size def _stringify(self, transform: StringifyTransform) -> str: @@ -2033,7 +2033,7 @@ class ASTDeclaratorNameParamQual(ASTDeclarator): class ASTDeclaratorNameBitField(ASTDeclarator): - def __init__(self, declId: ASTNestedName, size: ASTExpression): + def __init__(self, declId: ASTNestedName, size: ASTExpression) -> None: self.declId = declId self.size = size @@ -2490,7 +2490,7 @@ class ASTDeclaratorParen(ASTDeclarator): ############################################################################################## class ASTPackExpansionExpr(ASTExpression): - def __init__(self, expr: ASTExpression | ASTBracedInitList): + def __init__(self, expr: ASTExpression | ASTBracedInitList) -> None: self.expr = expr def _stringify(self, transform: StringifyTransform) -> str: diff --git a/sphinx/environment/__init__.py b/sphinx/environment/__init__.py index 431734baa..53616fc4b 100644 --- a/sphinx/environment/__init__.py +++ b/sphinx/environment/__init__.py @@ -146,7 +146,7 @@ class BuildEnvironment: # --------- ENVIRONMENT INITIALIZATION ------------------------------------- - def __init__(self, app: Sphinx): + def __init__(self, app: Sphinx) -> None: self.app: Sphinx = app self.doctreedir: Path = app.doctreedir self.srcdir: Path = app.srcdir diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py index 9bcdc1f34..22e616c3f 100644 --- a/sphinx/ext/autosummary/__init__.py +++ b/sphinx/ext/autosummary/__init__.py @@ -605,7 +605,7 @@ class ImportExceptionGroup(Exception): It contains an error messages and a list of exceptions as its arguments. """ - def __init__(self, message: str | None, exceptions: Sequence[BaseException]): + def __init__(self, message: str | None, exceptions: Sequence[BaseException]) -> None: super().__init__(message) self.exceptions = list(exceptions)