How to use typeguard - 10 common examples

To help you get started, we’ve selected a few typeguard examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github agronholm / typeguard / tests / test_typeguard.py View on Github external
def foo(a: Union[str, int]):
            assert check_argument_types()
github agronholm / typeguard / tests / test_typeguard.py View on Github external
def foo(a: FooGeneric[str]):
            assert check_argument_types()
github life4 / deal / deal / _testing.py View on Github external
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'],
        )
github jdkandersson / OpenAlchemy / tests / open_alchemy / models_file / model / type / test_database.py View on Github external
)
    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)
github facebookincubator / TestSlide / testslide / lib.py View on Github external
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))
github agronholm / typeguard / tests / test_typeguard.py View on Github external
def foo() -> int:
        assert check_return_type(0)
        return 0
github facebookincubator / TestSlide / testslide / lib.py View on Github external
def get_qualified_name(self):
        return typeguard.qualified_name(self._spec_class)
github agronholm / typeguard / tests / test_typeguard.py View on Github external
def test_qualified_name(inputval, expected):
    assert qualified_name(inputval) == expected
github facebookincubator / TestSlide / testslide / lib.py View on Github external
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,
github agronholm / typeguard / tests / test_typeguard.py View on Github external
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