RussBaz / enforce

Python 3.5+ runtime type checking for integration testing and data validation
542 stars 21 forks source link

Extended support from typing.List to typing.Iterable, and from typing.Dict to typing.Mapping #64

Closed smarie closed 6 years ago

smarie commented 6 years ago

Fixes #52, Fixes #51, fixes #47.

You might wish to include the following basic tests:

from enforce import runtime_validation, config
from enforce.exceptions import RuntimeTypeError
config(dict(mode='covariant'))  # by the way, so sad that this is not the default :)

########## SEQUENCE / ITERABLE / SET / GENERATOR
from typing import Sequence, Iterable, Set, Generator

@runtime_validation
def seq(s: Sequence[str]):
    pass

@runtime_validation
def it(s: Iterable[str]):
    pass

seq(['a'])
# transform this into appropriate test handler
try:
    seq(['r', 1])
    raise Exception('failed!')
except RuntimeTypeError:
    pass

it(['a'])
# transform this into appropriate test handler
try:
    it(['r', 1])
    raise Exception('failed!')
except RuntimeTypeError:
    pass

@runtime_validation
def st(m: Set[int]):
    pass

st({2, 2})
# transform this into appropriate test handler
try:
    st({'r', 1})
    raise Exception('failed!')
except RuntimeTypeError:
    pass

@runtime_validation
def generator() -> Generator[int, None, None]:
    i = 0
    while True:
        if i == 0:
            yield i
        else:
            yield 'a string! it cannot be detected by enforce but thats a bit normal'
        i += 1

g = generator()
print(next(g))
print(next(g))

########## MAPPING
from typing import Mapping

@runtime_validation
def bar(m: Mapping[int, int]):
    pass

bar({2: 2})
# transform this into appropriate test handler
try:
    bar({'r': 1})
    raise Exception('failed!')
except RuntimeTypeError:
    pass
coveralls commented 6 years ago

Coverage Status

Coverage decreased (-0.04%) to 93.628% when pulling e0defb4427d8e63e2f888183eff964b323cb1de6 on smarie:generics-fix into caf1dc3984c595a120882337fa6c2a7d23d90201 on RussBaz:master.

RussBaz commented 6 years ago

Wow, thanks! However, I must say that you are trying to push your PRs into the 'master' branch. The development is actually done in the 'dev' branch, I have been pushing updates into it since the last release. Please check it with the a 'dev' branch first. There were few important changes. Thank you again for taking your time to help!

smarie commented 6 years ago

Ok I'll try to find a way to create a single pull request from dev branch, containing both fixes