Reputation: 11
I'm trying to write unit tests for types we're using with beartype, our runtime type checker. The tests run as expected, however MyPy will not accept the code.
The following code works, however the MyPy check fails.
def typecheck(val: Any, t: type):
"""
isinstance() does not allow things like Tuple[Int]
"""
@beartype
def f(v: t):
return v
try:
f(val)
except BeartypeCallHintPepParamException:
return False
return True
# passes
def test_typecheck():
assert typecheck((1,), Tuple[int])
assert not typecheck((1,), Tuple[str])
However, I get the following error. The linked to page isn't helpful.
error: Variable "t" is not valid as a type
note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
How do I properly annotate this? Ive tried TypeVar
s but I get the same error.
Upvotes: 1
Views: 613
Reputation: 532238
Assign the type hint directly, rather than letting it be assigned via a function annotation.
def typecheck(val, t):
"""
isinstance() does not allow things like Tuple[Int]
"""
@beartype
def f(v):
return v
f.__annotations__['v'] = t
try:
f(val)
except BeartypeCallHintPepParamException:
return False
return True
# passes
def test_typecheck():
assert typecheck((1,), Tuple[int])
assert not typecheck((1,), Tuple[str])
Upvotes: 1