From 5d690beac48faf5e6d7f20deacffbc0e47dd1519 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Sun, 26 Jan 2025 19:29:08 +0900 Subject: [PATCH 1/3] Make default return value for NodeVisitor explicitly optional --- mypy/checkexpr.py | 4 +- mypy/checkpattern.py | 1 + mypy/nodes.py | 156 +++++++++--------- mypy/patterns.py | 18 +- mypy/strconv.py | 22 ++- mypy/stubgen.py | 93 ++++++++--- mypy/test/testmerge.py | 1 + mypy/treetransform.py | 1 + mypy/visitor.py | 327 ++++++++++++++++++------------------- mypy_self_check.ini | 4 - mypyc/irbuild/builder.py | 1 + mypyc/irbuild/statement.py | 1 + 12 files changed, 341 insertions(+), 288 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index a10dc00bb1de..c4156e2c6cfa 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -5944,7 +5944,9 @@ def accept( elif allow_none_return and isinstance(node, AwaitExpr): typ = self.visit_await_expr(node, allow_none_return=True) else: - typ = node.accept(self) + typ_ = node.accept(self) + assert typ_ is not None + typ = typ_ except Exception as err: report_internal_error( err, self.chk.errors.file, node.line, self.chk.errors, self.chk.options diff --git a/mypy/checkpattern.py b/mypy/checkpattern.py index 4b34c0ddb54b..cd47fb13df4c 100644 --- a/mypy/checkpattern.py +++ b/mypy/checkpattern.py @@ -130,6 +130,7 @@ def accept(self, o: Pattern, type_context: Type) -> PatternType: result = o.accept(self) self.type_context.pop() + assert result is not None return result def visit_as_pattern(self, o: AsPattern) -> PatternType: diff --git a/mypy/nodes.py b/mypy/nodes.py index b7b09f506c35..ee6431c179e5 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -181,7 +181,7 @@ def str_with_options(self, options: Options) -> str: assert ans return ans - def accept(self, visitor: NodeVisitor[T]) -> T: + def accept(self, visitor: NodeVisitor[T]) -> T | None: raise RuntimeError("Not implemented", type(self)) @@ -191,7 +191,7 @@ class Statement(Node): __slots__ = () - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: raise RuntimeError("Not implemented", type(self)) @@ -201,7 +201,7 @@ class Expression(Node): __slots__ = () - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: raise RuntimeError("Not implemented", type(self)) @@ -355,7 +355,7 @@ def name(self) -> str: def fullname(self) -> str: return self._fullname - def accept(self, visitor: NodeVisitor[T]) -> T: + def accept(self, visitor: NodeVisitor[T]) -> T | None: return visitor.visit_mypy_file(self) def is_package_init_file(self) -> bool: @@ -433,7 +433,7 @@ def __init__(self, ids: list[tuple[str, str | None]]) -> None: super().__init__() self.ids = ids - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_import(self) @@ -454,7 +454,7 @@ def __init__(self, id: str, relative: int, names: list[tuple[str, str | None]]) self.names = names self.relative = relative - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_import_from(self) @@ -473,7 +473,7 @@ def __init__(self, id: str, relative: int) -> None: self.id = id self.relative = relative - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_import_all(self) @@ -574,7 +574,7 @@ def name(self) -> str: assert self.impl is not None return self.impl.name - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_overloaded_func_def(self) def serialize(self) -> JsonDict: @@ -807,7 +807,7 @@ def __init__( def name(self) -> str: return self._name - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_func_def(self) def serialize(self) -> JsonDict: @@ -919,7 +919,7 @@ def info(self) -> TypeInfo: def type(self) -> mypy.types.Type | None: return self.var.type - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_decorator(self) def serialize(self) -> JsonDict: @@ -1065,7 +1065,7 @@ def name(self) -> str: def fullname(self) -> str: return self._fullname - def accept(self, visitor: NodeVisitor[T]) -> T: + def accept(self, visitor: NodeVisitor[T]) -> T | None: return visitor.visit_var(self) def serialize(self) -> JsonDict: @@ -1173,7 +1173,7 @@ def fullname(self) -> str: def fullname(self, v: str) -> None: self._fullname = v - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_class_def(self) def is_generic(self) -> bool: @@ -1218,7 +1218,7 @@ def __init__(self, names: list[str]) -> None: super().__init__() self.names = names - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_global_decl(self) @@ -1235,7 +1235,7 @@ def __init__(self, names: list[str]) -> None: super().__init__() self.names = names - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_nonlocal_decl(self) @@ -1254,7 +1254,7 @@ def __init__(self, body: list[Statement], *, is_unreachable: bool = False) -> No # in those cases. self.is_unreachable = is_unreachable - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_block(self) @@ -1274,7 +1274,7 @@ def __init__(self, expr: Expression) -> None: super().__init__() self.expr = expr - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_expression_stmt(self) @@ -1341,7 +1341,7 @@ def __init__( self.is_final_def = False self.invalid_recursive_alias = False - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_assignment_stmt(self) @@ -1362,7 +1362,7 @@ def __init__(self, op: str, lvalue: Lvalue, rvalue: Expression) -> None: self.lvalue = lvalue self.rvalue = rvalue - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_operator_assignment_stmt(self) @@ -1381,7 +1381,7 @@ def __init__(self, expr: Expression, body: Block, else_body: Block | None) -> No self.body = body self.else_body = else_body - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_while_stmt(self) @@ -1435,7 +1435,7 @@ def __init__( self.else_body = else_body self.is_async = False - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_for_stmt(self) @@ -1450,7 +1450,7 @@ def __init__(self, expr: Expression | None) -> None: super().__init__() self.expr = expr - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_return_stmt(self) @@ -1467,7 +1467,7 @@ def __init__(self, expr: Expression, msg: Expression | None = None) -> None: self.expr = expr self.msg = msg - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_assert_stmt(self) @@ -1482,28 +1482,28 @@ def __init__(self, expr: Lvalue) -> None: super().__init__() self.expr = expr - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_del_stmt(self) class BreakStmt(Statement): __slots__ = () - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_break_stmt(self) class ContinueStmt(Statement): __slots__ = () - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_continue_stmt(self) class PassStmt(Statement): __slots__ = () - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_pass_stmt(self) @@ -1522,7 +1522,7 @@ def __init__(self, expr: list[Expression], body: list[Block], else_body: Block | self.body = body self.else_body = else_body - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_if_stmt(self) @@ -1540,7 +1540,7 @@ def __init__(self, expr: Expression | None, from_expr: Expression | None) -> Non self.expr = expr self.from_expr = from_expr - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_raise_stmt(self) @@ -1577,7 +1577,7 @@ def __init__( self.finally_body = finally_body self.is_star = False - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_try_stmt(self) @@ -1610,7 +1610,7 @@ def __init__( self.body = body self.is_async = False - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_with_stmt(self) @@ -1638,7 +1638,7 @@ def __init__( self.guards = guards self.bodies = bodies - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_match_stmt(self) @@ -1661,7 +1661,7 @@ def __init__(self, name: NameExpr, type_args: list[TypeParam], value: LambdaExpr self.invalid_recursive_alias = False self.alias_node = None - def accept(self, visitor: StatementVisitor[T]) -> T: + def accept(self, visitor: StatementVisitor[T]) -> T | None: return visitor.visit_type_alias_stmt(self) @@ -1681,7 +1681,7 @@ def __init__(self, value: int) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_int_expr(self) @@ -1704,7 +1704,7 @@ def __init__(self, value: str) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_str_expr(self) @@ -1734,7 +1734,7 @@ def __init__(self, value: str) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_bytes_expr(self) @@ -1751,7 +1751,7 @@ def __init__(self, value: float) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_float_expr(self) @@ -1768,7 +1768,7 @@ def __init__(self, value: complex) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_complex_expr(self) @@ -1777,7 +1777,7 @@ class EllipsisExpr(Expression): __slots__ = () - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_ellipsis(self) @@ -1798,7 +1798,7 @@ def __init__(self, expr: Expression) -> None: # Whether this starred expression is used in a tuple/list and as lvalue self.valid = False - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_star_expr(self) @@ -1863,7 +1863,7 @@ def __init__(self, name: str) -> None: # Is this a l.h.s. of a special form assignment like typed dict or type variable? self.is_special_form = False - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_name_expr(self) def serialize(self) -> JsonDict: @@ -1885,7 +1885,7 @@ def __init__(self, expr: Expression, name: str) -> None: # The nodes of other kinds of member expressions are resolved during type checking. self.def_var: Var | None = None - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_member_expr(self) @@ -1961,7 +1961,7 @@ def __init__( # cast(...) this is a CastExpr. self.analyzed = analyzed - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_call_expr(self) @@ -1976,7 +1976,7 @@ def __init__(self, expr: Expression) -> None: super().__init__() self.expr = expr - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_yield_from_expr(self) @@ -1991,7 +1991,7 @@ def __init__(self, expr: Expression | None) -> None: super().__init__() self.expr = expr - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_yield_expr(self) @@ -2020,7 +2020,7 @@ def __init__(self, base: Expression, index: Expression) -> None: self.method_type = None self.analyzed = None - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_index_expr(self) @@ -2042,7 +2042,7 @@ def __init__(self, op: str, expr: Expression) -> None: self.expr = expr self.method_type = None - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_unary_expr(self) @@ -2058,7 +2058,7 @@ def __init__(self, target: Expression, value: Expression) -> None: self.target = target self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_assignment_expr(self) @@ -2104,7 +2104,7 @@ def __init__( self.right_unreachable = False self.analyzed = analyzed - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_op_expr(self) @@ -2133,7 +2133,7 @@ def pairwise(self) -> Iterator[tuple[str, Expression, Expression]]: for i, operator in enumerate(self.operators): yield operator, self.operands[i], self.operands[i + 1] - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_comparison_expr(self) @@ -2162,7 +2162,7 @@ def __init__( self.end_index = end_index self.stride = stride - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_slice_expr(self) @@ -2181,7 +2181,7 @@ def __init__(self, expr: Expression, typ: mypy.types.Type) -> None: self.expr = expr self.type = typ - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_cast_expr(self) @@ -2200,7 +2200,7 @@ def __init__(self, expr: Expression, typ: mypy.types.Type) -> None: self.expr = expr self.type = typ - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_assert_type_expr(self) @@ -2228,7 +2228,7 @@ def __init__( self.local_nodes = local_nodes self.is_imported = is_imported - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_reveal_expr(self) @@ -2249,7 +2249,7 @@ def __init__(self, name: str, call: CallExpr) -> None: self.call = call self.info = None - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_super_expr(self) @@ -2270,7 +2270,7 @@ def expr(self) -> Expression: assert expr is not None # lambda can't have empty body return expr - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_lambda_expr(self) def is_dynamic(self) -> bool: @@ -2290,7 +2290,7 @@ def __init__(self, items: list[Expression]) -> None: super().__init__() self.items = items - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_list_expr(self) @@ -2307,7 +2307,7 @@ def __init__(self, items: list[tuple[Expression | None, Expression]]) -> None: super().__init__() self.items = items - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_dict_expr(self) @@ -2326,7 +2326,7 @@ def __init__(self, items: list[Expression]) -> None: super().__init__() self.items = items - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_tuple_expr(self) @@ -2343,7 +2343,7 @@ def __init__(self, items: list[Expression]) -> None: super().__init__() self.items = items - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_set_expr(self) @@ -2375,7 +2375,7 @@ def __init__( self.indices = indices self.is_async = is_async - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_generator_expr(self) @@ -2392,7 +2392,7 @@ def __init__(self, generator: GeneratorExpr) -> None: super().__init__() self.generator = generator - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_list_comprehension(self) @@ -2409,7 +2409,7 @@ def __init__(self, generator: GeneratorExpr) -> None: super().__init__() self.generator = generator - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_set_comprehension(self) @@ -2444,7 +2444,7 @@ def __init__( self.indices = indices self.is_async = is_async - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_dictionary_comprehension(self) @@ -2465,7 +2465,7 @@ def __init__(self, cond: Expression, if_expr: Expression, else_expr: Expression) self.if_expr = if_expr self.else_expr = else_expr - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_conditional_expr(self) @@ -2484,7 +2484,7 @@ def __init__(self, expr: Expression, types: list[mypy.types.Type]) -> None: self.expr = expr self.types = types - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_type_application(self) @@ -2586,7 +2586,7 @@ def __init__( super().__init__(name, fullname, upper_bound, default, variance, is_new_style, line=line) self.values = values - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_type_var_expr(self) def serialize(self) -> JsonDict: @@ -2618,7 +2618,7 @@ class ParamSpecExpr(TypeVarLikeExpr): __match_args__ = ("name", "upper_bound", "default") - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_paramspec_expr(self) def serialize(self) -> JsonDict: @@ -2666,7 +2666,7 @@ def __init__( super().__init__(name, fullname, upper_bound, default, variance, is_new_style, line=line) self.tuple_fallback = tuple_fallback - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_type_var_tuple_expr(self) def serialize(self) -> JsonDict: @@ -2706,7 +2706,7 @@ def __init__(self, node: TypeAlias) -> None: super().__init__() self.node = node - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_type_alias_expr(self) @@ -2727,7 +2727,7 @@ def __init__(self, info: TypeInfo, is_typed: bool = False) -> None: self.info = info self.is_typed = is_typed - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_namedtuple_expr(self) @@ -2745,7 +2745,7 @@ def __init__(self, info: TypeInfo) -> None: super().__init__() self.info = info - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_typeddict_expr(self) @@ -2768,7 +2768,7 @@ def __init__(self, info: TypeInfo, items: list[str], values: list[Expression | N self.items = items self.values = values - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_enum_call_expr(self) @@ -2783,7 +2783,7 @@ def __init__(self, type: mypy.types.ProperType) -> None: super().__init__() self.type = type - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit__promote_expr(self) @@ -2808,7 +2808,7 @@ def __init__( self.old_type = old_type self.info = None - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_newtype_expr(self) @@ -2825,7 +2825,7 @@ def __init__(self, expr: Expression) -> None: super().__init__() self.expr = expr - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_await_expr(self) @@ -2861,7 +2861,7 @@ def __init__( def __repr__(self) -> str: return "TempNode:%d(%s)" % (self.line, str(self.type)) - def accept(self, visitor: ExpressionVisitor[T]) -> T: + def accept(self, visitor: ExpressionVisitor[T]) -> T | None: return visitor.visit_temp_node(self) @@ -3705,7 +3705,7 @@ def serialize(self) -> JsonDict: } return data - def accept(self, visitor: NodeVisitor[T]) -> T: + def accept(self, visitor: NodeVisitor[T]) -> T | None: return visitor.visit_type_alias(self) @classmethod @@ -3802,7 +3802,7 @@ def fullname(self) -> str: def serialize(self) -> JsonDict: assert False, "PlaceholderNode can't be serialized" - def accept(self, visitor: NodeVisitor[T]) -> T: + def accept(self, visitor: NodeVisitor[T]) -> T | None: return visitor.visit_placeholder_node(self) diff --git a/mypy/patterns.py b/mypy/patterns.py index a01bf6acc876..47a290f60902 100644 --- a/mypy/patterns.py +++ b/mypy/patterns.py @@ -18,7 +18,7 @@ class Pattern(Node): __slots__ = () - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: raise RuntimeError("Not implemented", type(self)) @@ -38,7 +38,7 @@ def __init__(self, pattern: Pattern | None, name: NameExpr | None) -> None: self.pattern = pattern self.name = name - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: return visitor.visit_as_pattern(self) @@ -51,7 +51,7 @@ def __init__(self, patterns: list[Pattern]) -> None: super().__init__() self.patterns = patterns - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: return visitor.visit_or_pattern(self) @@ -64,7 +64,7 @@ def __init__(self, expr: Expression) -> None: super().__init__() self.expr = expr - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: return visitor.visit_value_pattern(self) @@ -76,7 +76,7 @@ def __init__(self, value: bool | None) -> None: super().__init__() self.value = value - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: return visitor.visit_singleton_pattern(self) @@ -89,7 +89,7 @@ def __init__(self, patterns: list[Pattern]) -> None: super().__init__() self.patterns = patterns - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: return visitor.visit_sequence_pattern(self) @@ -102,7 +102,7 @@ def __init__(self, capture: NameExpr | None) -> None: super().__init__() self.capture = capture - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: return visitor.visit_starred_pattern(self) @@ -120,7 +120,7 @@ def __init__( self.values = values self.rest = rest - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: return visitor.visit_mapping_pattern(self) @@ -146,5 +146,5 @@ def __init__( self.keyword_keys = keyword_keys self.keyword_values = keyword_values - def accept(self, visitor: PatternVisitor[T]) -> T: + def accept(self, visitor: PatternVisitor[T]) -> T | None: return visitor.visit_class_pattern(self) diff --git a/mypy/strconv.py b/mypy/strconv.py index 3e9d37586f72..2102c76c66c4 100644 --- a/mypy/strconv.py +++ b/mypy/strconv.py @@ -182,7 +182,8 @@ def visit_class_def(self, o: mypy.nodes.ClassDef) -> str: if o.type_vars: a.insert(1, ("TypeVars", o.type_vars)) if o.metaclass: - a.insert(1, f"Metaclass({o.metaclass.accept(self)})") + inner = o.metaclass.accept(self) + a.insert(1, f"Metaclass({inner})") if o.decorators: a.insert(1, ("Decorators", o.decorators)) if o.info and o.info._promote: @@ -430,13 +431,16 @@ def visit_yield_expr(self, o: mypy.nodes.YieldExpr) -> str: def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr) -> str: if o.expr: - return self.dump([o.expr.accept(self)], o) + inner = o.expr.accept(self) + return self.dump([inner], o) else: return self.dump([], o) def visit_call_expr(self, o: mypy.nodes.CallExpr) -> str: if o.analyzed: - return o.analyzed.accept(self) + result = o.analyzed.accept(self) + assert result is not None + return result args: list[mypy.nodes.Expression] = [] extra: list[str | tuple[str, list[Any]]] = [] for i, kind in enumerate(o.arg_kinds): @@ -455,7 +459,9 @@ def visit_call_expr(self, o: mypy.nodes.CallExpr) -> str: def visit_op_expr(self, o: mypy.nodes.OpExpr) -> str: if o.analyzed: - return o.analyzed.accept(self) + result = o.analyzed.accept(self) + assert result is not None + return result return self.dump([o.op, o.left, o.right], o) def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr) -> str: @@ -494,7 +500,9 @@ def visit_tuple_expr(self, o: mypy.nodes.TupleExpr) -> str: def visit_index_expr(self, o: mypy.nodes.IndexExpr) -> str: if o.analyzed: - return o.analyzed.accept(self) + result = o.analyzed.accept(self) + assert result is not None + return result return self.dump([o.base, o.index], o) def visit_super_expr(self, o: mypy.nodes.SuperExpr) -> str: @@ -653,7 +661,9 @@ def dump_tagged(nodes: Sequence[object], tag: str | None, str_conv: StrConv) -> s = dump_tagged(n[1], n[0], str_conv) a.append(indent(s, 2)) elif isinstance(n, mypy.nodes.Node): - a.append(indent(n.accept(str_conv), 2)) + indented = n.accept(str_conv) + assert indented is not None + a.append(indent(indented, 2)) elif isinstance(n, Type): a.append( indent(n.accept(TypeStrVisitor(str_conv.id_mapper, options=str_conv.options)), 2) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 1f8a1a4740f1..9df68b762eee 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -285,14 +285,16 @@ def visit_call_expr(self, node: CallExpr) -> str: callee = node.callee.accept(self) args = [] for name, arg, kind in zip(node.arg_names, node.args, node.arg_kinds): + arg_str = arg.accept(self) + assert arg_str is not None if kind == ARG_POS: - args.append(arg.accept(self)) + args.append(arg_str) elif kind == ARG_STAR: - args.append("*" + arg.accept(self)) + args.append("*" + arg_str) elif kind == ARG_STAR2: - args.append("**" + arg.accept(self)) + args.append("**" + arg_str) elif kind == ARG_NAMED: - args.append(f"{name}={arg.accept(self)}") + args.append(f"{name}={arg_str}") else: raise ValueError(f"Unknown argument kind {kind} in call") return f"{callee}({', '.join(args)})" @@ -335,41 +337,65 @@ def visit_index_expr(self, node: IndexExpr) -> str: base_fullname = self.stubgen.get_fullname(node.base) if base_fullname == "typing.Union": if isinstance(node.index, TupleExpr): - return " | ".join([item.accept(self) for item in node.index.items]) - return node.index.accept(self) + items = [] + for item in node.index.items: + item_str = item.accept(self) + assert item_str is not None + items.append(item_str) + return " | ".join(items) + result = node.index.accept(self) + assert result is not None + return result if base_fullname == "typing.Optional": if isinstance(node.index, TupleExpr): return self.stubgen.add_name("_typeshed.Incomplete") - return f"{node.index.accept(self)} | None" + inner = node.index.accept(self) + return f"{inner} | None" base = node.base.accept(self) index = node.index.accept(self) + assert index is not None if len(index) > 2 and index.startswith("(") and index.endswith(")"): index = index[1:-1].rstrip(",") return f"{base}[{index}]" def visit_tuple_expr(self, node: TupleExpr) -> str: suffix = "," if len(node.items) == 1 else "" - return f"({', '.join(n.accept(self) for n in node.items)}{suffix})" + items = [] + for item in node.items: + item_str = item.accept(self) + assert item_str is not None + items.append(item_str) + return f"({', '.join(items)}{suffix})" def visit_list_expr(self, node: ListExpr) -> str: - return f"[{', '.join(n.accept(self) for n in node.items)}]" + items = [] + for item in node.items: + item_str = item.accept(self) + assert item_str is not None + items.append(item_str) + return f"[{', '.join(items)}]" def visit_dict_expr(self, o: DictExpr) -> str: dict_items = [] for key, value in o.items: # This is currently only used for TypedDict where all keys are strings. assert isinstance(key, StrExpr) - dict_items.append(f"{key.accept(self)}: {value.accept(self)}") + inner1 = key.accept(self) + inner2 = value.accept(self) + dict_items.append(f"{inner1}: {inner2}") return f"{{{', '.join(dict_items)}}}" def visit_ellipsis(self, node: EllipsisExpr) -> str: return "..." def visit_op_expr(self, o: OpExpr) -> str: - return f"{o.left.accept(self)} {o.op} {o.right.accept(self)}" + inner1 = o.left.accept(self) + inner2 = o.right.accept(self) + return f"{inner1} {o.op} {inner2}" def visit_star_expr(self, o: StarExpr) -> str: - return f"*{o.expr.accept(self)}" + inner = o.expr.accept(self) + return f"*{inner}" def visit_lambda_expr(self, o: LambdaExpr) -> str: # TODO: Required for among other things dataclass.field default_factory @@ -754,7 +780,8 @@ def process_decorator(self, o: Decorator) -> None: self.add_decorator(qualname, require_name=False) elif fullname in DATACLASS_TRANSFORM_NAMES: p = AliasPrinter(self) - self._decorators.append(f"@{decorator.accept(p)}") + inner = decorator.accept(p) + self._decorators.append(f"@{inner}") def get_fullname(self, expr: Expression) -> str: """Return the expression's full name.""" @@ -787,7 +814,7 @@ def visit_class_def(self, o: ClassDef) -> None: self.processing_enum = True if isinstance(o.metaclass, (NameExpr, MemberExpr)): meta = o.metaclass.accept(AliasPrinter(self)) - base_types.append("metaclass=" + meta) + base_types.append(f"metaclass={meta}") elif self.analyzed and o.info.is_abstract and not o.info.is_protocol: base_types.append("metaclass=abc.ABCMeta") self.import_tracker.add_import("abc") @@ -831,7 +858,9 @@ def get_base_types(self, cdef: ClassDef) -> list[str]: if self.get_fullname(base) != "builtins.object": base_types.append(get_qualified_name(base)) elif isinstance(base, IndexExpr): - base_types.append(base.accept(p)) + base_str = base.accept(p) + assert base_str is not None + base_types.append(base_str) elif isinstance(base, CallExpr): # namedtuple(typename, fields), NamedTuple(typename, fields) calls can # be used as a base class. The first argument is a string literal that @@ -854,7 +883,9 @@ def get_base_types(self, cdef: ClassDef) -> list[str]: namedtuple_name = self.add_name("typing.NamedTuple") base_types.append(f"{namedtuple_name}({typename!r}, [{fields_str}])") elif self.is_typed_namedtuple(base): - base_types.append(base.accept(p)) + base_str = base.accept(p) + assert base_str is not None + base_types.append(base_str) else: # At this point, we don't know what the base class is, so we # just use Incomplete as the base class. @@ -871,11 +902,15 @@ def get_class_decorators(self, cdef: ClassDef) -> list[str]: p = AliasPrinter(self) for d in cdef.decorators: if self.is_dataclass(d): - decorators.append(d.accept(p)) + d_str = d.accept(p) + assert d_str is not None + decorators.append(d_str) self.import_tracker.require_name(get_qualified_name(d)) self.processing_dataclass = True if self.is_dataclass_transform(d): - decorators.append(d.accept(p)) + d_str = d.accept(p) + assert d_str is not None + decorators.append(d_str) self.import_tracker.require_name(get_qualified_name(d)) return decorators @@ -986,7 +1021,9 @@ def _get_namedtuple_fields(self, call: CallExpr) -> list[tuple[str, str]] | None field_name, field_type = field.items if not isinstance(field_name, StrExpr): return None - fields.append((field_name.value, field_type.accept(p))) + field_type_str = field_type.accept(p) + assert field_type_str is not None + fields.append((field_name.value, field_type_str)) return fields else: return None # Not a named tuple call @@ -1056,13 +1093,15 @@ def process_typeddict(self, lvalue: NameExpr, rvalue: CallExpr) -> None: p = AliasPrinter(self) if any(not key.isidentifier() or keyword.iskeyword(key) for key, _ in items): # Keep the call syntax if there are non-identifier or reserved keyword keys. - self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n") + inner = rvalue.accept(p) + self.add(f"{self._indent}{lvalue.name} = {inner}\n") self._state = VAR else: bases = self.add_name("typing_extensions.TypedDict") # TODO: Add support for generic TypedDicts. Requires `Generic` as base class. if total is not None: - bases += f", total={total.accept(p)}" + inner = total.accept(p) + bases += f", total={inner}" class_def = f"{self._indent}class {lvalue.name}({bases}):" if len(items) == 0: self.add(f"{class_def} ...\n") @@ -1072,7 +1111,8 @@ def process_typeddict(self, lvalue: NameExpr, rvalue: CallExpr) -> None: self.add("\n") self.add(f"{class_def}\n") for key, key_type in items: - self.add(f"{self._indent} {key}: {key_type.accept(p)}\n") + inner = key_type.accept(p) + self.add(f"{self._indent} {key}: {inner}\n") self._state = CLASS def annotate_as_incomplete(self, lvalue: NameExpr) -> None: @@ -1144,7 +1184,8 @@ def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool: def process_typealias(self, lvalue: NameExpr, rvalue: Expression) -> None: p = AliasPrinter(self) - self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n") + inner = rvalue.accept(p) + self.add(f"{self._indent}{lvalue.name} = {inner}\n") self.record_name(lvalue.name) self._vars[-1].append(lvalue.name) @@ -1154,7 +1195,8 @@ def visit_type_alias_stmt(self, o: TypeAliasStmt) -> None: name = o.name.name rvalue = o.value.expr() type_args = self.format_type_args(o) - self.add(f"{self._indent}type {name}{type_args} = {rvalue.accept(p)}\n") + inner = rvalue.accept(p) + self.add(f"{self._indent}type {name}{type_args} = {inner}\n") self.record_name(name) self._vars[-1].append(name) @@ -1275,7 +1317,8 @@ def get_assign_initializer(self, rvalue: Expression) -> str: fullname = self.get_fullname(rvalue.callee) if fullname in (self.dataclass_field_specifier or DATACLASS_FIELD_SPECIFIERS): p = AliasPrinter(self) - return f" = {rvalue.accept(p)}" + inner = rvalue.accept(p) + return f" = {inner}" if not (isinstance(rvalue, TempNode) and rvalue.no_rhs): return " = ..." # TODO: support other possible cases, where initializer is important diff --git a/mypy/test/testmerge.py b/mypy/test/testmerge.py index 0582c9ed5882..65d1ca8d1ed1 100644 --- a/mypy/test/testmerge.py +++ b/mypy/test/testmerge.py @@ -150,6 +150,7 @@ def dump_asts(self, modules: dict[str, MypyFile]) -> list[str]: a = [] for m in sorted(modules): s = modules[m].accept(self.str_conv) + assert s is not None a.extend(s.splitlines()) return a diff --git a/mypy/treetransform.py b/mypy/treetransform.py index 3e5a7ef3f2ca..ec488fd89d39 100644 --- a/mypy/treetransform.py +++ b/mypy/treetransform.py @@ -695,6 +695,7 @@ def visit_temp_node(self, node: TempNode) -> TempNode: def node(self, node: Node) -> Node: new = node.accept(self) + assert new is not None new.set_line(node) return new diff --git a/mypy/visitor.py b/mypy/visitor.py index 6613b6cbb144..52d386fec708 100644 --- a/mypy/visitor.py +++ b/mypy/visitor.py @@ -20,179 +20,179 @@ @mypyc_attr(allow_interpreted_subclasses=True) class ExpressionVisitor(Generic[T]): @abstractmethod - def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T: + def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T | None: pass @abstractmethod - def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T: + def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T | None: pass @abstractmethod - def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T: + def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T | None: pass @abstractmethod - def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T: + def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T | None: pass @abstractmethod - def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T: + def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T | None: pass @abstractmethod - def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T: + def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T | None: pass @abstractmethod - def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T: + def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T | None: pass @abstractmethod - def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T: + def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T | None: pass @abstractmethod - def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T: + def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T | None: pass @abstractmethod - def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T: + def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T | None: pass @abstractmethod - def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T: + def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T | None: pass @abstractmethod - def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T: + def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T | None: pass @abstractmethod - def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T: + def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T | None: pass @abstractmethod - def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T: + def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T | None: pass @abstractmethod - def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T: + def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T | None: pass @abstractmethod - def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T: + def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T | None: pass @abstractmethod - def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T: + def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T | None: pass @abstractmethod - def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T: + def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T | None: pass @abstractmethod - def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T: + def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T | None: pass @abstractmethod - def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T: + def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T | None: pass @abstractmethod - def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T: + def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T | None: pass @abstractmethod - def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T: + def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T | None: pass @abstractmethod - def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T: + def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T | None: pass @abstractmethod - def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T: + def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T | None: pass @abstractmethod - def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T: + def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T | None: pass @abstractmethod - def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T: + def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T | None: pass @abstractmethod - def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T: + def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T | None: pass @abstractmethod - def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T: + def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T | None: pass @abstractmethod - def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T: + def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T | None: pass @abstractmethod - def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T: + def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T | None: pass @abstractmethod - def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T: + def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T | None: pass @abstractmethod - def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T: + def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T | None: pass @abstractmethod - def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T: + def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T | None: pass @abstractmethod - def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T: + def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T | None: pass @abstractmethod - def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T: + def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T | None: pass @abstractmethod - def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T: + def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T | None: pass @abstractmethod - def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T: + def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T | None: pass @abstractmethod - def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T: + def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T | None: pass @abstractmethod - def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T: + def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T | None: pass @abstractmethod - def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T: + def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T | None: pass @abstractmethod - def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T: + def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T | None: pass @abstractmethod - def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T: + def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T | None: pass @abstractmethod - def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T: + def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T | None: pass @abstractmethod - def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T: + def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T | None: pass @@ -202,115 +202,115 @@ class StatementVisitor(Generic[T]): # Definitions @abstractmethod - def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T: + def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T | None: pass @abstractmethod - def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T: + def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T | None: pass @abstractmethod - def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T: + def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T | None: pass @abstractmethod - def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T: + def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T | None: pass @abstractmethod - def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T: + def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T | None: pass @abstractmethod - def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T: + def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T | None: pass @abstractmethod - def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T: + def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T | None: pass @abstractmethod - def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T: + def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T | None: pass @abstractmethod - def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T: + def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T | None: pass @abstractmethod - def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T: + def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T | None: pass # Module structure @abstractmethod - def visit_import(self, o: mypy.nodes.Import, /) -> T: + def visit_import(self, o: mypy.nodes.Import, /) -> T | None: pass @abstractmethod - def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T: + def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T | None: pass @abstractmethod - def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T: + def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T | None: pass # Statements @abstractmethod - def visit_block(self, o: mypy.nodes.Block, /) -> T: + def visit_block(self, o: mypy.nodes.Block, /) -> T | None: pass @abstractmethod - def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T: + def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T | None: pass @abstractmethod - def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T: + def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T | None: pass @abstractmethod - def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T: + def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T | None: pass @abstractmethod - def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T: + def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T | None: pass @abstractmethod - def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T: + def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T | None: pass @abstractmethod - def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T: + def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T | None: pass @abstractmethod - def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T: + def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T | None: pass @abstractmethod - def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T: + def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T | None: pass @abstractmethod - def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T: + def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T | None: pass @abstractmethod - def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T: + def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T | None: pass @abstractmethod - def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T: + def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T | None: pass @abstractmethod - def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T: + def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T | None: pass @abstractmethod - def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T: + def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T | None: pass @@ -318,35 +318,35 @@ def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T: @mypyc_attr(allow_interpreted_subclasses=True) class PatternVisitor(Generic[T]): @abstractmethod - def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T: + def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T | None: pass @abstractmethod - def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T: + def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T | None: pass @abstractmethod - def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T: + def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T | None: pass @abstractmethod - def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T: + def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T | None: pass @abstractmethod - def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T: + def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T | None: pass @abstractmethod - def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T: + def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T | None: pass @abstractmethod - def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T: + def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T | None: pass @abstractmethod - def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T: + def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T | None: pass @@ -358,271 +358,268 @@ class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T], Pattern The T type argument specifies the return type of the visit methods. As all methods defined here return None by default, subclasses do not always need to override all the methods. - - TODO: make the default return value explicit, then turn on - empty body checking in mypy_self_check.ini. """ # Not in superclasses: - def visit_mypy_file(self, o: mypy.nodes.MypyFile, /) -> T: + def visit_mypy_file(self, o: mypy.nodes.MypyFile, /) -> T | None: pass # TODO: We have a visit_var method, but no visit_typeinfo or any # other non-Statement SymbolNode (accepting those will raise a # runtime error). Maybe this should be resolved in some direction. - def visit_var(self, o: mypy.nodes.Var, /) -> T: + def visit_var(self, o: mypy.nodes.Var, /) -> T | None: pass # Module structure - def visit_import(self, o: mypy.nodes.Import, /) -> T: + def visit_import(self, o: mypy.nodes.Import, /) -> T | None: pass - def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T: + def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T | None: pass - def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T: + def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T | None: pass # Definitions - def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T: + def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T | None: pass - def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T: + def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T | None: pass - def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T: + def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T | None: pass - def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T: + def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T | None: pass - def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T: + def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T | None: pass - def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T: + def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T | None: pass - def visit_type_alias(self, o: mypy.nodes.TypeAlias, /) -> T: + def visit_type_alias(self, o: mypy.nodes.TypeAlias, /) -> T | None: pass - def visit_placeholder_node(self, o: mypy.nodes.PlaceholderNode, /) -> T: + def visit_placeholder_node(self, o: mypy.nodes.PlaceholderNode, /) -> T | None: pass # Statements - def visit_block(self, o: mypy.nodes.Block, /) -> T: + def visit_block(self, o: mypy.nodes.Block, /) -> T | None: pass - def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T: + def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T | None: pass - def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T: + def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T | None: pass - def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T: + def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T | None: pass - def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T: + def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T | None: pass - def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T: + def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T | None: pass - def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T: + def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T | None: pass - def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T: + def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T | None: pass - def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T: + def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T | None: pass - def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T: + def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T | None: pass - def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T: + def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T | None: pass - def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T: + def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T | None: pass - def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T: + def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T | None: pass - def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T: + def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T | None: pass - def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T: + def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T | None: pass - def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T: + def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T | None: pass - def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T: + def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T | None: pass - def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T: + def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T | None: pass # Expressions (default no-op implementation) - def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T: + def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T | None: pass - def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T: + def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T | None: pass - def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T: + def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T | None: pass - def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T: + def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T | None: pass - def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T: + def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T | None: pass - def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T: + def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T | None: pass - def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T: + def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T | None: pass - def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T: + def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T | None: pass - def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T: + def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T | None: pass - def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T: + def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T | None: pass - def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T: + def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T | None: pass - def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T: + def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T | None: pass - def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T: + def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T | None: pass - def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T: + def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T | None: pass - def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T: + def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T | None: pass - def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T: + def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T | None: pass - def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T: + def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T | None: pass - def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T: + def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T | None: pass - def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T: + def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T | None: pass - def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T: + def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T | None: pass - def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T: + def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T | None: pass - def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T: + def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T | None: pass - def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T: + def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T | None: pass - def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T: + def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T | None: pass - def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T: + def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T | None: pass - def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T: + def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T | None: pass - def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T: + def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T | None: pass - def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T: + def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T | None: pass - def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T: + def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T | None: pass - def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T: + def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T | None: pass - def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T: + def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T | None: pass - def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T: + def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T | None: pass - def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T: + def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T | None: pass - def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T: + def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T | None: pass - def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T: + def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T | None: pass - def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T: + def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T | None: pass - def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T: + def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T | None: pass - def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T: + def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T | None: pass - def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T: + def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T | None: pass - def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T: + def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T | None: pass - def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T: + def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T | None: pass - def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T: + def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T | None: pass - def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T: + def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T | None: pass - def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T: + def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T | None: pass # Patterns - def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T: + def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T | None: pass - def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T: + def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T | None: pass - def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T: + def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T | None: pass - def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T: + def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T | None: pass - def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T: + def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T | None: pass - def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T: + def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T | None: pass - def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T: + def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T | None: pass - def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T: + def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T | None: pass diff --git a/mypy_self_check.ini b/mypy_self_check.ini index f54c1f17f025..7198a1f6f342 100644 --- a/mypy_self_check.ini +++ b/mypy_self_check.ini @@ -12,7 +12,3 @@ exclude = mypy/typeshed/|mypyc/test-data/|mypyc/lib-rt/ enable_error_code = ignore-without-code,redundant-expr enable_incomplete_feature = PreciseTupleTypes show_error_code_links = True - -[mypy-mypy.visitor] -# See docstring for NodeVisitor for motivation. -disable_error_code = empty-body diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index b0597617bdc5..154e0cd9798e 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -274,6 +274,7 @@ def accept(self, node: Statement | Expression, *, can_borrow: bool = False) -> V self.can_borrow = can_borrow try: res = node.accept(self.visitor) + assert res is not None res = self.coerce(res, self.node_type(node), node.line) # If we hit an error during compilation, we want to # keep trying, so we can produce more error diff --git a/mypyc/irbuild/statement.py b/mypyc/irbuild/statement.py index cdc1d54589eb..d5801dbcf619 100644 --- a/mypyc/irbuild/statement.py +++ b/mypyc/irbuild/statement.py @@ -1048,6 +1048,7 @@ def transform_type_alias_stmt(builder: IRBuilder, s: TypeAliasStmt) -> None: # The value needs to be lazily computed to match Python runtime behavior, but # Python public APIs don't support this, so we use a C primitive. compute_fn = s.value.accept(builder.visitor) + assert compute_fn is not None builder.builder.primitive_op(set_type_alias_compute_function_op, [alias, compute_fn], line) target = builder.get_assignment_target(s.name) From d80e041db4034262aae5ff676f9727398c58c586 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Tue, 28 Jan 2025 05:28:21 +0900 Subject: [PATCH 2/3] Revert "Make default return value for NodeVisitor explicitly optional" This reverts commit 5d690beac48faf5e6d7f20deacffbc0e47dd1519. --- mypy/checkexpr.py | 4 +- mypy/checkpattern.py | 1 - mypy/nodes.py | 156 +++++++++--------- mypy/patterns.py | 18 +- mypy/strconv.py | 22 +-- mypy/stubgen.py | 93 +++-------- mypy/test/testmerge.py | 1 - mypy/treetransform.py | 1 - mypy/visitor.py | 327 +++++++++++++++++++------------------ mypy_self_check.ini | 4 + mypyc/irbuild/builder.py | 1 - mypyc/irbuild/statement.py | 1 - 12 files changed, 288 insertions(+), 341 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c4156e2c6cfa..a10dc00bb1de 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -5944,9 +5944,7 @@ def accept( elif allow_none_return and isinstance(node, AwaitExpr): typ = self.visit_await_expr(node, allow_none_return=True) else: - typ_ = node.accept(self) - assert typ_ is not None - typ = typ_ + typ = node.accept(self) except Exception as err: report_internal_error( err, self.chk.errors.file, node.line, self.chk.errors, self.chk.options diff --git a/mypy/checkpattern.py b/mypy/checkpattern.py index cd47fb13df4c..4b34c0ddb54b 100644 --- a/mypy/checkpattern.py +++ b/mypy/checkpattern.py @@ -130,7 +130,6 @@ def accept(self, o: Pattern, type_context: Type) -> PatternType: result = o.accept(self) self.type_context.pop() - assert result is not None return result def visit_as_pattern(self, o: AsPattern) -> PatternType: diff --git a/mypy/nodes.py b/mypy/nodes.py index ee6431c179e5..b7b09f506c35 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -181,7 +181,7 @@ def str_with_options(self, options: Options) -> str: assert ans return ans - def accept(self, visitor: NodeVisitor[T]) -> T | None: + def accept(self, visitor: NodeVisitor[T]) -> T: raise RuntimeError("Not implemented", type(self)) @@ -191,7 +191,7 @@ class Statement(Node): __slots__ = () - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: raise RuntimeError("Not implemented", type(self)) @@ -201,7 +201,7 @@ class Expression(Node): __slots__ = () - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: raise RuntimeError("Not implemented", type(self)) @@ -355,7 +355,7 @@ def name(self) -> str: def fullname(self) -> str: return self._fullname - def accept(self, visitor: NodeVisitor[T]) -> T | None: + def accept(self, visitor: NodeVisitor[T]) -> T: return visitor.visit_mypy_file(self) def is_package_init_file(self) -> bool: @@ -433,7 +433,7 @@ def __init__(self, ids: list[tuple[str, str | None]]) -> None: super().__init__() self.ids = ids - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_import(self) @@ -454,7 +454,7 @@ def __init__(self, id: str, relative: int, names: list[tuple[str, str | None]]) self.names = names self.relative = relative - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_import_from(self) @@ -473,7 +473,7 @@ def __init__(self, id: str, relative: int) -> None: self.id = id self.relative = relative - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_import_all(self) @@ -574,7 +574,7 @@ def name(self) -> str: assert self.impl is not None return self.impl.name - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_overloaded_func_def(self) def serialize(self) -> JsonDict: @@ -807,7 +807,7 @@ def __init__( def name(self) -> str: return self._name - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_func_def(self) def serialize(self) -> JsonDict: @@ -919,7 +919,7 @@ def info(self) -> TypeInfo: def type(self) -> mypy.types.Type | None: return self.var.type - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_decorator(self) def serialize(self) -> JsonDict: @@ -1065,7 +1065,7 @@ def name(self) -> str: def fullname(self) -> str: return self._fullname - def accept(self, visitor: NodeVisitor[T]) -> T | None: + def accept(self, visitor: NodeVisitor[T]) -> T: return visitor.visit_var(self) def serialize(self) -> JsonDict: @@ -1173,7 +1173,7 @@ def fullname(self) -> str: def fullname(self, v: str) -> None: self._fullname = v - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_class_def(self) def is_generic(self) -> bool: @@ -1218,7 +1218,7 @@ def __init__(self, names: list[str]) -> None: super().__init__() self.names = names - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_global_decl(self) @@ -1235,7 +1235,7 @@ def __init__(self, names: list[str]) -> None: super().__init__() self.names = names - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_nonlocal_decl(self) @@ -1254,7 +1254,7 @@ def __init__(self, body: list[Statement], *, is_unreachable: bool = False) -> No # in those cases. self.is_unreachable = is_unreachable - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_block(self) @@ -1274,7 +1274,7 @@ def __init__(self, expr: Expression) -> None: super().__init__() self.expr = expr - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_expression_stmt(self) @@ -1341,7 +1341,7 @@ def __init__( self.is_final_def = False self.invalid_recursive_alias = False - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_assignment_stmt(self) @@ -1362,7 +1362,7 @@ def __init__(self, op: str, lvalue: Lvalue, rvalue: Expression) -> None: self.lvalue = lvalue self.rvalue = rvalue - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_operator_assignment_stmt(self) @@ -1381,7 +1381,7 @@ def __init__(self, expr: Expression, body: Block, else_body: Block | None) -> No self.body = body self.else_body = else_body - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_while_stmt(self) @@ -1435,7 +1435,7 @@ def __init__( self.else_body = else_body self.is_async = False - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_for_stmt(self) @@ -1450,7 +1450,7 @@ def __init__(self, expr: Expression | None) -> None: super().__init__() self.expr = expr - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_return_stmt(self) @@ -1467,7 +1467,7 @@ def __init__(self, expr: Expression, msg: Expression | None = None) -> None: self.expr = expr self.msg = msg - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_assert_stmt(self) @@ -1482,28 +1482,28 @@ def __init__(self, expr: Lvalue) -> None: super().__init__() self.expr = expr - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_del_stmt(self) class BreakStmt(Statement): __slots__ = () - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_break_stmt(self) class ContinueStmt(Statement): __slots__ = () - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_continue_stmt(self) class PassStmt(Statement): __slots__ = () - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_pass_stmt(self) @@ -1522,7 +1522,7 @@ def __init__(self, expr: list[Expression], body: list[Block], else_body: Block | self.body = body self.else_body = else_body - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_if_stmt(self) @@ -1540,7 +1540,7 @@ def __init__(self, expr: Expression | None, from_expr: Expression | None) -> Non self.expr = expr self.from_expr = from_expr - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_raise_stmt(self) @@ -1577,7 +1577,7 @@ def __init__( self.finally_body = finally_body self.is_star = False - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_try_stmt(self) @@ -1610,7 +1610,7 @@ def __init__( self.body = body self.is_async = False - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_with_stmt(self) @@ -1638,7 +1638,7 @@ def __init__( self.guards = guards self.bodies = bodies - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_match_stmt(self) @@ -1661,7 +1661,7 @@ def __init__(self, name: NameExpr, type_args: list[TypeParam], value: LambdaExpr self.invalid_recursive_alias = False self.alias_node = None - def accept(self, visitor: StatementVisitor[T]) -> T | None: + def accept(self, visitor: StatementVisitor[T]) -> T: return visitor.visit_type_alias_stmt(self) @@ -1681,7 +1681,7 @@ def __init__(self, value: int) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_int_expr(self) @@ -1704,7 +1704,7 @@ def __init__(self, value: str) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_str_expr(self) @@ -1734,7 +1734,7 @@ def __init__(self, value: str) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_bytes_expr(self) @@ -1751,7 +1751,7 @@ def __init__(self, value: float) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_float_expr(self) @@ -1768,7 +1768,7 @@ def __init__(self, value: complex) -> None: super().__init__() self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_complex_expr(self) @@ -1777,7 +1777,7 @@ class EllipsisExpr(Expression): __slots__ = () - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_ellipsis(self) @@ -1798,7 +1798,7 @@ def __init__(self, expr: Expression) -> None: # Whether this starred expression is used in a tuple/list and as lvalue self.valid = False - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_star_expr(self) @@ -1863,7 +1863,7 @@ def __init__(self, name: str) -> None: # Is this a l.h.s. of a special form assignment like typed dict or type variable? self.is_special_form = False - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_name_expr(self) def serialize(self) -> JsonDict: @@ -1885,7 +1885,7 @@ def __init__(self, expr: Expression, name: str) -> None: # The nodes of other kinds of member expressions are resolved during type checking. self.def_var: Var | None = None - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_member_expr(self) @@ -1961,7 +1961,7 @@ def __init__( # cast(...) this is a CastExpr. self.analyzed = analyzed - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_call_expr(self) @@ -1976,7 +1976,7 @@ def __init__(self, expr: Expression) -> None: super().__init__() self.expr = expr - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_yield_from_expr(self) @@ -1991,7 +1991,7 @@ def __init__(self, expr: Expression | None) -> None: super().__init__() self.expr = expr - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_yield_expr(self) @@ -2020,7 +2020,7 @@ def __init__(self, base: Expression, index: Expression) -> None: self.method_type = None self.analyzed = None - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_index_expr(self) @@ -2042,7 +2042,7 @@ def __init__(self, op: str, expr: Expression) -> None: self.expr = expr self.method_type = None - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_unary_expr(self) @@ -2058,7 +2058,7 @@ def __init__(self, target: Expression, value: Expression) -> None: self.target = target self.value = value - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_assignment_expr(self) @@ -2104,7 +2104,7 @@ def __init__( self.right_unreachable = False self.analyzed = analyzed - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_op_expr(self) @@ -2133,7 +2133,7 @@ def pairwise(self) -> Iterator[tuple[str, Expression, Expression]]: for i, operator in enumerate(self.operators): yield operator, self.operands[i], self.operands[i + 1] - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_comparison_expr(self) @@ -2162,7 +2162,7 @@ def __init__( self.end_index = end_index self.stride = stride - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_slice_expr(self) @@ -2181,7 +2181,7 @@ def __init__(self, expr: Expression, typ: mypy.types.Type) -> None: self.expr = expr self.type = typ - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_cast_expr(self) @@ -2200,7 +2200,7 @@ def __init__(self, expr: Expression, typ: mypy.types.Type) -> None: self.expr = expr self.type = typ - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_assert_type_expr(self) @@ -2228,7 +2228,7 @@ def __init__( self.local_nodes = local_nodes self.is_imported = is_imported - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_reveal_expr(self) @@ -2249,7 +2249,7 @@ def __init__(self, name: str, call: CallExpr) -> None: self.call = call self.info = None - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_super_expr(self) @@ -2270,7 +2270,7 @@ def expr(self) -> Expression: assert expr is not None # lambda can't have empty body return expr - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_lambda_expr(self) def is_dynamic(self) -> bool: @@ -2290,7 +2290,7 @@ def __init__(self, items: list[Expression]) -> None: super().__init__() self.items = items - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_list_expr(self) @@ -2307,7 +2307,7 @@ def __init__(self, items: list[tuple[Expression | None, Expression]]) -> None: super().__init__() self.items = items - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_dict_expr(self) @@ -2326,7 +2326,7 @@ def __init__(self, items: list[Expression]) -> None: super().__init__() self.items = items - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_tuple_expr(self) @@ -2343,7 +2343,7 @@ def __init__(self, items: list[Expression]) -> None: super().__init__() self.items = items - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_set_expr(self) @@ -2375,7 +2375,7 @@ def __init__( self.indices = indices self.is_async = is_async - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_generator_expr(self) @@ -2392,7 +2392,7 @@ def __init__(self, generator: GeneratorExpr) -> None: super().__init__() self.generator = generator - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_list_comprehension(self) @@ -2409,7 +2409,7 @@ def __init__(self, generator: GeneratorExpr) -> None: super().__init__() self.generator = generator - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_set_comprehension(self) @@ -2444,7 +2444,7 @@ def __init__( self.indices = indices self.is_async = is_async - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_dictionary_comprehension(self) @@ -2465,7 +2465,7 @@ def __init__(self, cond: Expression, if_expr: Expression, else_expr: Expression) self.if_expr = if_expr self.else_expr = else_expr - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_conditional_expr(self) @@ -2484,7 +2484,7 @@ def __init__(self, expr: Expression, types: list[mypy.types.Type]) -> None: self.expr = expr self.types = types - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_type_application(self) @@ -2586,7 +2586,7 @@ def __init__( super().__init__(name, fullname, upper_bound, default, variance, is_new_style, line=line) self.values = values - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_type_var_expr(self) def serialize(self) -> JsonDict: @@ -2618,7 +2618,7 @@ class ParamSpecExpr(TypeVarLikeExpr): __match_args__ = ("name", "upper_bound", "default") - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_paramspec_expr(self) def serialize(self) -> JsonDict: @@ -2666,7 +2666,7 @@ def __init__( super().__init__(name, fullname, upper_bound, default, variance, is_new_style, line=line) self.tuple_fallback = tuple_fallback - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_type_var_tuple_expr(self) def serialize(self) -> JsonDict: @@ -2706,7 +2706,7 @@ def __init__(self, node: TypeAlias) -> None: super().__init__() self.node = node - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_type_alias_expr(self) @@ -2727,7 +2727,7 @@ def __init__(self, info: TypeInfo, is_typed: bool = False) -> None: self.info = info self.is_typed = is_typed - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_namedtuple_expr(self) @@ -2745,7 +2745,7 @@ def __init__(self, info: TypeInfo) -> None: super().__init__() self.info = info - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_typeddict_expr(self) @@ -2768,7 +2768,7 @@ def __init__(self, info: TypeInfo, items: list[str], values: list[Expression | N self.items = items self.values = values - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_enum_call_expr(self) @@ -2783,7 +2783,7 @@ def __init__(self, type: mypy.types.ProperType) -> None: super().__init__() self.type = type - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit__promote_expr(self) @@ -2808,7 +2808,7 @@ def __init__( self.old_type = old_type self.info = None - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_newtype_expr(self) @@ -2825,7 +2825,7 @@ def __init__(self, expr: Expression) -> None: super().__init__() self.expr = expr - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_await_expr(self) @@ -2861,7 +2861,7 @@ def __init__( def __repr__(self) -> str: return "TempNode:%d(%s)" % (self.line, str(self.type)) - def accept(self, visitor: ExpressionVisitor[T]) -> T | None: + def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_temp_node(self) @@ -3705,7 +3705,7 @@ def serialize(self) -> JsonDict: } return data - def accept(self, visitor: NodeVisitor[T]) -> T | None: + def accept(self, visitor: NodeVisitor[T]) -> T: return visitor.visit_type_alias(self) @classmethod @@ -3802,7 +3802,7 @@ def fullname(self) -> str: def serialize(self) -> JsonDict: assert False, "PlaceholderNode can't be serialized" - def accept(self, visitor: NodeVisitor[T]) -> T | None: + def accept(self, visitor: NodeVisitor[T]) -> T: return visitor.visit_placeholder_node(self) diff --git a/mypy/patterns.py b/mypy/patterns.py index 47a290f60902..a01bf6acc876 100644 --- a/mypy/patterns.py +++ b/mypy/patterns.py @@ -18,7 +18,7 @@ class Pattern(Node): __slots__ = () - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: raise RuntimeError("Not implemented", type(self)) @@ -38,7 +38,7 @@ def __init__(self, pattern: Pattern | None, name: NameExpr | None) -> None: self.pattern = pattern self.name = name - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: return visitor.visit_as_pattern(self) @@ -51,7 +51,7 @@ def __init__(self, patterns: list[Pattern]) -> None: super().__init__() self.patterns = patterns - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: return visitor.visit_or_pattern(self) @@ -64,7 +64,7 @@ def __init__(self, expr: Expression) -> None: super().__init__() self.expr = expr - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: return visitor.visit_value_pattern(self) @@ -76,7 +76,7 @@ def __init__(self, value: bool | None) -> None: super().__init__() self.value = value - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: return visitor.visit_singleton_pattern(self) @@ -89,7 +89,7 @@ def __init__(self, patterns: list[Pattern]) -> None: super().__init__() self.patterns = patterns - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: return visitor.visit_sequence_pattern(self) @@ -102,7 +102,7 @@ def __init__(self, capture: NameExpr | None) -> None: super().__init__() self.capture = capture - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: return visitor.visit_starred_pattern(self) @@ -120,7 +120,7 @@ def __init__( self.values = values self.rest = rest - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: return visitor.visit_mapping_pattern(self) @@ -146,5 +146,5 @@ def __init__( self.keyword_keys = keyword_keys self.keyword_values = keyword_values - def accept(self, visitor: PatternVisitor[T]) -> T | None: + def accept(self, visitor: PatternVisitor[T]) -> T: return visitor.visit_class_pattern(self) diff --git a/mypy/strconv.py b/mypy/strconv.py index 2102c76c66c4..3e9d37586f72 100644 --- a/mypy/strconv.py +++ b/mypy/strconv.py @@ -182,8 +182,7 @@ def visit_class_def(self, o: mypy.nodes.ClassDef) -> str: if o.type_vars: a.insert(1, ("TypeVars", o.type_vars)) if o.metaclass: - inner = o.metaclass.accept(self) - a.insert(1, f"Metaclass({inner})") + a.insert(1, f"Metaclass({o.metaclass.accept(self)})") if o.decorators: a.insert(1, ("Decorators", o.decorators)) if o.info and o.info._promote: @@ -431,16 +430,13 @@ def visit_yield_expr(self, o: mypy.nodes.YieldExpr) -> str: def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr) -> str: if o.expr: - inner = o.expr.accept(self) - return self.dump([inner], o) + return self.dump([o.expr.accept(self)], o) else: return self.dump([], o) def visit_call_expr(self, o: mypy.nodes.CallExpr) -> str: if o.analyzed: - result = o.analyzed.accept(self) - assert result is not None - return result + return o.analyzed.accept(self) args: list[mypy.nodes.Expression] = [] extra: list[str | tuple[str, list[Any]]] = [] for i, kind in enumerate(o.arg_kinds): @@ -459,9 +455,7 @@ def visit_call_expr(self, o: mypy.nodes.CallExpr) -> str: def visit_op_expr(self, o: mypy.nodes.OpExpr) -> str: if o.analyzed: - result = o.analyzed.accept(self) - assert result is not None - return result + return o.analyzed.accept(self) return self.dump([o.op, o.left, o.right], o) def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr) -> str: @@ -500,9 +494,7 @@ def visit_tuple_expr(self, o: mypy.nodes.TupleExpr) -> str: def visit_index_expr(self, o: mypy.nodes.IndexExpr) -> str: if o.analyzed: - result = o.analyzed.accept(self) - assert result is not None - return result + return o.analyzed.accept(self) return self.dump([o.base, o.index], o) def visit_super_expr(self, o: mypy.nodes.SuperExpr) -> str: @@ -661,9 +653,7 @@ def dump_tagged(nodes: Sequence[object], tag: str | None, str_conv: StrConv) -> s = dump_tagged(n[1], n[0], str_conv) a.append(indent(s, 2)) elif isinstance(n, mypy.nodes.Node): - indented = n.accept(str_conv) - assert indented is not None - a.append(indent(indented, 2)) + a.append(indent(n.accept(str_conv), 2)) elif isinstance(n, Type): a.append( indent(n.accept(TypeStrVisitor(str_conv.id_mapper, options=str_conv.options)), 2) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 9df68b762eee..1f8a1a4740f1 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -285,16 +285,14 @@ def visit_call_expr(self, node: CallExpr) -> str: callee = node.callee.accept(self) args = [] for name, arg, kind in zip(node.arg_names, node.args, node.arg_kinds): - arg_str = arg.accept(self) - assert arg_str is not None if kind == ARG_POS: - args.append(arg_str) + args.append(arg.accept(self)) elif kind == ARG_STAR: - args.append("*" + arg_str) + args.append("*" + arg.accept(self)) elif kind == ARG_STAR2: - args.append("**" + arg_str) + args.append("**" + arg.accept(self)) elif kind == ARG_NAMED: - args.append(f"{name}={arg_str}") + args.append(f"{name}={arg.accept(self)}") else: raise ValueError(f"Unknown argument kind {kind} in call") return f"{callee}({', '.join(args)})" @@ -337,65 +335,41 @@ def visit_index_expr(self, node: IndexExpr) -> str: base_fullname = self.stubgen.get_fullname(node.base) if base_fullname == "typing.Union": if isinstance(node.index, TupleExpr): - items = [] - for item in node.index.items: - item_str = item.accept(self) - assert item_str is not None - items.append(item_str) - return " | ".join(items) - result = node.index.accept(self) - assert result is not None - return result + return " | ".join([item.accept(self) for item in node.index.items]) + return node.index.accept(self) if base_fullname == "typing.Optional": if isinstance(node.index, TupleExpr): return self.stubgen.add_name("_typeshed.Incomplete") - inner = node.index.accept(self) - return f"{inner} | None" + return f"{node.index.accept(self)} | None" base = node.base.accept(self) index = node.index.accept(self) - assert index is not None if len(index) > 2 and index.startswith("(") and index.endswith(")"): index = index[1:-1].rstrip(",") return f"{base}[{index}]" def visit_tuple_expr(self, node: TupleExpr) -> str: suffix = "," if len(node.items) == 1 else "" - items = [] - for item in node.items: - item_str = item.accept(self) - assert item_str is not None - items.append(item_str) - return f"({', '.join(items)}{suffix})" + return f"({', '.join(n.accept(self) for n in node.items)}{suffix})" def visit_list_expr(self, node: ListExpr) -> str: - items = [] - for item in node.items: - item_str = item.accept(self) - assert item_str is not None - items.append(item_str) - return f"[{', '.join(items)}]" + return f"[{', '.join(n.accept(self) for n in node.items)}]" def visit_dict_expr(self, o: DictExpr) -> str: dict_items = [] for key, value in o.items: # This is currently only used for TypedDict where all keys are strings. assert isinstance(key, StrExpr) - inner1 = key.accept(self) - inner2 = value.accept(self) - dict_items.append(f"{inner1}: {inner2}") + dict_items.append(f"{key.accept(self)}: {value.accept(self)}") return f"{{{', '.join(dict_items)}}}" def visit_ellipsis(self, node: EllipsisExpr) -> str: return "..." def visit_op_expr(self, o: OpExpr) -> str: - inner1 = o.left.accept(self) - inner2 = o.right.accept(self) - return f"{inner1} {o.op} {inner2}" + return f"{o.left.accept(self)} {o.op} {o.right.accept(self)}" def visit_star_expr(self, o: StarExpr) -> str: - inner = o.expr.accept(self) - return f"*{inner}" + return f"*{o.expr.accept(self)}" def visit_lambda_expr(self, o: LambdaExpr) -> str: # TODO: Required for among other things dataclass.field default_factory @@ -780,8 +754,7 @@ def process_decorator(self, o: Decorator) -> None: self.add_decorator(qualname, require_name=False) elif fullname in DATACLASS_TRANSFORM_NAMES: p = AliasPrinter(self) - inner = decorator.accept(p) - self._decorators.append(f"@{inner}") + self._decorators.append(f"@{decorator.accept(p)}") def get_fullname(self, expr: Expression) -> str: """Return the expression's full name.""" @@ -814,7 +787,7 @@ def visit_class_def(self, o: ClassDef) -> None: self.processing_enum = True if isinstance(o.metaclass, (NameExpr, MemberExpr)): meta = o.metaclass.accept(AliasPrinter(self)) - base_types.append(f"metaclass={meta}") + base_types.append("metaclass=" + meta) elif self.analyzed and o.info.is_abstract and not o.info.is_protocol: base_types.append("metaclass=abc.ABCMeta") self.import_tracker.add_import("abc") @@ -858,9 +831,7 @@ def get_base_types(self, cdef: ClassDef) -> list[str]: if self.get_fullname(base) != "builtins.object": base_types.append(get_qualified_name(base)) elif isinstance(base, IndexExpr): - base_str = base.accept(p) - assert base_str is not None - base_types.append(base_str) + base_types.append(base.accept(p)) elif isinstance(base, CallExpr): # namedtuple(typename, fields), NamedTuple(typename, fields) calls can # be used as a base class. The first argument is a string literal that @@ -883,9 +854,7 @@ def get_base_types(self, cdef: ClassDef) -> list[str]: namedtuple_name = self.add_name("typing.NamedTuple") base_types.append(f"{namedtuple_name}({typename!r}, [{fields_str}])") elif self.is_typed_namedtuple(base): - base_str = base.accept(p) - assert base_str is not None - base_types.append(base_str) + base_types.append(base.accept(p)) else: # At this point, we don't know what the base class is, so we # just use Incomplete as the base class. @@ -902,15 +871,11 @@ def get_class_decorators(self, cdef: ClassDef) -> list[str]: p = AliasPrinter(self) for d in cdef.decorators: if self.is_dataclass(d): - d_str = d.accept(p) - assert d_str is not None - decorators.append(d_str) + decorators.append(d.accept(p)) self.import_tracker.require_name(get_qualified_name(d)) self.processing_dataclass = True if self.is_dataclass_transform(d): - d_str = d.accept(p) - assert d_str is not None - decorators.append(d_str) + decorators.append(d.accept(p)) self.import_tracker.require_name(get_qualified_name(d)) return decorators @@ -1021,9 +986,7 @@ def _get_namedtuple_fields(self, call: CallExpr) -> list[tuple[str, str]] | None field_name, field_type = field.items if not isinstance(field_name, StrExpr): return None - field_type_str = field_type.accept(p) - assert field_type_str is not None - fields.append((field_name.value, field_type_str)) + fields.append((field_name.value, field_type.accept(p))) return fields else: return None # Not a named tuple call @@ -1093,15 +1056,13 @@ def process_typeddict(self, lvalue: NameExpr, rvalue: CallExpr) -> None: p = AliasPrinter(self) if any(not key.isidentifier() or keyword.iskeyword(key) for key, _ in items): # Keep the call syntax if there are non-identifier or reserved keyword keys. - inner = rvalue.accept(p) - self.add(f"{self._indent}{lvalue.name} = {inner}\n") + self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n") self._state = VAR else: bases = self.add_name("typing_extensions.TypedDict") # TODO: Add support for generic TypedDicts. Requires `Generic` as base class. if total is not None: - inner = total.accept(p) - bases += f", total={inner}" + bases += f", total={total.accept(p)}" class_def = f"{self._indent}class {lvalue.name}({bases}):" if len(items) == 0: self.add(f"{class_def} ...\n") @@ -1111,8 +1072,7 @@ def process_typeddict(self, lvalue: NameExpr, rvalue: CallExpr) -> None: self.add("\n") self.add(f"{class_def}\n") for key, key_type in items: - inner = key_type.accept(p) - self.add(f"{self._indent} {key}: {inner}\n") + self.add(f"{self._indent} {key}: {key_type.accept(p)}\n") self._state = CLASS def annotate_as_incomplete(self, lvalue: NameExpr) -> None: @@ -1184,8 +1144,7 @@ def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool: def process_typealias(self, lvalue: NameExpr, rvalue: Expression) -> None: p = AliasPrinter(self) - inner = rvalue.accept(p) - self.add(f"{self._indent}{lvalue.name} = {inner}\n") + self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n") self.record_name(lvalue.name) self._vars[-1].append(lvalue.name) @@ -1195,8 +1154,7 @@ def visit_type_alias_stmt(self, o: TypeAliasStmt) -> None: name = o.name.name rvalue = o.value.expr() type_args = self.format_type_args(o) - inner = rvalue.accept(p) - self.add(f"{self._indent}type {name}{type_args} = {inner}\n") + self.add(f"{self._indent}type {name}{type_args} = {rvalue.accept(p)}\n") self.record_name(name) self._vars[-1].append(name) @@ -1317,8 +1275,7 @@ def get_assign_initializer(self, rvalue: Expression) -> str: fullname = self.get_fullname(rvalue.callee) if fullname in (self.dataclass_field_specifier or DATACLASS_FIELD_SPECIFIERS): p = AliasPrinter(self) - inner = rvalue.accept(p) - return f" = {inner}" + return f" = {rvalue.accept(p)}" if not (isinstance(rvalue, TempNode) and rvalue.no_rhs): return " = ..." # TODO: support other possible cases, where initializer is important diff --git a/mypy/test/testmerge.py b/mypy/test/testmerge.py index 65d1ca8d1ed1..0582c9ed5882 100644 --- a/mypy/test/testmerge.py +++ b/mypy/test/testmerge.py @@ -150,7 +150,6 @@ def dump_asts(self, modules: dict[str, MypyFile]) -> list[str]: a = [] for m in sorted(modules): s = modules[m].accept(self.str_conv) - assert s is not None a.extend(s.splitlines()) return a diff --git a/mypy/treetransform.py b/mypy/treetransform.py index ec488fd89d39..3e5a7ef3f2ca 100644 --- a/mypy/treetransform.py +++ b/mypy/treetransform.py @@ -695,7 +695,6 @@ def visit_temp_node(self, node: TempNode) -> TempNode: def node(self, node: Node) -> Node: new = node.accept(self) - assert new is not None new.set_line(node) return new diff --git a/mypy/visitor.py b/mypy/visitor.py index 52d386fec708..6613b6cbb144 100644 --- a/mypy/visitor.py +++ b/mypy/visitor.py @@ -20,179 +20,179 @@ @mypyc_attr(allow_interpreted_subclasses=True) class ExpressionVisitor(Generic[T]): @abstractmethod - def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T | None: + def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T: pass @abstractmethod - def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T | None: + def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T: pass @abstractmethod - def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T | None: + def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T: pass @abstractmethod - def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T | None: + def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T: pass @abstractmethod - def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T | None: + def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T: pass @abstractmethod - def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T | None: + def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T: pass @abstractmethod - def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T | None: + def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T: pass @abstractmethod - def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T | None: + def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T: pass @abstractmethod - def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T | None: + def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T: pass @abstractmethod - def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T | None: + def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T: pass @abstractmethod - def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T | None: + def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T: pass @abstractmethod - def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T | None: + def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T: pass @abstractmethod - def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T | None: + def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T: pass @abstractmethod - def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T | None: + def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T: pass @abstractmethod - def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T | None: + def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T: pass @abstractmethod - def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T | None: + def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T: pass @abstractmethod - def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T | None: + def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T: pass @abstractmethod - def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T | None: + def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T: pass @abstractmethod - def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T | None: + def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T: pass @abstractmethod - def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T | None: + def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T: pass @abstractmethod - def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T | None: + def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T: pass @abstractmethod - def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T | None: + def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T: pass @abstractmethod - def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T | None: + def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T: pass @abstractmethod - def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T | None: + def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T: pass @abstractmethod - def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T | None: + def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T: pass @abstractmethod - def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T | None: + def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T: pass @abstractmethod - def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T | None: + def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T: pass @abstractmethod - def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T | None: + def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T: pass @abstractmethod - def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T | None: + def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T: pass @abstractmethod - def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T | None: + def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T: pass @abstractmethod - def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T | None: + def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T: pass @abstractmethod - def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T | None: + def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T: pass @abstractmethod - def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T | None: + def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T: pass @abstractmethod - def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T | None: + def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T: pass @abstractmethod - def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T | None: + def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T: pass @abstractmethod - def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T | None: + def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T: pass @abstractmethod - def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T | None: + def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T: pass @abstractmethod - def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T | None: + def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T: pass @abstractmethod - def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T | None: + def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T: pass @abstractmethod - def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T | None: + def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T: pass @abstractmethod - def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T | None: + def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T: pass @abstractmethod - def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T | None: + def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T: pass @abstractmethod - def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T | None: + def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T: pass @abstractmethod - def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T | None: + def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T: pass @@ -202,115 +202,115 @@ class StatementVisitor(Generic[T]): # Definitions @abstractmethod - def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T | None: + def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T: pass @abstractmethod - def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T | None: + def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T: pass @abstractmethod - def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T | None: + def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T: pass @abstractmethod - def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T | None: + def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T: pass @abstractmethod - def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T | None: + def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T: pass @abstractmethod - def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T | None: + def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T: pass @abstractmethod - def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T | None: + def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T: pass @abstractmethod - def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T | None: + def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T: pass @abstractmethod - def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T | None: + def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T: pass @abstractmethod - def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T | None: + def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T: pass # Module structure @abstractmethod - def visit_import(self, o: mypy.nodes.Import, /) -> T | None: + def visit_import(self, o: mypy.nodes.Import, /) -> T: pass @abstractmethod - def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T | None: + def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T: pass @abstractmethod - def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T | None: + def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T: pass # Statements @abstractmethod - def visit_block(self, o: mypy.nodes.Block, /) -> T | None: + def visit_block(self, o: mypy.nodes.Block, /) -> T: pass @abstractmethod - def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T | None: + def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T: pass @abstractmethod - def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T | None: + def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T: pass @abstractmethod - def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T | None: + def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T: pass @abstractmethod - def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T | None: + def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T: pass @abstractmethod - def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T | None: + def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T: pass @abstractmethod - def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T | None: + def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T: pass @abstractmethod - def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T | None: + def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T: pass @abstractmethod - def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T | None: + def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T: pass @abstractmethod - def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T | None: + def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T: pass @abstractmethod - def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T | None: + def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T: pass @abstractmethod - def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T | None: + def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T: pass @abstractmethod - def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T | None: + def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T: pass @abstractmethod - def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T | None: + def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T: pass @@ -318,35 +318,35 @@ def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T | None: @mypyc_attr(allow_interpreted_subclasses=True) class PatternVisitor(Generic[T]): @abstractmethod - def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T | None: + def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T: pass @abstractmethod - def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T | None: + def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T: pass @abstractmethod - def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T | None: + def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T: pass @abstractmethod - def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T | None: + def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T: pass @abstractmethod - def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T | None: + def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T: pass @abstractmethod - def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T | None: + def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T: pass @abstractmethod - def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T | None: + def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T: pass @abstractmethod - def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T | None: + def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T: pass @@ -358,268 +358,271 @@ class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T], Pattern The T type argument specifies the return type of the visit methods. As all methods defined here return None by default, subclasses do not always need to override all the methods. + + TODO: make the default return value explicit, then turn on + empty body checking in mypy_self_check.ini. """ # Not in superclasses: - def visit_mypy_file(self, o: mypy.nodes.MypyFile, /) -> T | None: + def visit_mypy_file(self, o: mypy.nodes.MypyFile, /) -> T: pass # TODO: We have a visit_var method, but no visit_typeinfo or any # other non-Statement SymbolNode (accepting those will raise a # runtime error). Maybe this should be resolved in some direction. - def visit_var(self, o: mypy.nodes.Var, /) -> T | None: + def visit_var(self, o: mypy.nodes.Var, /) -> T: pass # Module structure - def visit_import(self, o: mypy.nodes.Import, /) -> T | None: + def visit_import(self, o: mypy.nodes.Import, /) -> T: pass - def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T | None: + def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T: pass - def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T | None: + def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T: pass # Definitions - def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T | None: + def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T: pass - def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T | None: + def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T: pass - def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T | None: + def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T: pass - def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T | None: + def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T: pass - def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T | None: + def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T: pass - def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T | None: + def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T: pass - def visit_type_alias(self, o: mypy.nodes.TypeAlias, /) -> T | None: + def visit_type_alias(self, o: mypy.nodes.TypeAlias, /) -> T: pass - def visit_placeholder_node(self, o: mypy.nodes.PlaceholderNode, /) -> T | None: + def visit_placeholder_node(self, o: mypy.nodes.PlaceholderNode, /) -> T: pass # Statements - def visit_block(self, o: mypy.nodes.Block, /) -> T | None: + def visit_block(self, o: mypy.nodes.Block, /) -> T: pass - def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T | None: + def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T: pass - def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T | None: + def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T: pass - def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T | None: + def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T: pass - def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T | None: + def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T: pass - def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T | None: + def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T: pass - def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T | None: + def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T: pass - def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T | None: + def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T: pass - def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T | None: + def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T: pass - def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T | None: + def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T: pass - def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T | None: + def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T: pass - def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T | None: + def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T: pass - def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T | None: + def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T: pass - def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T | None: + def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T: pass - def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T | None: + def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T: pass - def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T | None: + def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T: pass - def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T | None: + def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T: pass - def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T | None: + def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T: pass # Expressions (default no-op implementation) - def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T | None: + def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T: pass - def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T | None: + def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T: pass - def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T | None: + def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T: pass - def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T | None: + def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T: pass - def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T | None: + def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T: pass - def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T | None: + def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T: pass - def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T | None: + def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T: pass - def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T | None: + def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T: pass - def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T | None: + def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T: pass - def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T | None: + def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T: pass - def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T | None: + def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T: pass - def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T | None: + def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T: pass - def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T | None: + def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T: pass - def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T | None: + def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T: pass - def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T | None: + def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T: pass - def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T | None: + def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T: pass - def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T | None: + def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T: pass - def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T | None: + def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T: pass - def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T | None: + def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T: pass - def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T | None: + def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T: pass - def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T | None: + def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T: pass - def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T | None: + def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T: pass - def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T | None: + def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T: pass - def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T | None: + def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T: pass - def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T | None: + def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T: pass - def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T | None: + def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T: pass - def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T | None: + def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T: pass - def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T | None: + def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T: pass - def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T | None: + def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T: pass - def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T | None: + def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T: pass - def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T | None: + def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T: pass - def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T | None: + def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T: pass - def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T | None: + def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T: pass - def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T | None: + def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T: pass - def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T | None: + def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T: pass - def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T | None: + def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T: pass - def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T | None: + def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T: pass - def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T | None: + def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T: pass - def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T | None: + def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T: pass - def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T | None: + def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T: pass - def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T | None: + def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T: pass - def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T | None: + def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T: pass - def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T | None: + def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T: pass - def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T | None: + def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T: pass # Patterns - def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T | None: + def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T: pass - def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T | None: + def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T: pass - def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T | None: + def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T: pass - def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T | None: + def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T: pass - def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T | None: + def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T: pass - def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T | None: + def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T: pass - def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T | None: + def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T: pass - def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T | None: + def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T: pass diff --git a/mypy_self_check.ini b/mypy_self_check.ini index 7198a1f6f342..f54c1f17f025 100644 --- a/mypy_self_check.ini +++ b/mypy_self_check.ini @@ -12,3 +12,7 @@ exclude = mypy/typeshed/|mypyc/test-data/|mypyc/lib-rt/ enable_error_code = ignore-without-code,redundant-expr enable_incomplete_feature = PreciseTupleTypes show_error_code_links = True + +[mypy-mypy.visitor] +# See docstring for NodeVisitor for motivation. +disable_error_code = empty-body diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index 154e0cd9798e..b0597617bdc5 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -274,7 +274,6 @@ def accept(self, node: Statement | Expression, *, can_borrow: bool = False) -> V self.can_borrow = can_borrow try: res = node.accept(self.visitor) - assert res is not None res = self.coerce(res, self.node_type(node), node.line) # If we hit an error during compilation, we want to # keep trying, so we can produce more error diff --git a/mypyc/irbuild/statement.py b/mypyc/irbuild/statement.py index d5801dbcf619..cdc1d54589eb 100644 --- a/mypyc/irbuild/statement.py +++ b/mypyc/irbuild/statement.py @@ -1048,7 +1048,6 @@ def transform_type_alias_stmt(builder: IRBuilder, s: TypeAliasStmt) -> None: # The value needs to be lazily computed to match Python runtime behavior, but # Python public APIs don't support this, so we use a C primitive. compute_fn = s.value.accept(builder.visitor) - assert compute_fn is not None builder.builder.primitive_op(set_type_alias_compute_function_op, [alias, compute_fn], line) target = builder.get_assignment_target(s.name) From a06b55dd9c5ef57947b8f252b5ccb78f5de3bc1f Mon Sep 17 00:00:00 2001 From: A5rocks Date: Tue, 28 Jan 2025 10:41:51 +0900 Subject: [PATCH 3/3] Raise instead of returning None --- mypy/checker.py | 13 ++++ mypy/semanal.py | 33 +++++++++ mypy/traverser.py | 82 +++++++++++++++++++++ mypy/visitor.py | 171 ++++++++++++++++++++++---------------------- mypy_self_check.ini | 4 -- 5 files changed, 212 insertions(+), 91 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index 7b0b88186f76..91501f66e6de 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -77,6 +77,7 @@ FuncBase, FuncDef, FuncItem, + GlobalDecl, IfStmt, Import, ImportAll, @@ -92,6 +93,7 @@ MypyFile, NameExpr, Node, + NonlocalDecl, OperatorAssignmentStmt, OpExpr, OverloadedFuncDef, @@ -7766,6 +7768,17 @@ def warn_deprecated_overload_item( if candidate == target: self.warn_deprecated(item.func, context) + # leafs + + def visit_pass_stmt(self, o: PassStmt, /) -> None: + return None + + def visit_nonlocal_decl(self, o: NonlocalDecl, /) -> None: + return None + + def visit_global_decl(self, o: GlobalDecl, /) -> None: + return None + class CollectArgTypeVarTypes(TypeTraverserVisitor): """Collects the non-nested argument types in a set.""" diff --git a/mypy/semanal.py b/mypy/semanal.py index 034d8fb28b42..e9776325a496 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -97,10 +97,12 @@ AwaitExpr, Block, BreakStmt, + BytesExpr, CallExpr, CastExpr, ClassDef, ComparisonExpr, + ComplexExpr, ConditionalExpr, Context, ContinueStmt, @@ -114,6 +116,7 @@ Expression, ExpressionStmt, FakeExpression, + FloatExpr, ForStmt, FuncBase, FuncDef, @@ -126,6 +129,7 @@ ImportBase, ImportFrom, IndexExpr, + IntExpr, LambdaExpr, ListComprehension, ListExpr, @@ -193,6 +197,7 @@ MappingPattern, OrPattern, SequencePattern, + SingletonPattern, StarredPattern, ValuePattern, ) @@ -7522,6 +7527,34 @@ def parse_dataclass_transform_field_specifiers(self, arg: Expression) -> tuple[s names.append(specifier.fullname) return tuple(names) + # leafs + def visit_int_expr(self, o: IntExpr, /) -> None: + return None + + def visit_str_expr(self, o: StrExpr, /) -> None: + return None + + def visit_bytes_expr(self, o: BytesExpr, /) -> None: + return None + + def visit_float_expr(self, o: FloatExpr, /) -> None: + return None + + def visit_complex_expr(self, o: ComplexExpr, /) -> None: + return None + + def visit_ellipsis(self, o: EllipsisExpr, /) -> None: + return None + + def visit_temp_node(self, o: TempNode, /) -> None: + return None + + def visit_pass_stmt(self, o: PassStmt, /) -> None: + return None + + def visit_singleton_pattern(self, o: SingletonPattern, /) -> None: + return None + def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike: if isinstance(sig, CallableType): diff --git a/mypy/traverser.py b/mypy/traverser.py index 2c8ea49491bc..7d7794822396 100644 --- a/mypy/traverser.py +++ b/mypy/traverser.py @@ -58,6 +58,7 @@ OverloadedFuncDef, ParamSpecExpr, PassStmt, + PromoteExpr, RaiseStmt, ReturnStmt, RevealExpr, @@ -67,6 +68,7 @@ StarExpr, StrExpr, SuperExpr, + TempNode, TryStmt, TupleExpr, TypeAlias, @@ -77,6 +79,7 @@ TypeVarExpr, TypeVarTupleExpr, UnaryExpr, + Var, WhileStmt, WithStmt, YieldExpr, @@ -415,6 +418,85 @@ def visit_import_from(self, o: ImportFrom, /) -> None: for a in o.assignments: a.accept(self) + # leaf nodes + def visit_name_expr(self, o: NameExpr, /) -> None: + return None + + def visit_str_expr(self, o: StrExpr, /) -> None: + return None + + def visit_int_expr(self, o: IntExpr, /) -> None: + return None + + def visit_float_expr(self, o: FloatExpr, /) -> None: + return None + + def visit_bytes_expr(self, o: BytesExpr, /) -> None: + return None + + def visit_ellipsis(self, o: EllipsisExpr, /) -> None: + return None + + def visit_var(self, o: Var, /) -> None: + return None + + def visit_continue_stmt(self, o: ContinueStmt, /) -> None: + return None + + def visit_pass_stmt(self, o: PassStmt, /) -> None: + return None + + def visit_break_stmt(self, o: BreakStmt, /) -> None: + return None + + def visit_temp_node(self, o: TempNode, /) -> None: + return None + + def visit_nonlocal_decl(self, o: NonlocalDecl, /) -> None: + return None + + def visit_global_decl(self, o: GlobalDecl, /) -> None: + return None + + def visit_import_all(self, o: ImportAll, /) -> None: + return None + + def visit_type_var_expr(self, o: TypeVarExpr, /) -> None: + return None + + def visit_paramspec_expr(self, o: ParamSpecExpr, /) -> None: + return None + + def visit_type_var_tuple_expr(self, o: TypeVarTupleExpr, /) -> None: + return None + + def visit_type_alias_expr(self, o: TypeAliasExpr, /) -> None: + return None + + def visit_type_alias(self, o: TypeAlias, /) -> None: + return None + + def visit_namedtuple_expr(self, o: NamedTupleExpr, /) -> None: + return None + + def visit_typeddict_expr(self, o: TypedDictExpr, /) -> None: + return None + + def visit_newtype_expr(self, o: NewTypeExpr, /) -> None: + return None + + def visit__promote_expr(self, o: PromoteExpr, /) -> None: + return None + + def visit_complex_expr(self, o: ComplexExpr, /) -> None: + return None + + def visit_enum_call_expr(self, o: EnumCallExpr, /) -> None: + return None + + def visit_singleton_pattern(self, o: SingletonPattern, /) -> None: + return None + class ExtendedTraverserVisitor(TraverserVisitor): """This is a more flexible traverser. diff --git a/mypy/visitor.py b/mypy/visitor.py index 6613b6cbb144..d1b2ca416410 100644 --- a/mypy/visitor.py +++ b/mypy/visitor.py @@ -356,273 +356,270 @@ class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T], Pattern """Empty base class for parse tree node visitors. The T type argument specifies the return type of the visit - methods. As all methods defined here return None by default, + methods. As all methods defined here raise by default, subclasses do not always need to override all the methods. - - TODO: make the default return value explicit, then turn on - empty body checking in mypy_self_check.ini. """ # Not in superclasses: def visit_mypy_file(self, o: mypy.nodes.MypyFile, /) -> T: - pass + raise NotImplementedError() # TODO: We have a visit_var method, but no visit_typeinfo or any # other non-Statement SymbolNode (accepting those will raise a # runtime error). Maybe this should be resolved in some direction. def visit_var(self, o: mypy.nodes.Var, /) -> T: - pass + raise NotImplementedError() # Module structure def visit_import(self, o: mypy.nodes.Import, /) -> T: - pass + raise NotImplementedError() def visit_import_from(self, o: mypy.nodes.ImportFrom, /) -> T: - pass + raise NotImplementedError() def visit_import_all(self, o: mypy.nodes.ImportAll, /) -> T: - pass + raise NotImplementedError() # Definitions def visit_func_def(self, o: mypy.nodes.FuncDef, /) -> T: - pass + raise NotImplementedError() def visit_overloaded_func_def(self, o: mypy.nodes.OverloadedFuncDef, /) -> T: - pass + raise NotImplementedError() def visit_class_def(self, o: mypy.nodes.ClassDef, /) -> T: - pass + raise NotImplementedError() def visit_global_decl(self, o: mypy.nodes.GlobalDecl, /) -> T: - pass + raise NotImplementedError() def visit_nonlocal_decl(self, o: mypy.nodes.NonlocalDecl, /) -> T: - pass + raise NotImplementedError() def visit_decorator(self, o: mypy.nodes.Decorator, /) -> T: - pass + raise NotImplementedError() def visit_type_alias(self, o: mypy.nodes.TypeAlias, /) -> T: - pass + raise NotImplementedError() def visit_placeholder_node(self, o: mypy.nodes.PlaceholderNode, /) -> T: - pass + raise NotImplementedError() # Statements def visit_block(self, o: mypy.nodes.Block, /) -> T: - pass + raise NotImplementedError() def visit_expression_stmt(self, o: mypy.nodes.ExpressionStmt, /) -> T: - pass + raise NotImplementedError() def visit_assignment_stmt(self, o: mypy.nodes.AssignmentStmt, /) -> T: - pass + raise NotImplementedError() def visit_operator_assignment_stmt(self, o: mypy.nodes.OperatorAssignmentStmt, /) -> T: - pass + raise NotImplementedError() def visit_while_stmt(self, o: mypy.nodes.WhileStmt, /) -> T: - pass + raise NotImplementedError() def visit_for_stmt(self, o: mypy.nodes.ForStmt, /) -> T: - pass + raise NotImplementedError() def visit_return_stmt(self, o: mypy.nodes.ReturnStmt, /) -> T: - pass + raise NotImplementedError() def visit_assert_stmt(self, o: mypy.nodes.AssertStmt, /) -> T: - pass + raise NotImplementedError() def visit_del_stmt(self, o: mypy.nodes.DelStmt, /) -> T: - pass + raise NotImplementedError() def visit_if_stmt(self, o: mypy.nodes.IfStmt, /) -> T: - pass + raise NotImplementedError() def visit_break_stmt(self, o: mypy.nodes.BreakStmt, /) -> T: - pass + raise NotImplementedError() def visit_continue_stmt(self, o: mypy.nodes.ContinueStmt, /) -> T: - pass + raise NotImplementedError() def visit_pass_stmt(self, o: mypy.nodes.PassStmt, /) -> T: - pass + raise NotImplementedError() def visit_raise_stmt(self, o: mypy.nodes.RaiseStmt, /) -> T: - pass + raise NotImplementedError() def visit_try_stmt(self, o: mypy.nodes.TryStmt, /) -> T: - pass + raise NotImplementedError() def visit_with_stmt(self, o: mypy.nodes.WithStmt, /) -> T: - pass + raise NotImplementedError() def visit_match_stmt(self, o: mypy.nodes.MatchStmt, /) -> T: - pass + raise NotImplementedError() def visit_type_alias_stmt(self, o: mypy.nodes.TypeAliasStmt, /) -> T: - pass + raise NotImplementedError() # Expressions (default no-op implementation) def visit_int_expr(self, o: mypy.nodes.IntExpr, /) -> T: - pass + raise NotImplementedError() def visit_str_expr(self, o: mypy.nodes.StrExpr, /) -> T: - pass + raise NotImplementedError() def visit_bytes_expr(self, o: mypy.nodes.BytesExpr, /) -> T: - pass + raise NotImplementedError() def visit_float_expr(self, o: mypy.nodes.FloatExpr, /) -> T: - pass + raise NotImplementedError() def visit_complex_expr(self, o: mypy.nodes.ComplexExpr, /) -> T: - pass + raise NotImplementedError() def visit_ellipsis(self, o: mypy.nodes.EllipsisExpr, /) -> T: - pass + raise NotImplementedError() def visit_star_expr(self, o: mypy.nodes.StarExpr, /) -> T: - pass + raise NotImplementedError() def visit_name_expr(self, o: mypy.nodes.NameExpr, /) -> T: - pass + raise NotImplementedError() def visit_member_expr(self, o: mypy.nodes.MemberExpr, /) -> T: - pass + raise NotImplementedError() def visit_yield_from_expr(self, o: mypy.nodes.YieldFromExpr, /) -> T: - pass + raise NotImplementedError() def visit_yield_expr(self, o: mypy.nodes.YieldExpr, /) -> T: - pass + raise NotImplementedError() def visit_call_expr(self, o: mypy.nodes.CallExpr, /) -> T: - pass + raise NotImplementedError() def visit_op_expr(self, o: mypy.nodes.OpExpr, /) -> T: - pass + raise NotImplementedError() def visit_comparison_expr(self, o: mypy.nodes.ComparisonExpr, /) -> T: - pass + raise NotImplementedError() def visit_cast_expr(self, o: mypy.nodes.CastExpr, /) -> T: - pass + raise NotImplementedError() def visit_assert_type_expr(self, o: mypy.nodes.AssertTypeExpr, /) -> T: - pass + raise NotImplementedError() def visit_reveal_expr(self, o: mypy.nodes.RevealExpr, /) -> T: - pass + raise NotImplementedError() def visit_super_expr(self, o: mypy.nodes.SuperExpr, /) -> T: - pass + raise NotImplementedError() def visit_assignment_expr(self, o: mypy.nodes.AssignmentExpr, /) -> T: - pass + raise NotImplementedError() def visit_unary_expr(self, o: mypy.nodes.UnaryExpr, /) -> T: - pass + raise NotImplementedError() def visit_list_expr(self, o: mypy.nodes.ListExpr, /) -> T: - pass + raise NotImplementedError() def visit_dict_expr(self, o: mypy.nodes.DictExpr, /) -> T: - pass + raise NotImplementedError() def visit_tuple_expr(self, o: mypy.nodes.TupleExpr, /) -> T: - pass + raise NotImplementedError() def visit_set_expr(self, o: mypy.nodes.SetExpr, /) -> T: - pass + raise NotImplementedError() def visit_index_expr(self, o: mypy.nodes.IndexExpr, /) -> T: - pass + raise NotImplementedError() def visit_type_application(self, o: mypy.nodes.TypeApplication, /) -> T: - pass + raise NotImplementedError() def visit_lambda_expr(self, o: mypy.nodes.LambdaExpr, /) -> T: - pass + raise NotImplementedError() def visit_list_comprehension(self, o: mypy.nodes.ListComprehension, /) -> T: - pass + raise NotImplementedError() def visit_set_comprehension(self, o: mypy.nodes.SetComprehension, /) -> T: - pass + raise NotImplementedError() def visit_dictionary_comprehension(self, o: mypy.nodes.DictionaryComprehension, /) -> T: - pass + raise NotImplementedError() def visit_generator_expr(self, o: mypy.nodes.GeneratorExpr, /) -> T: - pass + raise NotImplementedError() def visit_slice_expr(self, o: mypy.nodes.SliceExpr, /) -> T: - pass + raise NotImplementedError() def visit_conditional_expr(self, o: mypy.nodes.ConditionalExpr, /) -> T: - pass + raise NotImplementedError() def visit_type_var_expr(self, o: mypy.nodes.TypeVarExpr, /) -> T: - pass + raise NotImplementedError() def visit_paramspec_expr(self, o: mypy.nodes.ParamSpecExpr, /) -> T: - pass + raise NotImplementedError() def visit_type_var_tuple_expr(self, o: mypy.nodes.TypeVarTupleExpr, /) -> T: - pass + raise NotImplementedError() def visit_type_alias_expr(self, o: mypy.nodes.TypeAliasExpr, /) -> T: - pass + raise NotImplementedError() def visit_namedtuple_expr(self, o: mypy.nodes.NamedTupleExpr, /) -> T: - pass + raise NotImplementedError() def visit_enum_call_expr(self, o: mypy.nodes.EnumCallExpr, /) -> T: - pass + raise NotImplementedError() def visit_typeddict_expr(self, o: mypy.nodes.TypedDictExpr, /) -> T: - pass + raise NotImplementedError() def visit_newtype_expr(self, o: mypy.nodes.NewTypeExpr, /) -> T: - pass + raise NotImplementedError() def visit__promote_expr(self, o: mypy.nodes.PromoteExpr, /) -> T: - pass + raise NotImplementedError() def visit_await_expr(self, o: mypy.nodes.AwaitExpr, /) -> T: - pass + raise NotImplementedError() def visit_temp_node(self, o: mypy.nodes.TempNode, /) -> T: - pass + raise NotImplementedError() # Patterns def visit_as_pattern(self, o: mypy.patterns.AsPattern, /) -> T: - pass + raise NotImplementedError() def visit_or_pattern(self, o: mypy.patterns.OrPattern, /) -> T: - pass + raise NotImplementedError() def visit_value_pattern(self, o: mypy.patterns.ValuePattern, /) -> T: - pass + raise NotImplementedError() def visit_singleton_pattern(self, o: mypy.patterns.SingletonPattern, /) -> T: - pass + raise NotImplementedError() def visit_sequence_pattern(self, o: mypy.patterns.SequencePattern, /) -> T: - pass + raise NotImplementedError() def visit_starred_pattern(self, o: mypy.patterns.StarredPattern, /) -> T: - pass + raise NotImplementedError() def visit_mapping_pattern(self, o: mypy.patterns.MappingPattern, /) -> T: - pass + raise NotImplementedError() def visit_class_pattern(self, o: mypy.patterns.ClassPattern, /) -> T: - pass + raise NotImplementedError() diff --git a/mypy_self_check.ini b/mypy_self_check.ini index f54c1f17f025..7198a1f6f342 100644 --- a/mypy_self_check.ini +++ b/mypy_self_check.ini @@ -12,7 +12,3 @@ exclude = mypy/typeshed/|mypyc/test-data/|mypyc/lib-rt/ enable_error_code = ignore-without-code,redundant-expr enable_incomplete_feature = PreciseTupleTypes show_error_code_links = True - -[mypy-mypy.visitor] -# See docstring for NodeVisitor for motivation. -disable_error_code = empty-body