Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions mypy/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test-data/unit/check-reports.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading