Skip to content

Commit

Permalink
Merge pull request #63 from erezsh/dev3
Browse files Browse the repository at this point in the history
Better dispatch docs; cleanup in validation
  • Loading branch information
erezsh authored Aug 23, 2024
2 parents 4a7daca + 2bcf5d4 commit 4e299f4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 44 deletions.
27 changes: 13 additions & 14 deletions docs/dispatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ Features:
- Fast


Decorator
---------

.. autofunction:: runtype.multidispatch

.. autofunction:: runtype.Dispatch

.. autoclass:: runtype.dispatch.MultiDispatch
:members: choices, feed_token, copy, pretty, resume_parse, exhaust_lexer, accepts, as_immutable

.. autoclass:: runtype.dispatch.DispatchError



What is multiple-dispatch?
--------------------------

Expand Down Expand Up @@ -120,6 +106,19 @@ Then, the group can be used as a decorator for any number of functions, in any m
Functions will still be grouped by name.


Decorator
---------

.. autofunction:: runtype.multidispatch

.. autofunction:: runtype.Dispatch

.. autoclass:: runtype.dispatch.MultiDispatch
:members: choices, feed_token, copy, pretty, resume_parse, exhaust_lexer, accepts, as_immutable

.. autoclass:: runtype.dispatch.DispatchError


Specificity
-----------

Expand Down
12 changes: 6 additions & 6 deletions runtype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,25 @@ def decorate(self, f: Callable) -> Callable:
>>> from runtype import multidispatch as md
>>> @md
... def add1(i: Optional[int]):
... def add1(i: int):
... return i + 1
>>> @md
... def add1(s: Optional[str]):
... return s + "1"
... def add1(s: str):
... return s + '1'
>>> @md
... def add1(a): # accepts any type (least-specific)
... return (a, 1)
... return ('add', a, 1)
>>> add1(1)
2
>>> add1("1")
>>> add1('1')
11
>>> add1(1.0)
(1.0, 1)
('add', 1.0, 1)
"""
Expand Down
4 changes: 2 additions & 2 deletions runtype/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class MultiDispatch:
"""Creates a dispatch group for multiple dispatch
Parameters:
typesystem - instance for interfacing with the typesystem
typesystem (Typesystem): Which type-system to use for dispatch.
test_subtypes: indices of params that should be matched by subclass instead of isinstance.
(will be soon deprecated, and replaced by using Type[..] annotations)
(will be soon deprecated and replaced by using Type[..] annotations)
"""

def __init__(self, typesystem: TypeSystem, test_subtypes: Sequence[int] = ()):
Expand Down
25 changes: 3 additions & 22 deletions runtype/validation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"User-facing API for validation"

from typing import Any, Dict, List, Tuple, Set, FrozenSet
from functools import wraps

from .common import CHECK_TYPES
from .utils import get_func_signatures
from .pytypes import TypeMismatchError, type_caster
from .pytypes import TypeMismatchError, type_caster, All
from .typesystem import TypeSystem

def ensure_isa(obj, t, sampler=None):
Expand Down Expand Up @@ -54,24 +53,6 @@ def assert_isa(obj, t):
raise TypeError(msg)


_CANONICAL_TYPES = {
# Any: object,
List: list,
Set: set,
FrozenSet: frozenset,
Dict: dict,
Tuple: tuple,
List[Any]: list,
Set[Any]: set,
FrozenSet[Any]: frozenset,
Dict[Any, Any]: dict,
Tuple[Any, ...]: tuple,
}

def to_canonical_type(t):
"Turns List -> list, Dict -> dict, etc."
return _CANONICAL_TYPES.get(t, t)


def issubclass(t1, t2):
"""Test if t1 is a subclass of t2
Expand All @@ -90,9 +71,9 @@ def issubclass(t1, t2):
class PythonTyping(TypeSystem):
isinstance = staticmethod(isa)
issubclass = staticmethod(issubclass)
to_canonical_type = staticmethod(to_canonical_type)
to_canonical_type = type_caster.to_canon
get_type = type
default_type = object
default_type = All


def validate_func(f):
Expand Down

0 comments on commit 4e299f4

Please sign in to comment.