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

Bugfix in dispatch: Now properly handles Any #65

Merged
merged 1 commit into from
Sep 13, 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
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
Loading