Skip to content

Commit

Permalink
Merge pull request #65 from erezsh/sep13_2024
Browse files Browse the repository at this point in the history
Bugfix in dispatch: Now properly handles Any
  • Loading branch information
erezsh authored Sep 13, 2024
2 parents e5d056f + e92b759 commit f74c8d1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 4 additions & 1 deletion runtype/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,15 @@ def define_function(self, f):

def choose_most_specific_function(self, args, *funcs):
issubclass = self.typesystem.issubclass
any_type = self.typesystem.any_type

class IsSubclass:
def __init__(self, k):
self.i, self.t = k

def __lt__(self, other):
if self.t is any_type:
return False
return issubclass(self.t, other.t)

most_specific_per_param = []
Expand All @@ -208,7 +211,7 @@ def __lt__(self, other):
if ms_t == t:
# Add more indexes with the same type
ms_set.add(i)
elif issubclass(t, ms_t) or not issubclass(ms_t, t):
elif (issubclass(t, ms_t) and t is not any_type) or not issubclass(ms_t, t):
# Possibly ambiguous. We might throw an error below
# TODO secondary candidates should still obscure less specific candidates
# by only considering the top match, we are ignoring them
Expand Down
4 changes: 3 additions & 1 deletion runtype/typesystem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

from typing import Any

class TypeSystem:
def isinstance(self, obj, t: type) -> bool:
Expand All @@ -14,6 +14,7 @@ def get_type(self, obj) -> type:
raise NotImplementedError()

default_type: type = NotImplemented
any_type: type = NotImplemented



Expand All @@ -22,3 +23,4 @@ class PythonBasic(TypeSystem):
issubclass = issubclass
get_type = type
default_type = object
any_type = Any
6 changes: 4 additions & 2 deletions runtype/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

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

def ensure_isa(obj, t, sampler=None):
Expand Down Expand Up @@ -69,7 +70,8 @@ class PythonTyping(TypeSystem):
issubclass = staticmethod(issubclass)
to_canonical_type = type_caster.to_canon
get_type = type
default_type = All
default_type = pytypes.All
any_type = pytypes.Any


def validate_func(f):
Expand Down

0 comments on commit f74c8d1

Please sign in to comment.