How to use the typeguard.ForwardRefPolicy.GUESS function in typeguard

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
    @pytest.mark.parametrize('policy', [ForwardRefPolicy.WARN, ForwardRefPolicy.GUESS],
                             ids=['warn', 'guess'])
    def test_forward_ref_policy_resolution_fails(self, checker, policy):
        def unresolvable_annotation(x: 'OrderedDict'):  # noqa
            pass

        checker.annotation_policy = policy
        gc.collect()  # prevent find_function() from finding more than one instance of the function
        with checker, pytest.warns(TypeHintWarning) as record:
            unresolvable_annotation({})

        assert len(record) == 1
        assert ("unresolvable_annotation: name 'OrderedDict' is not defined"
                in str(record[0].message))
        assert 'x' not in unresolvable_annotation.__annotations__
github agronholm / typeguard / tests / test_typeguard.py View on Github external
def test_forward_ref_policy_guess(self, checker):
        import collections

        def unresolvable_annotation(x: 'OrderedDict'):  # noqa
            pass

        checker.annotation_policy = ForwardRefPolicy.GUESS
        with checker, pytest.warns(TypeHintWarning) as record:
            unresolvable_annotation(collections.OrderedDict())

        assert len(record) == 1
        assert str(record[0].message).startswith("Replaced forward declaration 'OrderedDict' in")
        assert unresolvable_annotation.__annotations__['x'] is collections.OrderedDict