RussBaz / enforce

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

runtime_validation yields a false positive when inheritance is involved #57

Closed marmistrz closed 6 years ago

marmistrz commented 6 years ago

Run the following code:

import enforce

class Foo:
    @staticmethod
    def fun() -> None:
        print(42)

class Bar(Foo):
    @staticmethod
    def fun() -> None:
        print(84)

@enforce.runtime_validation
def meta(x: Foo) -> None:
    x.fun()

meta(Foo())
meta(Bar())

This errors out with:

42
Traceback (most recent call last):
  File "/tmp/test.py", line 19, in <module>
    meta(Bar())
  File "/home/marcin/venv3/golem/lib/python3.6/site-packages/enforce/decorators.py", line 104, in universal
    _args, _kwargs, _ = enforcer.validate_inputs(parameters)
  File "/home/marcin/venv3/golem/lib/python3.6/site-packages/enforce/enforcers.py", line 86, in validate_inputs
    raise RuntimeTypeError(exception_text)
enforce.exceptions.RuntimeTypeError: 
  The following runtime type errors were encountered:
       Argument 'x' was not of type <class '__main__.Foo'>. Actual type was Bar.

The code is validated properly by mypy --strict.

marmistrz commented 6 years ago

My bad, it was invariant type checking here

RussBaz commented 6 years ago

It is OK. Do not worry.