From 3aebb87548a852b90b276a6a65148bdceab0e9c1 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 20 Aug 2022 23:33:20 +0100 Subject: [PATCH] Modernize a few `Counter`/`DefaultDict` annotations (#13463) --- mypy/nodes.py | 3 +-- mypy/report.py | 7 +++---- mypy/server/deps.py | 5 +++-- mypy/stats.py | 3 +-- mypy/test/testdeps.py | 3 +-- mypyc/irbuild/function.py | 4 ++-- mypyc/irbuild/prepare.py | 4 ++-- 7 files changed, 13 insertions(+), 16 deletions(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 765feb171b9b..2b32d5f4f25c 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -10,7 +10,6 @@ TYPE_CHECKING, Any, Callable, - DefaultDict, Dict, Iterator, Optional, @@ -306,7 +305,7 @@ class MypyFile(SymbolNode): # Top-level definitions and statements defs: list[Statement] # Type alias dependencies as mapping from target to set of alias full names - alias_deps: DefaultDict[str, set[str]] + alias_deps: defaultdict[str, set[str]] # Is there a UTF-8 BOM at the start? is_bom: bool names: SymbolTable diff --git a/mypy/report.py b/mypy/report.py index 183d0390e2c9..ea9669770fba 100644 --- a/mypy/report.py +++ b/mypy/report.py @@ -10,7 +10,6 @@ import sys import time import tokenize -import typing from abc import ABCMeta, abstractmethod from operator import attrgetter from typing import Any, Callable, Dict, Iterator, Tuple, cast @@ -211,7 +210,7 @@ class AnyExpressionsReporter(AbstractReporter): def __init__(self, reports: Reports, output_dir: str) -> None: super().__init__(reports, output_dir) self.counts: dict[str, tuple[int, int]] = {} - self.any_types_counter: dict[str, typing.Counter[int]] = {} + self.any_types_counter: dict[str, collections.Counter[int]] = {} def on_file( self, @@ -286,7 +285,7 @@ def _report_any_exprs(self) -> None: self._write_out_report("any-exprs.txt", column_names, rows, total_row) def _report_types_of_anys(self) -> None: - total_counter: typing.Counter[int] = collections.Counter() + total_counter: collections.Counter[int] = collections.Counter() for counter in self.any_types_counter.values(): for any_type, value in counter.items(): total_counter[any_type] += value @@ -528,7 +527,7 @@ def on_file( def _get_any_info_for_line(visitor: stats.StatisticsVisitor, lineno: int) -> str: if lineno in visitor.any_line_map: result = "Any Types on this line: " - counter: typing.Counter[int] = collections.Counter() + counter: collections.Counter[int] = collections.Counter() for typ in visitor.any_line_map[lineno]: counter[typ.type_of_any] += 1 for any_type, occurrences in counter.items(): diff --git a/mypy/server/deps.py b/mypy/server/deps.py index 7cb4aeda7534..121386c4c73d 100644 --- a/mypy/server/deps.py +++ b/mypy/server/deps.py @@ -81,7 +81,8 @@ class 'mod.Cls'. This can also refer to an attribute inherited from a from __future__ import annotations -from typing import DefaultDict, List +from collections import defaultdict +from typing import List from mypy.nodes import ( GDEF, @@ -220,7 +221,7 @@ def __init__( self, type_map: dict[Expression, Type], python_version: tuple[int, int], - alias_deps: DefaultDict[str, set[str]], + alias_deps: defaultdict[str, set[str]], options: Options | None = None, ) -> None: self.scope = Scope() diff --git a/mypy/stats.py b/mypy/stats.py index f68edd7b9c04..af6c5fc14a50 100644 --- a/mypy/stats.py +++ b/mypy/stats.py @@ -3,7 +3,6 @@ from __future__ import annotations import os -import typing from collections import Counter from contextlib import contextmanager from typing import Iterator, cast @@ -102,7 +101,7 @@ def __init__( self.line_map: dict[int, int] = {} - self.type_of_any_counter: typing.Counter[int] = Counter() + self.type_of_any_counter: Counter[int] = Counter() self.any_line_map: dict[int, list[AnyType]] = {} # For each scope (top level/function), whether the scope was type checked diff --git a/mypy/test/testdeps.py b/mypy/test/testdeps.py index 7cbe619bad09..3a2bfa4d9c63 100644 --- a/mypy/test/testdeps.py +++ b/mypy/test/testdeps.py @@ -4,7 +4,6 @@ import os from collections import defaultdict -from typing import DefaultDict from mypy import build from mypy.errors import CompileError @@ -40,7 +39,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None: if not a: a = ["Unknown compile error (likely syntax error in test case or fixture)"] else: - deps: DefaultDict[str, set[str]] = defaultdict(set) + deps: defaultdict[str, set[str]] = defaultdict(set) for module in files: if ( module in dumped_modules diff --git a/mypyc/irbuild/function.py b/mypyc/irbuild/function.py index eb35a983866d..ea8d86ff0468 100644 --- a/mypyc/irbuild/function.py +++ b/mypyc/irbuild/function.py @@ -13,7 +13,7 @@ from __future__ import annotations from collections import defaultdict -from typing import DefaultDict, NamedTuple, Sequence +from typing import NamedTuple, Sequence from mypy.nodes import ( ArgKind, @@ -933,7 +933,7 @@ def maybe_insert_into_registry_dict(builder: IRBuilder, fitem: FuncDef) -> None: line = fitem.line is_singledispatch_main_func = fitem in builder.singledispatch_impls # dict of singledispatch_func to list of register_types (fitem is the function to register) - to_register: DefaultDict[FuncDef, list[TypeInfo]] = defaultdict(list) + to_register: defaultdict[FuncDef, list[TypeInfo]] = defaultdict(list) for main_func, impls in builder.singledispatch_impls.items(): for dispatch_type, impl in impls: if fitem == impl: diff --git a/mypyc/irbuild/prepare.py b/mypyc/irbuild/prepare.py index e40dfa0d7c02..05ebac07b983 100644 --- a/mypyc/irbuild/prepare.py +++ b/mypyc/irbuild/prepare.py @@ -14,7 +14,7 @@ from __future__ import annotations from collections import defaultdict -from typing import DefaultDict, Iterable, NamedTuple, Tuple +from typing import Iterable, NamedTuple, Tuple from mypy.build import Graph from mypy.nodes import ( @@ -379,7 +379,7 @@ def __init__(self, errors: Errors) -> None: super().__init__() # Map of main singledispatch function to list of registered implementations - self.singledispatch_impls: DefaultDict[FuncDef, list[RegisterImplInfo]] = defaultdict(list) + self.singledispatch_impls: defaultdict[FuncDef, list[RegisterImplInfo]] = defaultdict(list) # Map of decorated function to the indices of any decorators to remove self.decorators_to_remove: dict[FuncDef, list[int]] = {}