diff --git a/mypy/stats.py b/mypy/stats.py index e3499d234563..a84099d5ae45 100644 --- a/mypy/stats.py +++ b/mypy/stats.py @@ -46,6 +46,7 @@ from mypy.type_visitor import ANY_STRATEGY, BoolTypeQuery from mypy.typeanal import collect_all_inner_types from mypy.types import ( + OVERLOAD_NAMES, AnyType, CallableType, FunctionLike, @@ -239,7 +240,10 @@ def visit_continue_stmt(self, o: ContinueStmt) -> None: self.record_precise_if_checked_scope(o) def visit_name_expr(self, o: NameExpr) -> None: - if o.fullname in ("builtins.None", "builtins.True", "builtins.False", "builtins.Ellipsis"): + if ( + o.fullname in ("builtins.None", "builtins.True", "builtins.False", "builtins.Ellipsis") + or o.fullname in OVERLOAD_NAMES + ): self.record_precise_if_checked_scope(o) else: self.process_node(o) @@ -346,7 +350,12 @@ def process_node(self, node: Expression) -> None: self.type(self.typemap.get(node)) def record_precise_if_checked_scope(self, node: Node) -> None: - if isinstance(node, Expression) and self.typemap and node not in self.typemap: + if ( + isinstance(node, Expression) + and self.typemap + and node not in self.typemap + and not (isinstance(node, NameExpr) and node.fullname in OVERLOAD_NAMES) + ): kind = TYPE_UNANALYZED elif self.is_checked_scope(): kind = TYPE_PRECISE diff --git a/test-data/unit/check-reports.test b/test-data/unit/check-reports.test index 423cbcc49289..44f6b2023a9b 100644 --- a/test-data/unit/check-reports.test +++ b/test-data/unit/check-reports.test @@ -343,3 +343,34 @@ Name Lines Precise Imprecise Any Empty Unanalyzed ------------------------------------------------------------- __main__ 5 3 0 1 1 0 m 4 3 0 1 0 0 + +[case testLinePrecisionOverloadDecoratorCheckedScope] +# flags: --lineprecision-report out +from typing import overload + +@overload +def f(x: int) -> int: ... +@overload +def f(x: str) -> str: ... +def f(x): + return x +[outfile out/lineprecision.txt] +Name Lines Precise Imprecise Any Empty Unanalyzed +------------------------------------------------------------- +__main__ 9 5 0 2 2 0 + +[case testLinePrecisionOverloadDecoratorUncheckedScope] +# flags: --lineprecision-report out +from typing import overload + +def outer(): + @overload + def f(x: int) -> int: ... + @overload + def f(x: str) -> str: ... + def f(x): + return x +[outfile out/lineprecision.txt] +Name Lines Precise Imprecise Any Empty Unanalyzed +------------------------------------------------------------- +__main__ 10 1 0 7 2 0