Skip to content

Commit

Permalink
refactor: use dataclasses, no namedtuple
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Mar 7, 2024
1 parent 5a031b0 commit 6289be8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
10 changes: 6 additions & 4 deletions coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import token
import tokenize

from dataclasses import dataclass
from types import CodeType
from typing import (
cast, Any, Callable, Dict, Iterable, List, Optional, Protocol, Sequence,
Expand Down Expand Up @@ -462,7 +463,8 @@ def _find_statements(self) -> Iterable[TLineNo]:
# AST analysis
#

class ArcStart(collections.namedtuple("Arc", "lineno, cause")):
@dataclass(frozen=True, order=True)
class ArcStart:
"""The information needed to start an arc.
`lineno` is the line number the arc starts from.
Expand All @@ -474,8 +476,8 @@ class ArcStart(collections.namedtuple("Arc", "lineno, cause")):
to have `lineno` interpolated into it.
"""
def __new__(cls, lineno: TLineNo, cause: str | None = None) -> ArcStart:
return super().__new__(cls, lineno, cause)
lineno: TLineNo
cause: str = ""


class TAddArcFn(Protocol):
Expand Down Expand Up @@ -1256,7 +1258,7 @@ def _combine_finally_starts(self, starts: set[ArcStart], exits: set[ArcStart]) -
"""
causes = []
for start in sorted(starts):
if start.cause is not None:
if start.cause:
causes.append(start.cause.format(lineno=start.lineno))
cause = " or ".join(causes)
exits = {ArcStart(xit.lineno, cause) for xit in exits}
Expand Down
4 changes: 2 additions & 2 deletions coverage/sysmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from __future__ import annotations

import dataclasses
import functools
import inspect
import os
Expand All @@ -14,6 +13,7 @@
import threading
import traceback

from dataclasses import dataclass
from types import CodeType, FrameType
from typing import (
Any,
Expand Down Expand Up @@ -151,7 +151,7 @@ def _decorator(meth: AnyCallable) -> AnyCallable:
return _decorator


@dataclasses.dataclass
@dataclass
class CodeInfo:
"""The information we want about each code object."""

Expand Down
6 changes: 3 additions & 3 deletions lab/benchmark/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import collections
import contextlib
import dataclasses
import itertools
import os
import random
Expand All @@ -13,6 +12,7 @@
import time
from pathlib import Path

from dataclasses import dataclass
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple

import tabulate
Expand Down Expand Up @@ -480,7 +480,7 @@ def __init__(self, path, slug):
self.toxenv = None


@dataclasses.dataclass
@dataclass
class Coverage:
"""A version of coverage.py to use, maybe None."""

Expand Down Expand Up @@ -537,7 +537,7 @@ def __init__(self, directory, slug="source", tweaks=None, env_vars=None):
)


@dataclasses.dataclass
@dataclass
class Env:
"""An environment to run a test suite in."""

Expand Down

0 comments on commit 6289be8

Please sign in to comment.