diff --git a/mypy/stats.py b/mypy/stats.py index e3499d2345635..9e9c43dab7c85 100644 --- a/mypy/stats.py +++ b/mypy/stats.py @@ -44,15 +44,18 @@ ) from mypy.traverser import TraverserVisitor from mypy.type_visitor import ANY_STRATEGY, BoolTypeQuery -from mypy.typeanal import collect_all_inner_types +from mypy.typeanal import CollectAllInnerTypesQuery from mypy.types import ( AnyType, CallableType, FunctionLike, Instance, + ParamSpecType, TupleType, Type, TypeOfAny, + TypeVarLikeType, + TypeVarTupleType, TypeVarType, get_proper_type, get_proper_types, @@ -381,7 +384,7 @@ def type(self, t: Type | None) -> None: self.num_precise_exprs += 1 self.record_line(self.line, TYPE_PRECISE) - for typ in get_proper_types(collect_all_inner_types(t)) + [t]: + for typ in get_proper_types(t.accept(StatisticsTypeCollector())) + [t]: if isinstance(typ, AnyType): typ = get_original_any(typ) if is_special_form_any(typ): @@ -453,6 +456,30 @@ def is_imprecise(t: Type) -> bool: return t.accept(HasAnyQuery()) +def type_var_types_for_statistics(t: TypeVarLikeType) -> list[Type]: + # A missing default is represented by Any(from_omitted_generics), but + # does not make a bound type variable imprecise. Keep actual defaults. + types = [t.upper_bound] + if t.has_default(): + types.append(t.default) + if isinstance(t, TypeVarType): + types.extend(t.values) + elif isinstance(t, ParamSpecType): + types.append(t.prefix) + return types + + +class StatisticsTypeCollector(CollectAllInnerTypesQuery): + def visit_type_var(self, t: TypeVarType) -> list[Type]: + return self.query_types(type_var_types_for_statistics(t)) + + def visit_param_spec(self, t: ParamSpecType) -> list[Type]: + return self.query_types(type_var_types_for_statistics(t)) + + def visit_type_var_tuple(self, t: TypeVarTupleType) -> list[Type]: + return self.query_types(type_var_types_for_statistics(t)) + + class HasAnyQuery(BoolTypeQuery): def __init__(self) -> None: super().__init__(ANY_STRATEGY) @@ -460,6 +487,15 @@ def __init__(self) -> None: def visit_any(self, t: AnyType) -> bool: return not is_special_form_any(t) + def visit_type_var(self, t: TypeVarType) -> bool: + return self.query_types(type_var_types_for_statistics(t)) + + def visit_param_spec(self, t: ParamSpecType) -> bool: + return self.query_types(type_var_types_for_statistics(t)) + + def visit_type_var_tuple(self, t: TypeVarTupleType) -> bool: + return self.query_types(type_var_types_for_statistics(t)) + def is_imprecise2(t: Type) -> bool: return t.accept(HasAnyQuery2()) diff --git a/test-data/unit/check-reports.test b/test-data/unit/check-reports.test index 423cbcc49289e..b1797fc52e015 100644 --- a/test-data/unit/check-reports.test +++ b/test-data/unit/check-reports.test @@ -274,6 +274,66 @@ Name Lines Precise Imprecise Any Empty Unanalyzed __main__ 5 3 0 1 1 0 m 3 2 0 1 0 0 +[case testLinePrecisionTypeVariablesWithoutDefaults] +# flags: --lineprecision-report out +from typing import Callable, ParamSpec, Tuple, TypeVar, TypeVarTuple, Unpack +T = TypeVar('T') +P = ParamSpec('P') +Ts = TypeVarTuple('Ts') +def identity(value: T) -> T: + return value +def preserve(function: Callable[P, T]) -> Callable[P, T]: + def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: + return function(*args, **kwargs) + return wrapper +def tuple_identity(*values: Unpack[Ts]) -> Tuple[Unpack[Ts]]: + return values +[builtins fixtures/tuple.pyi] +[outfile out/lineprecision.txt] +Name Lines Precise Imprecise Any Empty Unanalyzed +------------------------------------------------------------- +__main__ 13 11 0 0 2 0 + +[case testLinePrecisionTypeVariablesWithDefaults] +# flags: --lineprecision-report out +from typing import Any, Callable, ParamSpec, Tuple, TypeVar, TypeVarTuple, Unpack +T = TypeVar('T', default=int) +A = TypeVar('A', default=Any) +P = ParamSpec('P', default=[]) +Q = ParamSpec('Q', default=[Any]) +Ts = TypeVarTuple('Ts', default=Unpack[Tuple[int]]) +Us = TypeVarTuple('Us', default=Unpack[Tuple[Any]]) +def precise(value: T) -> T: + return value +def imprecise(value: A) -> A: + return value +def precise_callable(function: Callable[P, int]) -> None: pass +def imprecise_callable(function: Callable[Q, int]) -> None: pass +def precise_tuple(*values: Unpack[Ts]) -> None: pass +def imprecise_tuple(*values: Unpack[Us]) -> None: pass +[builtins fixtures/tuple.pyi] +[outfile out/lineprecision.txt] +Name Lines Precise Imprecise Any Empty Unanalyzed +------------------------------------------------------------- +__main__ 16 10 3 0 3 0 + +[case testLinePrecisionTypeVariableAnyBoundsAndPrefixes] +# flags: --lineprecision-report out +from typing import Any, Callable, ParamSpec, TypeVar +from typing_extensions import Concatenate +T = TypeVar('T', bound=Any) +P = ParamSpec('P') +def bounded(value: T) -> T: + return value +def prefixed(function: Callable[Concatenate[Any, P], int], *args: P.args, **kwargs: P.kwargs) -> int: + return function(0, *args, **kwargs) +def omitted(value: list) -> None: pass +[builtins fixtures/list.pyi] +[outfile out/lineprecision.txt] +Name Lines Precise Imprecise Any Empty Unanalyzed +------------------------------------------------------------- +__main__ 10 4 3 1 2 0 + [case testLinePrecisionCallImpreciseArg] # flags: --lineprecision-report out from m import f diff --git a/test-data/unit/reports.test b/test-data/unit/reports.test index 6e80683ad957b..bdcade80acdda 100644 --- a/test-data/unit/reports.test +++ b/test-data/unit/reports.test @@ -341,6 +341,32 @@ def g(m: Movie) -> Movie: Total 0 14 100.00% +[case testAnyExpressionsReportTypeVariablesWithoutDefaults] +# cmd: mypy --any-exprs-report report n.py + +[file n.py] +from typing import Callable, ParamSpec, TypeVar +from typing_extensions import TypeVarTuple, Unpack +T = TypeVar('T') +P = ParamSpec('P') +Ts = TypeVarTuple('Ts') +def identity(value: T) -> T: + return value +def preserve(function: Callable[P, T]) -> Callable[P, T]: + def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: + return function(*args, **kwargs) + return wrapper +def tuple_identity(*values: Unpack[Ts]) -> tuple[Unpack[Ts]]: + return values + +[file report/any-exprs.txt] +[outfile report/types-of-anys.txt] + Name Unannotated Explicit Unimported Omitted Generics Error Special Form Implementation Artifact +----------------------------------------------------------------------------------------------------------------- + n 0 0 0 0 0 0 0 +----------------------------------------------------------------------------------------------------------------- +Total 0 0 0 0 0 0 0 + [case testAnyExpressionsReportTypesOfAny] # cmd: mypy --any-exprs-report report n.py