1515import re
1616import sys
1717import time
18+ from collections .abc import Generator , Sequence
1819from configparser import ConfigParser # type: ignore
19- from typing import Any , Dict , Generator , List , Optional , Sequence , TextIO , Tuple , Union
20+ from typing import Any , TextIO
2021
2122import pytest
2223from _pytest .config import Config
3233LEN_RIGHT_MARGIN = 0
3334LEN_PROGRESS_PERCENTAGE = 5
3435LEN_PROGRESS_BAR_SETTING = "10"
35- LEN_PROGRESS_BAR : Optional [ int ] = None
36+ LEN_PROGRESS_BAR : int | None = None
3637
3738
3839@dataclasses .dataclass
3940class Theme :
40- header : Optional [ str ] = "magenta"
41- skipped : Optional [ str ] = "blue"
42- success : Optional [ str ] = "green"
43- warning : Optional [ str ] = "yellow"
44- fail : Optional [ str ] = "red"
45- error : Optional [ str ] = "red"
46- xfailed : Optional [ str ] = "green"
47- xpassed : Optional [ str ] = "red"
48- progressbar : Optional [ str ] = "green"
49- progressbar_fail : Optional [ str ] = "red"
50- progressbar_background : Optional [ str ] = "grey"
51- path : Optional [ str ] = "cyan"
41+ header : str | None = "magenta"
42+ skipped : str | None = "blue"
43+ success : str | None = "green"
44+ warning : str | None = "yellow"
45+ fail : str | None = "red"
46+ error : str | None = "red"
47+ xfailed : str | None = "green"
48+ xpassed : str | None = "red"
49+ progressbar : str | None = "green"
50+ progressbar_fail : str | None = "red"
51+ progressbar_background : str | None = "grey"
52+ path : str | None = "cyan"
5253 name = None
5354 symbol_passed : str = "✓"
5455 symbol_skipped : str = "s"
@@ -57,16 +58,16 @@ class Theme:
5758 symbol_xfailed_skipped : str = "x"
5859 symbol_xfailed_failed : str = "X"
5960 symbol_unknown : str = "?"
60- unknown : Optional [ str ] = "blue"
61- symbol_rerun : Optional [ str ] = "R"
62- rerun : Optional [ str ] = "blue"
61+ unknown : str | None = "blue"
62+ symbol_rerun : str | None = "R"
63+ rerun : str | None = "blue"
6364
6465 def __getitem__ (self , x ):
6566 return getattr (self , x )
6667
6768
6869THEME : Theme = Theme ()
69- PROGRESS_BAR_BLOCKS : List [str ] = [
70+ PROGRESS_BAR_BLOCKS : list [str ] = [
7071 " " ,
7172 "▏" ,
7273 "▎" ,
@@ -155,16 +156,16 @@ def pytest_sessionstart(session: Session) -> None:
155156 config = ConfigParser ()
156157 config .read (["pytest-sugar.conf" , os .path .expanduser ("~/.pytest-sugar.conf" )])
157158
158- theme_attributes : Dict [str , Optional [ str ] ] = {}
159- fields : Tuple [dataclasses .Field , ...] = dataclasses .fields (Theme )
159+ theme_attributes : dict [str , str | None ] = {}
160+ fields : tuple [dataclasses .Field , ...] = dataclasses .fields (Theme )
160161
161162 for field in fields :
162163 key = field .name
163164 if not config .has_option ("theme" , key ):
164165 continue
165166
166167 value_str : str = config .get ("theme" , key ).lower ()
167- value : Optional [ str ] = value_str
168+ value : str | None = value_str
168169 if value in ("" , "none" ):
169170 value = None
170171
@@ -212,7 +213,7 @@ def pytest_configure(config) -> None:
212213 config .pluginmanager .register (sugar_reporter , "terminalreporter" )
213214
214215
215- def pytest_report_teststatus (report : BaseReport ) -> Optional [ Tuple [ str , str , str ]] :
216+ def pytest_report_teststatus (report : BaseReport ) -> tuple [ str , str , str ] | None :
216217 if not IS_SUGAR_ENABLED :
217218 return None
218219
@@ -247,7 +248,7 @@ def pytest_report_teststatus(report: BaseReport) -> Optional[Tuple[str, str, str
247248
248249
249250class SugarTerminalReporter (TerminalReporter ):
250- def __init__ (self , config : Config , file : Union [ TextIO , None ] = None ) -> None :
251+ def __init__ (self , config : Config , file : TextIO | None = None ) -> None :
251252 TerminalReporter .__init__ (self , config , file )
252253 self .paths_left = []
253254 self .tests_count = 0
@@ -287,12 +288,8 @@ def pytest_sessionstart(self, session: Session) -> None:
287288 ),
288289 bold = True ,
289290 )
290- if int (pytest .__version__ .split ("." )[0 ]) <= 6 :
291- hook_call_kwargs = {"startdir" : self .startpath }
292- else :
293- hook_call_kwargs = {"start_path" : self .startpath }
294291 lines = self .config .hook .pytest_report_header (
295- config = self .config , ** hook_call_kwargs
292+ config = self .config , start_path = self . startpath
296293 )
297294 lines .reverse ()
298295 for line in flatten (lines ):
@@ -301,7 +298,7 @@ def pytest_sessionstart(self, session: Session) -> None:
301298 def write_fspath_result (self , nodeid : str , res , ** markup : bool ) -> None :
302299 return
303300
304- def insert_progress (self , report : Union [ CollectReport , TestReport ] ) -> None :
301+ def insert_progress (self , report : CollectReport | TestReport ) -> None :
305302 def get_progress_bar () -> str :
306303 length = LEN_PROGRESS_BAR
307304 if not length :
@@ -397,7 +394,7 @@ def get_max_column_for_test_status(self) -> int:
397394 )
398395
399396 def begin_new_line (
400- self , report : Union [ CollectReport , TestReport ] , print_filename : bool
397+ self , report : CollectReport | TestReport , print_filename : bool
401398 ) -> None :
402399 path = self .report_key (report )
403400 self .current_line_num += 1
@@ -435,7 +432,7 @@ def begin_new_line(
435432 self .write ("\r \n " )
436433
437434 def reached_last_column_for_test_status (
438- self , report : Union [ CollectReport , TestReport ]
435+ self , report : CollectReport | TestReport
439436 ) -> bool :
440437 len_line = real_string_length (self .current_lines [self .report_key (report )])
441438 return len_line >= self .get_max_column_for_test_status ()
@@ -450,14 +447,14 @@ def pytest_runtest_logfinish(self, nodeid: str) -> None:
450447 # pytest's default progress
451448 pass
452449
453- def report_key (self , report : Union [ CollectReport , TestReport ] ) -> Any :
450+ def report_key (self , report : CollectReport | TestReport ) -> Any :
454451 """Returns a key to identify which line the report should write to."""
455452 return (
456453 (report .location or "" ) if self .showlongtestinfo else (report .fspath or "" )
457454 )
458455
459456 def pytest_runtest_logreport (self , report : TestReport ) -> None :
460- global LEN_PROGRESS_BAR_SETTING , LEN_PROGRESS_BAR
457+ global LEN_PROGRESS_BAR
461458
462459 res = pytest_report_teststatus (report = report )
463460 assert res
@@ -615,7 +612,7 @@ def summary_stats(self) -> None:
615612 colored (" % 5d deselected" % self .count ("deselected" ), THEME .warning )
616613 )
617614
618- def _find_playwright_trace (self , report : TestReport ) -> Optional [ str ] :
615+ def _find_playwright_trace (self , report : TestReport ) -> str | None :
619616 """
620617 Finds the Playwright trace file associated with a specific test report.
621618
@@ -727,7 +724,7 @@ def summary_errors(self) -> None:
727724 # show the error instantly after error has occurred.
728725 pass
729726
730- def print_failure (self , report : Union [ CollectReport , TestReport ] ) -> None :
727+ def print_failure (self , report : CollectReport | TestReport ) -> None :
731728 # https://github.com/Frozenball/pytest-sugar/issues/34
732729 if hasattr (report , "wasxfail" ):
733730 return
0 commit comments