Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def foo(a: Union[str, int]):
assert check_argument_types()
def foo(a: FooGeneric[str]):
assert check_argument_types()
def _check_result(self, result: typing.Any) -> None:
hints = typing.get_type_hints(self.func)
if 'return' not in hints:
return
typeguard.check_type(
argname='return',
value=result,
expected_type=hints['return'],
)
)
calculated_type_str = models_file._model._type.model(artifacts=artifacts)
calculated_type = eval(calculated_type_str) # pylint: disable=eval-used
# Creating models
base.metadata.create_all(engine)
# Creating model instance
model_instance = model(column=value)
session = sessionmaker()
session.add(model_instance)
session.flush()
# Querying session
queried_model = session.query(model).first()
assert queried_model.column == value
typeguard.check_type("queried_model.column", queried_model.column, calculated_type)
argname,
inner_value,
inner_expected_type,
*args,
globals=globals,
locals=locals,
**kwargs,
)
with unittest.mock.patch.object(
typeguard, "check_type", new=wrapped_check_type
), unittest.mock.patch.object(
typeguard, "qualified_name", new=wrapped_qualified_name
):
try:
typeguard.check_type(name, value, expected_type)
except TypeError as type_error:
raise TypeCheckError(str(type_error))
def foo() -> int:
assert check_return_type(0)
return 0
def get_qualified_name(self):
return typeguard.qualified_name(self._spec_class)
def test_qualified_name(inputval, expected):
assert qualified_name(inputval) == expected
def _validate_argument_type(expected_type, name: str, value) -> None:
if "~" in str(expected_type):
# this means that we have a TypeVar type, and those require
# checking all the types of all the params as a whole, but we
# don't have a good way of getting right now
# TODO: #165
return
original_check_type = typeguard.check_type
original_qualified_name = typeguard.qualified_name
def wrapped_qualified_name(obj):
"""Needed to be able to show the useful qualified name for mock specs"""
if isinstance(obj, WrappedMock):
return obj.get_qualified_name()
return original_qualified_name(obj)
def wrapped_check_type(
argname,
inner_value,
inner_expected_type,
*args,
globals: Optional[Dict[str, Any]] = None,
locals: Optional[Dict[str, Any]] = None,
**kwargs,
def test_nested(self):
"""Test that nesting of type checker context managers works as expected."""
def foo(a: int):
pass
with warnings.catch_warnings(record=True):
warnings.simplefilter('ignore', DeprecationWarning)
parent = TypeChecker(__name__)
child = TypeChecker(__name__)
with parent, pytest.warns(TypeWarning) as record:
foo('x')
with child:
foo('x')
assert len(record) == 3