Skip to content

Commit

Permalink
Improve type annotations (#167)
Browse files Browse the repository at this point in the history
* Change ratio calculation to use Fraction

Replace the floating-point division in the ratio calculation with
Fraction

* Refine type annotations
  • Loading branch information
truongvan committed May 4, 2024
1 parent 2489e02 commit a7585cb
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pictures/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _grid(*, _columns=12, **breakpoint_sizes):
yield key, prev_size / _columns


def _media_query(*, container_width: int = None, **breakpoints: {str: int}):
def _media_query(*, container_width: int | None = None, **breakpoints: int):
settings = conf.get_settings()
prev_ratio = None
prev_width = 0
Expand All @@ -53,7 +53,7 @@ def _media_query(*, container_width: int = None, **breakpoints: {str: int}):
yield f"{container_width}px" if container_width else "100vw"


def sizes(*, cols=12, container_width: int = None, **breakpoints: {str: int}) -> str:
def sizes(*, cols=12, container_width: int | None = None, **breakpoints: int) -> str:
breakpoints = dict(_grid(_columns=cols, **breakpoints))
return ", ".join(_media_query(container_width=container_width, **breakpoints))

Expand All @@ -63,7 +63,7 @@ def source_set(
) -> set:
ratio = Fraction(ratio) if ratio else None
img_width, img_height = size
ratio = ratio or (img_width / img_height)
ratio = ratio or Fraction(img_width, img_height)
settings = conf.get_settings()
# calc all widths at 1X resolution
widths = (max_width * (w + 1) / cols for w in range(cols))
Expand Down

0 comments on commit a7585cb

Please sign in to comment.