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

Add type hints to the convert methods #146

Merged
merged 2 commits into from
May 14, 2024
Merged
Changes from 1 commit
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
43 changes: 29 additions & 14 deletions plum/promotion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
"""Promotion and conversion functions."""

__all__ = [
"convert",
"add_conversion_method",
"conversion_method",
"add_promotion_rule",
"promote",
]

from typing import Callable, Protocol, Type, TypeVar

from beartype.door import TypeHint

import plum.function
Expand All @@ -7,13 +19,8 @@
from .repr import repr_short
from .type import resolve_type_hint

__all__ = [
"convert",
"add_conversion_method",
"conversion_method",
"add_promotion_rule",
"promote",
]
T = TypeVar("T")
R = TypeVar("R")

_dispatch = Dispatcher()

Expand All @@ -40,13 +47,19 @@ def convert(obj, type_to):

@_dispatch
def _convert(obj, type_to):
if _is_bearable(obj, resolve_type_hint(type_to)):
return obj
else:
if not _is_bearable(obj, resolve_type_hint(type_to)):
raise TypeError(f"Cannot convert `{obj}` to `{repr_short(type_to)}`.")
return obj


class _ConversionCallable(Protocol[T, R]):
def __call__(self, obj: T) -> R:
...


def add_conversion_method(type_from, type_to, f):
def add_conversion_method(
type_from: Type[T], type_to: Type[R], f: _ConversionCallable
) -> None:
"""Add a conversion method to convert an object from one type to another.

Args:
Expand All @@ -57,11 +70,13 @@ def add_conversion_method(type_from, type_to, f):
"""

@_convert.dispatch
def perform_conversion(obj: type_from, _: type_to):
def perform_conversion(obj, _):
Copy link
Member

Choose a reason for hiding this comment

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

The tests might be failing because the type annotations here are not there anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted.

return f(obj)


def conversion_method(type_from, type_to):
def conversion_method(
type_from: Type[T], type_to: Type[R]
) -> Callable[[_ConversionCallable[T, R]], _ConversionCallable[T, R]]:
"""Decorator to add a conversion method to convert an object from one
type to another.

Expand All @@ -70,7 +85,7 @@ def conversion_method(type_from, type_to):
type_to (type): Type to convert to.
"""

def add_method(f):
def add_method(f: _ConversionCallable[T, R]) -> _ConversionCallable[T, R]:
add_conversion_method(type_from, type_to, f)
return f

Expand Down
Loading