Skip to content

Commit

Permalink
Merge pull request #17 from erezsh/issue15
Browse files Browse the repository at this point in the history
Implement <= between two OneOf instances (Fix for issue #15)
  • Loading branch information
erezsh committed Dec 15, 2022
2 parents 1a689cc + bc36d4c commit d21d4f7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion runtype/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .dataclass import dataclass
from .dispatch import DispatchError, MultiDispatch
from .validation import PythonTyping, TypeSystem, TypeMismatchError, assert_isa, isa, issubclass, validate_func
from .validation import PythonTyping, TypeSystem, TypeMismatchError, assert_isa, isa, issubclass, validate_func, is_subtype
from .pytypes import Constraint, String, Int

__version__ = "0.3.1"
Expand Down
2 changes: 2 additions & 0 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def __init__(self, values):
self.values = values

def __le__(self, other):
if isinstance(other, OneOf):
return set(self.values) <= set(other.values)
return NotImplemented

def validate_instance(self, obj, sampler=None):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
logging.basicConfig(level=logging.INFO)

from runtype import Dispatch, DispatchError, dataclass, isa, issubclass, assert_isa, String, Int, validate_func
from runtype import Dispatch, DispatchError, dataclass, isa, is_subtype, issubclass, assert_isa, String, Int, validate_func
from runtype.dataclass import Configuration


Expand Down Expand Up @@ -106,6 +106,8 @@ def test_py38(self):
assert isa('a', typing.Literal['a', 'b'])
assert not isa('c', typing.Literal['a', 'b'])

assert is_subtype(typing.Literal[1], typing.Literal[1,2])

def test_validate_func(self):
@validate_func
def f(a: int, b: str, c: List[int] = []):
Expand Down

0 comments on commit d21d4f7

Please sign in to comment.