Skip to content

Commit

Permalink
fix: remove colored dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Negin-Ulster committed Aug 28, 2023
1 parent 22d2197 commit b48ca88
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 93 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[mypy]
mypy_path=stubs
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
60 changes: 34 additions & 26 deletions src/syrupy/terminal.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
Empty file removed stubs/__init__.py
Empty file.
6 changes: 0 additions & 6 deletions stubs/colored.pyi

This file was deleted.

58 changes: 0 additions & 58 deletions tests/syrupy/test_terminal.py

This file was deleted.

1 comment on commit b48ca88

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: b48ca88 Previous: e2e314f Ratio
benchmarks/test_1000x.py::test_1000x_reads 0.7127848202946293 iter/sec (stddev: 0.06201153296017317) 0.5198448400308384 iter/sec (stddev: 0.05812831911626532) 0.73
benchmarks/test_1000x.py::test_1000x_writes 0.7085927045638013 iter/sec (stddev: 0.06360206478658968) 0.5139725385120247 iter/sec (stddev: 0.08167385620895319) 0.73
benchmarks/test_standard.py::test_standard 0.6762049090011545 iter/sec (stddev: 0.08938134097647676) 0.4866094944331513 iter/sec (stddev: 0.1352842241094931) 0.72

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.