Skip to content

Modernize a few Counter/DefaultDict annotations #13463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2022
Merged
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
3 changes: 1 addition & 2 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
TYPE_CHECKING,
Any,
Callable,
DefaultDict,
Dict,
Iterator,
Optional,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions mypy/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
5 changes: 3 additions & 2 deletions mypy/server/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions mypy/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions mypy/test/testdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import os
from collections import defaultdict
from typing import DefaultDict

from mypy import build
from mypy.errors import CompileError
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mypyc/irbuild/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions mypyc/irbuild/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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]] = {}
Expand Down