Skip to content

Commit

Permalink
typing: add more type annotations (#203)
Browse files Browse the repository at this point in the history
* typing(type): add type annotations to ResolveableType and subclasses

Signed-off-by: Nathaniel Starkman <[email protected]>

* typing(type): add type annotations to add_promotion_rule

Signed-off-by: Nathaniel Starkman <[email protected]>

---------

Signed-off-by: Nathaniel Starkman <[email protected]>
  • Loading branch information
nstarman authored Jan 8, 2025
1 parent bf1d894 commit 89831d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion plum/promotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _promotion_rule(type1, type2):


@_dispatch
def add_promotion_rule(type1, type2, type_to):
def add_promotion_rule(type1: object, type2: object, type_to: type) -> None:
"""Add a promotion rule.
Args:
Expand Down
29 changes: 16 additions & 13 deletions plum/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import sys
import typing
import warnings
from typing import Hashable, Union

from typing_extensions import Self, TypeGuard

from beartype.vale._core._valecore import BeartypeValidator

Expand Down Expand Up @@ -32,14 +35,14 @@ class ResolvableType(type):
name (str): Name of the type to be delivered.
"""

def __init__(self, name):
def __init__(self, name: str) -> None:
type.__init__(self, name, (), {})
self._type = None

def __new__(self, name):
def __new__(self, name: str) -> Self:
return type.__new__(self, name, (), {})

def deliver(self, type):
def deliver(self, type: type) -> Self:
"""Deliver the type.
Args:
Expand All @@ -51,7 +54,7 @@ def deliver(self, type):
self._type = type
return self

def resolve(self):
def resolve(self) -> Union[type, Self]:
"""Resolve the type.
Returns:
Expand All @@ -73,11 +76,11 @@ class PromisedType(ResolvableType):
`"SomeType"`.
"""

def __init__(self, name="SomeType"):
def __init__(self, name: str = "SomeType") -> None:
ResolvableType.__init__(self, f"PromisedType[{name}]")
self._name = name

def __new__(cls, name="SomeType"):
def __new__(cls, name: str = "SomeType") -> Self:
return ResolvableType.__new__(cls, f"PromisedType[{name}]")


Expand All @@ -91,18 +94,18 @@ class ModuleType(ResolvableType):
do not raise an `AttributeError`.
"""

def __init__(self, module, name, allow_fail=False):
def __init__(self, module: str, name: str, allow_fail: bool = False) -> None:
if module in {"__builtin__", "__builtins__"}:
module = "builtins"
ResolvableType.__init__(self, f"ModuleType[{module}.{name}]")
self._name = name
self._module = module
self._allow_fail = allow_fail

def __new__(cls, module, name, allow_fail=False):
def __new__(cls, module: str, name: str, allow_fail: bool = False) -> Self:
return ResolvableType.__new__(cls, f"ModuleType[{module}.{name}]")

def retrieve(self):
def retrieve(self) -> bool:
"""Attempt to retrieve the type from the reference module.
Returns:
Expand All @@ -120,7 +123,7 @@ def retrieve(self):
return self._type is not None


def _is_hint(x):
def _is_hint(x: object) -> bool:
"""Check if an object is a type hint.
Args:
Expand All @@ -144,7 +147,7 @@ def _is_hint(x):
return False


def _hashable(x):
def _hashable(x: object) -> TypeGuard[Hashable]:
"""Check if an object is hashable.
Args:
Expand Down Expand Up @@ -242,7 +245,7 @@ def resolve_type_hint(x):
return x


def is_faithful(x):
def is_faithful(x) -> bool:
"""Check whether a type hint is faithful.
A type or type hint `t` is defined _faithful_ if, for all `x`, the following holds
Expand All @@ -265,7 +268,7 @@ class UnfaithfulType:
return _is_faithful(resolve_type_hint(x))


def _is_faithful(x):
def _is_faithful(x) -> bool:
if _is_hint(x):
origin = get_origin(x)
args = get_args(x)
Expand Down

0 comments on commit 89831d9

Please sign in to comment.