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

feat: type annotate signature.py #172

Merged
merged 3 commits into from
Jun 28, 2024
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: 4 additions & 2 deletions plum/repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import types
import typing
from functools import partial
from typing import Any, Callable, Dict, Iterable, Optional
from typing import Any, Callable, Dict, Iterable, Optional, Type, TypeVar

import rich
from rich.color import Color
Expand All @@ -19,6 +19,8 @@
"rich_repr",
]

T = TypeVar("T")

path_style = Style(color=Color.from_ansi(7))
file_style = Style(bold=True, underline=True)

Expand Down Expand Up @@ -202,7 +204,7 @@ def _repr_mimebundle_from_rich_(
return data


def rich_repr(cls: Optional[type] = None, str: bool = False):
def rich_repr(cls: Optional[Type[T]] = None, str: bool = False) -> Type[T]:
"""Class decorator defining a `__repr__` method that calls :mod:`rich.`

This also sets `_repr_mimebundle_` for better rendering in Jupyter.
Expand Down
32 changes: 15 additions & 17 deletions plum/signature.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import inspect
import operator
import typing
from copy import copy
from typing import Callable, List, Set, Tuple, Union
from typing import Any, Callable, ClassVar, List, Set, Tuple, Union

from rich.segment import Segment
from typing_extensions import Self

import beartype.door
from beartype.peps import resolve_pep563 as beartype_resolve_pep563
Expand Down Expand Up @@ -41,17 +41,17 @@ class Signature(Comparable):
is_faithful (bool): Whether this signature only uses faithful types.
"""

_default_varargs = Missing
_default_precedence = 0
_default_varargs: ClassVar = Missing
_default_precedence: ClassVar[int] = 0

__slots__ = ("types", "varargs", "precedence", "is_faithful")
__slots__: Tuple[str, ...] = ("types", "varargs", "precedence", "is_faithful")

def __init__(
self,
*types: Tuple[TypeHint, ...],
varargs: OptionalType = _default_varargs,
precedence: int = _default_precedence,
):
) -> None:
"""Instantiate a signature, which contains exactly the information necessary for
dispatch.

Expand Down Expand Up @@ -90,17 +90,15 @@ def from_callable(f: Callable, precedence: int = 0) -> "Signature":
def has_varargs(self) -> bool:
return self.varargs is not Missing

def __copy__(self):
def __copy__(self) -> Self:
cls = type(self)
copy = cls.__new__(cls)
for attr in self.__slots__:
setattr(copy, attr, getattr(self, attr))

copy.types = self.types
copy.varargs = self.varargs
copy.precedence = self.precedence
copy.is_faithful = self.is_faithful
return copy

def __rich_console__(self, console, options):
def __rich_console__(self, console, options) -> Segment:
yield Segment("Signature(")
show_comma = True
if self.types:
Expand All @@ -115,7 +113,7 @@ def __rich_console__(self, console, options):
yield Segment("precedence=" + repr(self.precedence))
yield Segment(")")

def __eq__(self, other):
def __eq__(self, other: Any) -> bool:
if isinstance(other, Signature):
return (
self.types,
Expand Down Expand Up @@ -148,7 +146,7 @@ def expand_varargs(self, n: int) -> Tuple[TypeHint, ...]:
else:
return self.types

def __le__(self, other) -> bool:
def __le__(self, other: "Signature") -> bool:
# If the number of types of the signatures are unequal, then the signature
# with the fewer number of types must be expanded using variable arguments.
if not (
Expand Down Expand Up @@ -226,7 +224,7 @@ def __le__(self, other) -> bool:
else:
return False

def match(self, values) -> bool:
def match(self, values: Tuple) -> bool:
"""Check whether values match the signature.

Args:
Expand Down Expand Up @@ -298,7 +296,7 @@ def compute_mismatches(self, values: Tuple) -> Tuple[Set[int], bool]:
return mismatches, varargs_matched


def inspect_signature(f) -> inspect.Signature:
def inspect_signature(f: Callable) -> inspect.Signature:
"""Wrapper of :func:`inspect.signature` which adds support for certain non-function
objects.

Expand Down Expand Up @@ -352,7 +350,7 @@ def _extract_signature(f: Callable, precedence: int = 0) -> Signature:

# Parse and resolve annotation.
if p.annotation is inspect.Parameter.empty:
annotation = typing.Any
annotation = Any
else:
annotation = resolve_type_hint(p.annotation)

Expand Down
Loading