From d6a44dc70a65180d6e1518451d7b191753af31c7 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 23 Sep 2022 11:00:07 +0300 Subject: [PATCH 1/3] Deprecate __ALL__, use __all__ instead --- src/termcolor/termcolor.py | 14 +++++++++++++- tests/test_termcolor.py | 10 ++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/termcolor/termcolor.py b/src/termcolor/termcolor.py index 82dae30..aa37d1a 100644 --- a/src/termcolor/termcolor.py +++ b/src/termcolor/termcolor.py @@ -25,9 +25,21 @@ from __future__ import annotations import os +import warnings from typing import Any, Iterable -__ALL__ = ["colored", "cprint"] + +def __getattr__(name: str) -> list[str]: + if name == "__ALL__": + warnings.warn( + "__ALL__ is deprecated and will be removed in termcolor 3. " + "Use __all__ instead.", + DeprecationWarning, + stacklevel=2, + ) + return ["colored", "cprint"] + raise AttributeError(f"module '{__name__}' has no attribute '{name}'") + ATTRIBUTES = dict( list( diff --git a/tests/test_termcolor.py b/tests/test_termcolor.py index 172c9de..e4b5e2a 100644 --- a/tests/test_termcolor.py +++ b/tests/test_termcolor.py @@ -5,7 +5,7 @@ import pytest -from termcolor import ATTRIBUTES, COLORS, HIGHLIGHTS, colored, cprint +from termcolor import ATTRIBUTES, COLORS, HIGHLIGHTS, colored, cprint, termcolor ALL_COLORS = [*COLORS, None] ALL_HIGHLIGHTS = [*HIGHLIGHTS, None] @@ -119,9 +119,15 @@ def test_attrs(capsys: pytest.CaptureFixture[str], attr: str, expected: str) -> "", ], ) -def test_env_var( +def test_environment_variables( monkeypatch: pytest.MonkeyPatch, test_env_var: str, test_value: str ) -> None: """Assert nothing applied when this env var set, regardless of value.""" monkeypatch.setenv(test_env_var, test_value) assert colored("text", color="red") == "text" + + +def test_all_deprecation() -> None: + """Assert that __ALL__ is deprecated (use __all__ instead)""" + with pytest.deprecated_call(): + assert termcolor.__ALL__ From 011868718f63e0c4a0c9b59590291c3a40372f80 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 23 Sep 2022 11:36:41 +0300 Subject: [PATCH 2/3] Use f-string --- src/termcolor/termcolor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/termcolor/termcolor.py b/src/termcolor/termcolor.py index aa37d1a..f92a777 100644 --- a/src/termcolor/termcolor.py +++ b/src/termcolor/termcolor.py @@ -156,7 +156,7 @@ def cprint( if __name__ == "__main__": - print("Current terminal type: %s" % os.getenv("TERM")) + print(f"Current terminal type: {os.getenv('TERM')}") print("Test basic colors:") cprint("Grey color", "grey") cprint("Red color", "red") From e8b320f7f16167efdd6cee1413f78ef61ea5404d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 23 Sep 2022 11:38:08 +0300 Subject: [PATCH 3/3] Add module docstring --- src/termcolor/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/termcolor/__init__.py b/src/termcolor/__init__.py index 9dceed7..86ace41 100644 --- a/src/termcolor/__init__.py +++ b/src/termcolor/__init__.py @@ -1,3 +1,4 @@ +"""ANSI color formatting for output in terminal.""" from __future__ import annotations from termcolor.termcolor import ATTRIBUTES, COLORS, HIGHLIGHTS, RESET, colored, cprint