Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for resolve_fraction_unit zero division error (#2502) #2519

Merged
merged 8 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Fixed `ZeroDivisionError` in `resolve_fraction_unit` https://github.com/Textualize/textual/issues/2502

## [0.24.1] - 2023-05-08

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/textual/_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def resolve_scalar(
resolved: list[Fraction | None] = [None] * len(resolve)
remaining_fraction = Fraction(sum(scalar.value for scalar, _, _ in resolve))

while True:
while remaining_fraction > 0:
remaining_space_changed = False
resolve_fraction = Fraction(remaining_space, remaining_fraction)
for index, (scalar, min_value, max_value) in enumerate(resolve):
Expand All @@ -166,7 +166,7 @@ def resolve_scalar(

return (
Fraction(remaining_space, remaining_fraction)
if remaining_space
if remaining_space > 0
Copy link
Member

Choose a reason for hiding this comment

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

Should this be remaining_fraction > 0?

else Fraction(1)
)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_resolve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fractions import Fraction
from itertools import chain

import pytest

Expand Down Expand Up @@ -122,3 +123,22 @@ def test_resolve_fraction_unit():
Fraction(32),
resolve_dimension="width",
) == Fraction(2)


def test_resolve_issue_2502():
"""Test https://github.com/Textualize/textual/issues/2502"""

widget = Widget()
widget.styles.width = "1fr"
widget.styles.min_width = 11

assert isinstance(
resolve_fraction_unit(
(widget.styles,),
Size(80, 24),
Size(80, 24),
Fraction(10),
resolve_dimension="width",
),
Fraction,
)