Skip to content

Commit

Permalink
feat(space): set precedence on dispatches (#139)
Browse files Browse the repository at this point in the history
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman authored Jul 17, 2024
1 parent 30173aa commit 68f3f8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 0 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Doctest configuration."""

from __future__ import annotations

from doctest import ELLIPSIS, NORMALIZE_WHITESPACE

from sybil import Sybil
Expand Down
32 changes: 20 additions & 12 deletions src/coordinax/_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ def to_units(
raise NotImplementedError


# ===============================================================
# Related dispatches


@dispatch # type: ignore[misc]
def represent_as(space: Space, target: type[AbstractVector], /) -> Space:
"""Represent the current vector to the target vector."""
Expand All @@ -459,6 +463,22 @@ def represent_as(space: Space, target: type[AbstractVector], /) -> Space:
)


# NOTE: need to set the precedence because `Space` is both a `Mapping` and a
# `dataclass`, which are both in the `replace` dispatch table.
@dispatch(precedence=1) # type: ignore[misc]
def replace(obj: Space, /, **kwargs: AbstractVector) -> Space:
"""Replace the components of the vector."""
return type(obj)(**{**obj, **kwargs})


# NOTE: need to set the precedence because `Space` is both a `Mapping` and a
# `dataclass`, which are both in the `field_items` dispatch table.
@dispatch(precedence=1) # type: ignore[misc]
def field_items(obj: Space, /) -> ItemsView[str, AbstractVector]:
"""Return the items from a Space."""
return obj.items()


# =============================================================== Temporary
# These functions are very similar to `represent_as`, but I don't think this is
# the best API. Until we figure out a better way to do this, we'll keep these
Expand Down Expand Up @@ -495,15 +515,3 @@ def temp_represent_as(
space["speed"],
space["length"],
)


@dispatch # type: ignore[misc]
def replace(obj: Space, /, **kwargs: AbstractVector) -> Space:
"""Replace the components of the vector."""
return type(obj)(**{**obj, **kwargs})


@dispatch # type: ignore[misc]
def field_items(obj: Space, /) -> ItemsView[str, AbstractVector]:
"""Return the items from a Space."""
return obj.items()

0 comments on commit 68f3f8b

Please sign in to comment.