diff --git a/src/textual/box_model.py b/src/textual/box_model.py index 027a1ef8a..5259248a6 100644 --- a/src/textual/box_model.py +++ b/src/textual/box_model.py @@ -54,11 +54,11 @@ def get_box_model( else: width = max(0, width - styles.margin.width) - if styles.min_width: + if has_rule("min_width"): min_width = styles.min_width.resolve_dimension(container_size, parent_size) width = max(width, min_width) - if styles.max_width: + if has_rule("max_width"): max_width = styles.max_width.resolve_dimension(container_size, parent_size) width = min(width, max_width) @@ -70,11 +70,11 @@ def get_box_model( else: height = max(0, height - styles.margin.height) - if styles.min_height: + if has_rule("min_height"): min_height = styles.min_height.resolve_dimension(container_size, parent_size) height = max(height, min_height) - if styles.max_height: + if has_rule("max_height"): max_height = styles.max_height.resolve_dimension(container_size, parent_size) height = min(width, max_height) diff --git a/src/textual/css/styles.py b/src/textual/css/styles.py index b4f013860..286c0dd2c 100644 --- a/src/textual/css/styles.py +++ b/src/textual/css/styles.py @@ -132,6 +132,7 @@ class RulesMap(TypedDict, total=False): RULE_NAMES = list(RulesMap.__annotations__.keys()) +RULE_NAMES_SET = frozenset(RULE_NAMES) _rule_getter = attrgetter(*RULE_NAMES) @@ -416,6 +417,7 @@ class Styles(StylesBase): return Styles(node=self.node, _rules=self.get_rules(), important=self.important) def has_rule(self, rule: str) -> bool: + assert rule in RULE_NAMES_SET, f"no such rule {rule!r}" return rule in self._rules def clear_rule(self, rule: str) -> bool: diff --git a/tests/test_geometry.py b/tests/test_geometry.py index 46f1517c8..923116251 100644 --- a/tests/test_geometry.py +++ b/tests/test_geometry.py @@ -317,6 +317,13 @@ def test_spacing_add(): Spacing(1, 2, 3, 4) + "foo" +def test_spacing_sub(): + assert Spacing(1, 2, 3, 4) - Spacing(5, 6, 7, 8) == Spacing(-4, -4, -4, -4) + + with pytest.raises(TypeError): + Spacing(1, 2, 3, 4) - "foo" + + def test_split(): assert Region(10, 5, 22, 15).split(10, 5) == ( Region(10, 5, 10, 5),