11
11
12
12
from types import FrameType
13
13
from typing import (
14
- cast , Any , Callable , Dict , List , Mapping , Optional , Set , Type , TypeVar ,
14
+ cast , Any , Callable , Dict , List , Mapping , Set , TypeVar ,
15
15
)
16
16
17
17
from coverage import env
@@ -70,7 +70,7 @@ class Collector:
70
70
# The stack of active Collectors. Collectors are added here when started,
71
71
# and popped when stopped. Collectors on the stack are paused when not
72
72
# the top, and resumed when they become the top again.
73
- _collectors : List [Collector ] = []
73
+ _collectors : list [Collector ] = []
74
74
75
75
# The concurrency settings we support here.
76
76
LIGHT_THREADS = {"greenlet" , "eventlet" , "gevent" }
@@ -79,12 +79,12 @@ def __init__(
79
79
self ,
80
80
should_trace : Callable [[str , FrameType ], TFileDisposition ],
81
81
check_include : Callable [[str , FrameType ], bool ],
82
- should_start_context : Optional [ Callable [[FrameType ], Optional [ str ]]] ,
82
+ should_start_context : Callable [[FrameType ], str | None ] | None ,
83
83
file_mapper : Callable [[str ], str ],
84
84
timid : bool ,
85
85
branch : bool ,
86
86
warn : TWarnFn ,
87
- concurrency : List [str ],
87
+ concurrency : list [str ],
88
88
metacov : bool ,
89
89
) -> None :
90
90
"""Create a collector.
@@ -136,16 +136,16 @@ def __init__(
136
136
137
137
self .covdata : CoverageData
138
138
self .threading = None
139
- self .static_context : Optional [ str ] = None
139
+ self .static_context : str | None = None
140
140
141
141
self .origin = short_stack ()
142
142
143
143
self .concur_id_func = None
144
144
145
- self ._trace_class : Type [TracerCore ]
146
- self .file_disposition_class : Type [TFileDisposition ]
145
+ self ._trace_class : type [TracerCore ]
146
+ self .file_disposition_class : type [TFileDisposition ]
147
147
148
- core : Optional [ str ]
148
+ core : str | None
149
149
if timid :
150
150
core = "pytrace"
151
151
else :
@@ -240,7 +240,7 @@ def __init__(
240
240
def __repr__ (self ) -> str :
241
241
return f"<Collector at { id (self ):#x} : { self .tracer_name ()} >"
242
242
243
- def use_data (self , covdata : CoverageData , context : Optional [ str ] ) -> None :
243
+ def use_data (self , covdata : CoverageData , context : str | None ) -> None :
244
244
"""Use `covdata` for recording data."""
245
245
self .covdata = covdata
246
246
self .static_context = context
@@ -268,9 +268,9 @@ def reset(self) -> None:
268
268
269
269
# A dictionary mapping file names to file tracer plugin names that will
270
270
# handle them.
271
- self .file_tracers : Dict [str , str ] = {}
271
+ self .file_tracers : dict [str , str ] = {}
272
272
273
- self .disabled_plugins : Set [str ] = set ()
273
+ self .disabled_plugins : set [str ] = set ()
274
274
275
275
# The .should_trace_cache attribute is a cache from file names to
276
276
# coverage.FileDisposition objects, or None. When a file is first
@@ -301,7 +301,7 @@ def reset(self) -> None:
301
301
self .should_trace_cache = {}
302
302
303
303
# Our active Tracers.
304
- self .tracers : List [TracerCore ] = []
304
+ self .tracers : list [TracerCore ] = []
305
305
306
306
self ._clear_data ()
307
307
@@ -342,12 +342,12 @@ def _start_tracer(self) -> TTraceFn | None:
342
342
#
343
343
# New in 3.12: threading.settrace_all_threads: https://github.com/python/cpython/pull/96681
344
344
345
- def _installation_trace (self , frame : FrameType , event : str , arg : Any ) -> Optional [ TTraceFn ] :
345
+ def _installation_trace (self , frame : FrameType , event : str , arg : Any ) -> TTraceFn | None :
346
346
"""Called on new threads, installs the real tracer."""
347
347
# Remove ourselves as the trace function.
348
348
sys .settrace (None )
349
349
# Install the real tracer.
350
- fn : Optional [ TTraceFn ] = self ._start_tracer ()
350
+ fn : TTraceFn | None = self ._start_tracer ()
351
351
# Invoke the real trace function with the current event, to be sure
352
352
# not to lose an event.
353
353
if fn :
@@ -444,9 +444,9 @@ def _activity(self) -> bool:
444
444
"""
445
445
return any (tracer .activity () for tracer in self .tracers )
446
446
447
- def switch_context (self , new_context : Optional [ str ] ) -> None :
447
+ def switch_context (self , new_context : str | None ) -> None :
448
448
"""Switch to a new dynamic context."""
449
- context : Optional [ str ]
449
+ context : str | None
450
450
self .flush_data ()
451
451
if self .static_context :
452
452
context = self .static_context
@@ -471,7 +471,7 @@ def cached_mapped_file(self, filename: str) -> str:
471
471
"""A locally cached version of file names mapped through file_mapper."""
472
472
return self .file_mapper (filename )
473
473
474
- def mapped_file_dict (self , d : Mapping [str , T ]) -> Dict [str , T ]:
474
+ def mapped_file_dict (self , d : Mapping [str , T ]) -> dict [str , T ]:
475
475
"""Return a dict like d, but with keys modified by file_mapper."""
476
476
# The call to list(items()) ensures that the GIL protects the dictionary
477
477
# iterator against concurrent modifications by tracers running
@@ -511,7 +511,7 @@ def flush_data(self) -> bool:
511
511
# Unpack the line number pairs packed into integers. See
512
512
# tracer.c:CTracer_record_pair for the C code that creates
513
513
# these packed ints.
514
- arc_data : Dict [str , List [TArc ]] = {}
514
+ arc_data : dict [str , list [TArc ]] = {}
515
515
packed_data = cast (Dict [str , Set [int ]], self .data )
516
516
517
517
# The list() here and in the inner loop are to get a clean copy
0 commit comments