Skip to content

Commit 46f0d91

Browse files
authored
A more accurate termcolor.colored fallback (#9435)
1 parent f794cdd commit 46f0d91

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

scripts/runtests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import subprocess
88
import sys
99
from pathlib import Path
10+
from typing import Iterable
1011

1112
try:
1213
from termcolor import colored
1314
except ImportError:
1415

15-
def colored(text: str, color: str = "") -> str: # type: ignore[misc]
16+
def colored(text: str, color: str | None = None, on_color: str | None = None, attrs: Iterable[str] | None = None) -> str:
1617
return text
1718

1819

tests/utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@
1010
from collections.abc import Mapping
1111
from functools import cache
1212
from pathlib import Path
13-
from typing import NamedTuple
13+
from typing import Iterable, NamedTuple
1414
from typing_extensions import Annotated
1515

1616
import pathspec # type: ignore[import]
1717
import tomli
1818
from packaging.requirements import Requirement
1919

20+
try:
21+
from termcolor import colored as colored
22+
except ImportError:
23+
24+
def colored(text: str, color: str | None = None, on_color: str | None = None, attrs: Iterable[str] | None = None) -> str:
25+
return text
26+
27+
2028
# Used to install system-wide packages for different OS types:
2129
METADATA_MAPPING = {"linux": "apt_dependencies", "darwin": "brew_dependencies", "win32": "choco_dependencies"}
2230

@@ -25,14 +33,6 @@ def strip_comments(text: str) -> str:
2533
return text.split("#")[0].strip()
2634

2735

28-
try:
29-
from termcolor import colored as colored
30-
except ImportError:
31-
32-
def colored(s: str, _: str) -> str: # type: ignore[misc]
33-
return s
34-
35-
3636
def print_error(error: str, end: str = "\n", fix_path: tuple[str, str] = ("", "")) -> None:
3737
error_split = error.split("\n")
3838
old, new = fix_path

0 commit comments

Comments
 (0)