Skip to content

Commit 39dc92d

Browse files
authored
Fix bugs in Color.from_cocoa and from_legacy_trigger (#598)
* Fix undefined MissingDependency, int(str) typo, and tuple annotation in color.py - Define MissingDependency as a subclass of ImportError - Fix int(str) to int(s) in from_legacy_trigger - Fix return type annotation from ('Color', 'Color') to typing.Tuple * Add tests for MissingDependency exception
1 parent f1febc8 commit 39dc92d

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

api/library/python/iterm2/iterm2/color.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
import typing
1212

1313

14+
class MissingDependency(ImportError):
15+
"""Raised when an optional dependency (e.g. pyobjc) is not installed."""
16+
pass
17+
18+
1419
class ColorSpace(enum.Enum):
1520
"""Describes the color space of a :class:`Color`."""
1621
SRGB = "sRGB" #: SRGB color space
@@ -87,8 +92,8 @@ def from_cocoa(b: str) -> typing.Optional['Color']:
8792
round(nscolor.alphaComponent() * 255))
8893

8994
@staticmethod
90-
def from_legacy_trigger(s: str) -> ('Color', 'Color'):
91-
i = int(str)
95+
def from_legacy_trigger(s: str) -> typing.Tuple[typing.Optional['Color'], typing.Optional['Color']]:
96+
i = int(s)
9297
if not gAppKitAvailable:
9398
raise MissingDependency("Colors cannot be parsed unless the pyobjc package is installed")
9499

api/library/python/iterm2/tests/test_color.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for iterm2.color module."""
22
import pytest
3-
from iterm2.color import Color, ColorSpace
3+
from iterm2.color import Color, ColorSpace, MissingDependency
44

55

66
class TestColor:
@@ -149,6 +149,24 @@ def test_hex_white(self):
149149
assert color.hex == "#ffffff"
150150

151151

152+
class TestMissingDependency:
153+
"""Tests for MissingDependency exception."""
154+
155+
def test_is_subclass_of_import_error(self):
156+
"""Test that MissingDependency is a subclass of ImportError."""
157+
assert issubclass(MissingDependency, ImportError)
158+
159+
def test_from_cocoa_without_pyobjc(self):
160+
"""Test that from_cocoa raises MissingDependency without pyobjc."""
161+
with pytest.raises(MissingDependency):
162+
Color.from_cocoa("dGVzdA==")
163+
164+
def test_from_legacy_trigger_without_pyobjc(self):
165+
"""Test that from_legacy_trigger raises MissingDependency without pyobjc."""
166+
with pytest.raises(MissingDependency):
167+
Color.from_legacy_trigger("0")
168+
169+
152170
class TestColorRepr:
153171
"""Tests for Color.__repr__() method."""
154172

0 commit comments

Comments
 (0)