Skip to content

Commit

Permalink
Added typing info for isa & issubclass
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Sep 13, 2024
1 parent 2b9fa73 commit 9a234ff
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions runtype/validation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"User-facing API for validation"

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

from .common import CHECK_TYPES
Expand All @@ -24,21 +24,16 @@ def is_subtype(t1, t2):
return ct1 <= ct2


def isa(obj, t):
def isa(obj: Any, t: Union[Type[Any], Tuple[Type[Any], ...]]) -> bool:
"""Tests if 'obj' is of type 't'
Behaves like Python's isinstance, but supports the ``typing`` module and constraints.
"""
ct = type_caster.to_canon(t)
return ct.test_instance(obj)
# try:
# ensure_isa(obj, t)
# return True
# except TypeMismatchError:
# return False


def assert_isa(obj, t):
def assert_isa(obj: Any, t: Union[Type[Any], Tuple[Type[Any], ...]]):
"""Ensure 'obj' is of type 't'. Otherwise, throws a TypeError
Does nothing if Python is run with -O. (like the assert statement)
Expand Down Expand Up @@ -73,7 +68,7 @@ def to_canonical_type(t):
return _CANONICAL_TYPES.get(t, t)


def issubclass(t1, t2):
def issubclass(t1: Type[Any], t2: Union[Type[Any], Tuple[Type[Any], ...]]) -> bool:
"""Test if t1 is a subclass of t2
Parameters:
Expand Down

0 comments on commit 9a234ff

Please sign in to comment.