RussBaz / enforce

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

Union Fails for Nested Types #8

Closed TheDataLeek closed 7 years ago

TheDataLeek commented 8 years ago

In the current iteration of the dev branch, Union type hints are not enforced correctly in certain situations (generally ones where there is nesting involved). The following example does not throw an error.

@runtime_validation
def test_func(x: typing.Union[float, typing.List[str]]) -> int:
    return 5
test_func([1, 2, 3, 4])

I'm working on debugging it as well.

TheDataLeek commented 8 years ago

So, looking at this problem a lot more closely, it's rather due to something not quite working with the List type. As an example of demonstrated behavior:

@runtime_validation
def foo(x: List[str]) -> str:
    return x[0]
foo(['a', 2, 3])  # This will work because the first element is str
foo([1, 2, 3])   # This will fail which is good

Will continue to update as I isolate the problem. I think it's something to do with how the parser sets up the TypeTree.

RussBaz commented 7 years ago

This is actually fixed. Closing the issue.