From b48ca88e4228e816587e4d62bdf393f544e9ddb2 Mon Sep 17 00:00:00 2001 From: Noah Negin-Ulster Date: Mon, 28 Aug 2023 15:47:57 -0400 Subject: [PATCH] fix: remove colored dependency --- README.md | 2 +- mypy.ini | 1 - pyproject.toml | 1 - src/syrupy/terminal.py | 60 ++++++++++++++++++++--------------- stubs/__init__.py | 0 stubs/colored.pyi | 6 ---- tests/syrupy/test_terminal.py | 58 --------------------------------- 7 files changed, 35 insertions(+), 93 deletions(-) delete mode 100644 stubs/__init__.py delete mode 100644 stubs/colored.pyi delete mode 100644 tests/syrupy/test_terminal.py diff --git a/README.md b/README.md index 1f7afa91..42830af5 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ ## Overview -Syrupy is a [pytest](https://docs.pytest.org/en/latest/) snapshot plugin. It enables developers to write tests which assert immutability of computed results. +Syrupy is a zero-dependency [pytest](https://docs.pytest.org/en/latest/) snapshot plugin. It enables developers to write tests which assert immutability of computed results. ## Motivation diff --git a/mypy.ini b/mypy.ini index 35435210..f8b1844b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,2 +1 @@ [mypy] -mypy_path=stubs diff --git a/pyproject.toml b/pyproject.toml index 349f4608..868779a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,6 @@ syrupy = 'syrupy' [tool.poetry.dependencies] python = '>=3.8.1,<4' -colored = '>=1.3.92,<2.0.0' pytest = '>=7.0.0,<8.0.0' [tool.poetry.group.test.dependencies] diff --git a/src/syrupy/terminal.py b/src/syrupy/terminal.py index 4a982507..4ec42493 100644 --- a/src/syrupy/terminal.py +++ b/src/syrupy/terminal.py @@ -1,9 +1,5 @@ -from typing import ( - Any, - Union, -) - -import colored +from dataclasses import dataclass +from typing import Union from .constants import DISABLE_COLOR_ENV_VARS from .utils import get_env_value @@ -13,40 +9,52 @@ def _is_color_disabled() -> bool: return any(map(get_env_value, DISABLE_COLOR_ENV_VARS)) -def _attr(color: Any) -> str: +@dataclass +class TerminalCodes: + ESC: str = "\x1b[" + END: str = "m" + FOREGROUND_256: str = f"{ESC}38;5;" + BACKGROUND_256: str = f"{ESC}48;5;" + + STYLES = { + "bold": "1", + "dim": "2", + "italic": "3", + "underline": "4", + "reset": "0", + } + + COLORS = { + "black": "0", + "red": "1", + "green": "2", + "yellow": "3", + } + + +def _attr(style: str) -> str: if _is_color_disabled(): return "" - try: - return colored.attr(color) - except AttributeError: - # colored >=1.5.0, see: https://github.com/tophat/syrupy/issues/758 - return colored.style(color) # type: ignore + return f"{TerminalCodes.ESC}{TerminalCodes.STYLES[style]}{TerminalCodes.END}" -def _fg(color: Any) -> str: +def _fg(color: Union[int, str]) -> str: if _is_color_disabled(): return "" - try: - return colored.fg(color) - except AttributeError: - # colored >=1.5.0, see: https://github.com/tophat/syrupy/issues/758 - return colored.fore(color) # type: ignore + color_code = TerminalCodes.COLORS[color] if isinstance(color, (str,)) else color + return f"{TerminalCodes.FOREGROUND_256}{str(color_code)}{TerminalCodes.END}" -def _bg(color: Any) -> str: +def _bg(color: int) -> str: if _is_color_disabled(): return "" - try: - return colored.bg(color) - except AttributeError: - # colored >=1.5.0, see: https://github.com/tophat/syrupy/issues/758 - return colored.back(color) # type: ignore + return f"{TerminalCodes.BACKGROUND_256}{str(color)}{TerminalCodes.END}" -def _stylize(text: Union[str, int], *args: Any) -> str: +def _stylize(text: Union[str, int], formatting: str) -> str: if _is_color_disabled(): return str(text) - return colored.stylize(text, *args) + return f"{formatting}{text}{_attr('reset')}" def reset(text: Union[str, int]) -> str: diff --git a/stubs/__init__.py b/stubs/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/stubs/colored.pyi b/stubs/colored.pyi deleted file mode 100644 index 61e76b42..00000000 --- a/stubs/colored.pyi +++ /dev/null @@ -1,6 +0,0 @@ -from typing import Union - -def attr(color: Union[str, int]) -> str: ... -def bg(color: Union[str, int]) -> str: ... -def fg(color: Union[str, int]) -> str: ... -def stylize(text: Union[str, int], style: str, reset: bool = True) -> str: ... diff --git a/tests/syrupy/test_terminal.py b/tests/syrupy/test_terminal.py deleted file mode 100644 index cf9df9fb..00000000 --- a/tests/syrupy/test_terminal.py +++ /dev/null @@ -1,58 +0,0 @@ -from unittest.mock import ( - NonCallableMock, - patch, -) - -import pytest - -from syrupy.constants import DISABLE_COLOR_ENV_VAR -from syrupy.terminal import ( - bold, - context_style, - error_style, - green, - received_diff_style, - received_style, - red, - reset, - snapshot_diff_style, - snapshot_style, - success_style, - warning_style, - yellow, -) - - -def test_colors_off_does_not_call_colored(): - """ - Test that disabling colors prevents instantiating colored object. - Enables workarounds for when instantiating the colored object causes crashes, - see issue #633 - """ - - with patch( - "syrupy.terminal.colored.colored.__init__", new_callable=NonCallableMock - ): - with patch.dict("os.environ", {DISABLE_COLOR_ENV_VAR: "true"}): - for method in ( - reset, - red, - yellow, - green, - bold, - error_style, - warning_style, - success_style, - snapshot_style, - snapshot_diff_style, - received_style, - received_diff_style, - context_style, - ): - _ = method("foo") - - # Prevent test from accidentally passing by patching wrong object - with pytest.raises(TypeError) as excinfo: - _ = red("foo") - - assert "NonCallableMock" in str(excinfo.value)