Skip to content

Commit

Permalink
refactor: pyupgrade --py39-plus coverage/*.py tests/*.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Oct 3, 2024
1 parent 2be8f0d commit 84b7f3d
Show file tree
Hide file tree
Showing 51 changed files with 115 additions and 77 deletions.
3 changes: 2 additions & 1 deletion coverage/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import os
import re

from typing import Iterable, TYPE_CHECKING
from typing import TYPE_CHECKING
from collections.abc import Iterable

from coverage.files import flat_rootname
from coverage.misc import ensure_dir, isolate_module
Expand Down
2 changes: 1 addition & 1 deletion coverage/bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import annotations

from types import CodeType
from typing import Iterator
from collections.abc import Iterator


def code_objects(code: CodeType) -> Iterator[CodeType]:
Expand Down
11 changes: 6 additions & 5 deletions coverage/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

from types import FrameType
from typing import (
cast, Any, Callable, Dict, List, Mapping, Set, TypeVar,
cast, Any, Callable, Dict, List, Set, TypeVar,
)
from collections.abc import Mapping

from coverage import env
from coverage.config import CoverageConfig
Expand Down Expand Up @@ -420,7 +421,7 @@ def disable_plugin(self, disposition: TFileDisposition) -> None:
plugin._coverage_enabled = False
disposition.trace = False

@functools.lru_cache(maxsize=None) # pylint: disable=method-cache-max-size-none
@functools.cache # pylint: disable=method-cache-max-size-none
def cached_mapped_file(self, filename: str) -> str:
"""A locally cached version of file names mapped through file_mapper."""
return self.file_mapper(filename)
Expand Down Expand Up @@ -466,7 +467,7 @@ def flush_data(self) -> bool:
# tracer.c:CTracer_record_pair for the C code that creates
# these packed ints.
arc_data: dict[str, list[TArc]] = {}
packed_data = cast(Dict[str, Set[int]], self.data)
packed_data = cast(dict[str, set[int]], self.data)

# The list() here and in the inner loop are to get a clean copy
# even as tracers are continuing to add data.
Expand All @@ -482,10 +483,10 @@ def flush_data(self) -> bool:
tuples.append((l1, l2))
arc_data[fname] = tuples
else:
arc_data = cast(Dict[str, List[TArc]], self.data)
arc_data = cast(dict[str, list[TArc]], self.data)
self.covdata.add_arcs(self.mapped_file_dict(arc_data))
else:
line_data = cast(Dict[str, Set[int]], self.data)
line_data = cast(dict[str, set[int]], self.data)
self.covdata.add_lines(self.mapped_file_dict(line_data))

file_tracers = {
Expand Down
3 changes: 2 additions & 1 deletion coverage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import re

from typing import (
Any, Callable, Iterable, Union,
Any, Callable, Union,
)
from collections.abc import Iterable

from coverage.exceptions import ConfigError
from coverage.misc import isolate_module, human_sorted_items, substitute_variables
Expand Down
3 changes: 2 additions & 1 deletion coverage/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from __future__ import annotations

from types import FrameType
from typing import cast, Sequence
from typing import cast
from collections.abc import Sequence

from coverage.types import TShouldStartContextFn

Expand Down
5 changes: 3 additions & 2 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
from types import FrameType
from typing import (
cast,
Any, Callable, IO, Iterable, Iterator, List,
Any, Callable, IO, List,
)
from collections.abc import Iterable, Iterator

from coverage import env
from coverage.annotate import AnnotateReporter
Expand Down Expand Up @@ -784,7 +785,7 @@ def get_exclude_list(self, which: str = "exclude") -> list[str]:
"""
self._init()
return cast(List[str], getattr(self.config, which + "_list"))
return cast(list[str], getattr(self.config, which + "_list"))

def save(self) -> None:
"""Save the collected coverage data to the data file."""
Expand Down
5 changes: 3 additions & 2 deletions coverage/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import hashlib
import os.path

from typing import Callable, Iterable
from typing import Callable
from collections.abc import Iterable

from coverage.exceptions import CoverageException, NoDataError
from coverage.files import PathAliases
Expand Down Expand Up @@ -138,7 +139,7 @@ def combine_parallel_data(
if aliases is None:
map_path = None
else:
map_path = functools.lru_cache(maxsize=None)(aliases.map)
map_path = functools.cache(aliases.map)

file_hashes = set()
combined_any = False
Expand Down
3 changes: 2 additions & 1 deletion coverage/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

from typing import (
overload,
Any, Callable, IO, Iterable, Iterator, Mapping,
Any, Callable, IO,
)
from collections.abc import Iterable, Iterator, Mapping

from coverage.misc import human_sorted_items, isolate_module
from coverage.types import AnyCallable, TWritable
Expand Down
3 changes: 2 additions & 1 deletion coverage/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import platform
import sys

from typing import Any, Iterable
from typing import Any
from collections.abc import Iterable

# debug_info() at the bottom wants to show all the globals, but not imports.
# Grab the global names here to know which names to not show. Nothing defined
Expand Down
3 changes: 2 additions & 1 deletion coverage/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import re
import sys

from typing import Callable, Iterable
from typing import Callable
from collections.abc import Iterable

from coverage import env
from coverage.exceptions import ConfigError
Expand Down
5 changes: 3 additions & 2 deletions coverage/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import string

from dataclasses import dataclass, field
from typing import Any, Iterable, TYPE_CHECKING
from typing import Any, TYPE_CHECKING
from collections.abc import Iterable

import coverage
from coverage.data import CoverageData, add_data_to_hash
Expand Down Expand Up @@ -201,7 +202,7 @@ def __init__(self, fr: FileReporter, analysis: Analysis) -> None:

HTML_SAFE = string.ascii_letters + string.digits + "!#$%'()*+,-./:;=?@[]^_`{|}~"

@functools.lru_cache(maxsize=None)
@functools.cache
def encode_int(n: int) -> str:
"""Create a short HTML-safe string from an integer, using HTML_SAFE."""
if n == 0:
Expand Down
3 changes: 2 additions & 1 deletion coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

from types import FrameType, ModuleType
from typing import (
cast, Any, Iterable, TYPE_CHECKING,
cast, Any, TYPE_CHECKING,
)
from collections.abc import Iterable

from coverage import env
from coverage.disposition import FileDisposition, disposition_init
Expand Down
5 changes: 3 additions & 2 deletions coverage/jsonreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import json
import sys

from typing import Any, Dict, IO, Iterable, TYPE_CHECKING
from typing import Any, Dict, IO, TYPE_CHECKING
from collections.abc import Iterable

from coverage import __version__
from coverage.report_core import get_analysis_to_report
Expand All @@ -23,7 +24,7 @@


# A type for data that can be JSON-serialized.
JsonObj = Dict[str, Any]
JsonObj = dict[str, Any]

# "Version 1" had no format number at all.
# 2: add the meta.format field.
Expand Down
3 changes: 2 additions & 1 deletion coverage/lcovreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import hashlib
import sys

from typing import IO, Iterable, TYPE_CHECKING
from typing import IO, TYPE_CHECKING
from collections.abc import Iterable

from coverage.plugin import FileReporter
from coverage.report_core import get_analysis_to_report
Expand Down
5 changes: 3 additions & 2 deletions coverage/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

from types import ModuleType
from typing import (
Any, Iterable, Iterator, Mapping, NoReturn, Sequence, TypeVar,
Any, NoReturn, TypeVar,
)
from collections.abc import Iterable, Iterator, Mapping, Sequence

from coverage.exceptions import CoverageException
from coverage.types import TArc
Expand Down Expand Up @@ -301,7 +302,7 @@ def import_local_file(modname: str, modfile: str | None = None) -> ModuleType:
return mod


@functools.lru_cache(maxsize=None)
@functools.cache
def _human_key(s: str) -> tuple[list[str | int], str]:
"""Turn a string into a list of string and number chunks.
Expand Down
2 changes: 1 addition & 1 deletion coverage/numbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import sqlite3

from itertools import zip_longest
from typing import Iterable
from collections.abc import Iterable


def nums_to_numbits(nums: Iterable[int]) -> bytes:
Expand Down
9 changes: 5 additions & 4 deletions coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
from dataclasses import dataclass
from types import CodeType
from typing import (
cast, Any, Callable, Dict, Iterable, List, Optional, Protocol, Sequence,
cast, Any, Callable, Dict, List, Optional, Protocol,
Set, Tuple,
)
from collections.abc import Iterable, Sequence

from coverage import env
from coverage.bytecode import code_objects
Expand Down Expand Up @@ -324,7 +325,7 @@ def fix_with_jumps(self, arcs: Iterable[TArc]) -> set[TArc]:
to_remove.add(start_next)
return (set(arcs) | to_add) - to_remove

@functools.lru_cache()
@functools.lru_cache
def exit_counts(self) -> dict[TLineNo, int]:
"""Get a count of exits from that each line.
Expand Down Expand Up @@ -515,7 +516,7 @@ def __call__(
"""


TArcFragments = Dict[TArc, List[Tuple[Optional[str], Optional[str]]]]
TArcFragments = dict[TArc, list[tuple[Optional[str], Optional[str]]]]

class Block:
"""
Expand Down Expand Up @@ -851,7 +852,7 @@ def node_exits(self, node: ast.AST) -> set[ArcStart]:
"""
node_name = node.__class__.__name__
handler = cast(
Optional[Callable[[ast.AST], Set[ArcStart]]],
Optional[Callable[[ast.AST], set[ArcStart]]],
getattr(self, "_handle__" + node_name, None),
)
if handler is not None:
Expand Down
2 changes: 1 addition & 1 deletion coverage/phystokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import token
import tokenize

from typing import Iterable
from collections.abc import Iterable

from coverage import env
from coverage.types import TLineNo, TSourceTokenLines
Expand Down
3 changes: 2 additions & 1 deletion coverage/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def coverage_init(reg, options):
import functools

from types import FrameType
from typing import Any, Iterable
from typing import Any
from collections.abc import Iterable

from coverage import files
from coverage.misc import _needs_to_implement
Expand Down
3 changes: 2 additions & 1 deletion coverage/plugin_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import sys

from types import FrameType
from typing import Any, Iterable, Iterator
from typing import Any
from collections.abc import Iterable, Iterator

from coverage.exceptions import PluginError
from coverage.misc import isolate_module
Expand Down
3 changes: 2 additions & 1 deletion coverage/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import types
import zipimport

from typing import Iterable, TYPE_CHECKING
from typing import TYPE_CHECKING
from collections.abc import Iterable

from coverage import env
from coverage.exceptions import CoverageException, NoSource
Expand Down
6 changes: 3 additions & 3 deletions coverage/pytracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ def _trace(
flineno: TLineNo = frame.f_lineno

if self.trace_arcs:
cast(Set[TArc], self.cur_file_data).add((self.last_line, flineno))
cast(set[TArc], self.cur_file_data).add((self.last_line, flineno))
else:
cast(Set[TLineNo], self.cur_file_data).add(flineno)
cast(set[TLineNo], self.cur_file_data).add(flineno)
self.last_line = flineno

elif event == "return":
Expand Down Expand Up @@ -286,7 +286,7 @@ def _trace(
real_return = True
if real_return:
first = frame.f_code.co_firstlineno
cast(Set[TArc], self.cur_file_data).add((self.last_line, -first))
cast(set[TArc], self.cur_file_data).add((self.last_line, -first))

# Leaving this function, pop the filename stack.
self.cur_file_data, self.cur_file_name, self.last_line, self.started_context = (
Expand Down
3 changes: 2 additions & 1 deletion coverage/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import sys

from typing import Any, IO, Iterable, TYPE_CHECKING
from typing import Any, IO, TYPE_CHECKING
from collections.abc import Iterable

from coverage.exceptions import ConfigError, NoDataError
from coverage.misc import human_sorted_items
Expand Down
3 changes: 2 additions & 1 deletion coverage/report_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import sys

from typing import (
Callable, Iterable, Iterator, IO, Protocol, TYPE_CHECKING,
Callable, IO, Protocol, TYPE_CHECKING,
)
from collections.abc import Iterable, Iterator

from coverage.exceptions import NoDataError, NotPython
from coverage.files import prep_patterns, GlobMatcher
Expand Down
3 changes: 2 additions & 1 deletion coverage/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import dataclasses

from collections.abc import Container
from typing import Iterable, TYPE_CHECKING
from typing import TYPE_CHECKING
from collections.abc import Iterable

from coverage.exceptions import ConfigError
from coverage.misc import nice_pair
Expand Down
4 changes: 2 additions & 2 deletions coverage/sqldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import zlib

from typing import (
cast, Any, Callable, Collection, Mapping,
Sequence,
cast, Any, Callable,
)
from collections.abc import Collection, Mapping, Sequence

from coverage.debug import NoDebugging, auto_repr
from coverage.exceptions import CoverageException, DataError
Expand Down
Loading

0 comments on commit 84b7f3d

Please sign in to comment.