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

Implement <= between two OneOf instances (Fix for issue #15) #17

Merged
merged 1 commit into from
Dec 15, 2022
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
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