Python typing.overload() Examples

The following are 24 code examples of typing.overload(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module typing , or try the search function .
Example #1
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_all(self):
        from typing import __all__ as a
        # Just spot-check the first and last of every category.
        self.assertIn('AbstractSet', a)
        self.assertIn('ValuesView', a)
        self.assertIn('cast', a)
        self.assertIn('overload', a)
        if hasattr(contextlib, 'AbstractContextManager'):
            self.assertIn('ContextManager', a)
        # Check that io and re are not exported.
        self.assertNotIn('io', a)
        self.assertNotIn('re', a)
        # Spot-check that stdlib modules aren't exported.
        self.assertNotIn('os', a)
        self.assertNotIn('sys', a)
        # Check that Text is defined.
        self.assertIn('Text', a)
        # Check previously missing classes.
        self.assertIn('SupportsBytes', a)
        self.assertIn('SupportsComplex', a) 
Example #2
Source File: test_type_annotations.py    From pyflakes with MIT License 6 votes vote down vote up
def test_overload_with_multiple_decorators(self):
        self.flakes("""
            from typing import overload
            dec = lambda f: f

            @dec
            @overload
            def f(x):  # type: (int) -> int
                pass

            @dec
            @overload
            def f(x):  # type: (str) -> str
                pass

            @dec
            def f(x): return x
       """) 
Example #3
Source File: test_type_annotations.py    From pyflakes with MIT License 6 votes vote down vote up
def test_typingOverloadAsync(self):
        """Allow intentional redefinitions via @typing.overload (async)"""
        self.flakes("""
        from typing import overload

        @overload
        async def f(s):  # type: (None) -> None
            pass

        @overload
        async def f(s):  # type: (int) -> int
            pass

        async def f(s):
            return s
        """) 
Example #4
Source File: validator.py    From zulip with Apache License 2.0 6 votes vote down vote up
def check_tuple(sub_validators: List[Validator[ResultT]]) -> Validator[Tuple[Any, ...]]:
    def f(var_name: str, val: object) -> Tuple[Any, ...]:
        if not isinstance(val, tuple):
            raise ValidationError(_('{var_name} is not a tuple').format(var_name=var_name))

        desired_len = len(sub_validators)
        if desired_len != len(val):
            raise ValidationError(_('{var_name} should have exactly {desired_len} items').format(
                var_name=var_name, desired_len=desired_len,
            ))

        for i, sub_validator in enumerate(sub_validators):
            vname = f'{var_name}[{i}]'
            sub_validator(vname, val[i])

        return val
    return f

# https://zulip.readthedocs.io/en/latest/testing/mypy.html#using-overload-to-accurately-describe-variations 
Example #5
Source File: compat.py    From pytest with MIT License 5 votes vote down vote up
def overload(f):  # noqa: F811
        return f 
Example #6
Source File: analtype.py    From returns with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def safe_translate_to_function(
    function_def: MypyType,
    ctx: CallableContext,
) -> MypyType:
    """
    Tranforms many other types to something close to callable type.

    There's why we need it:

    - We can use this on real functions
    - We can use this on ``@overload`` functions
    - We can use this on instances with ``__call__``
    - We can use this on ``Type`` types

    It can probably work with other types as well.

    This function allows us to unify this process.
    We also need to disable errors, because we explicitly pass empty args.
    """
    checker = ctx.api.expr_checker  # type: ignore
    checker.msg.disable_errors()
    _return_type, function_def = checker.check_call(
        function_def, [], [], ctx.context, [],
    )
    checker.msg.enable_errors()
    return function_def 
Example #7
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_overload_succeeds(self):
        from typing import overload

        @overload
        def blah():
            pass

        def blah():
            pass

        blah() 
Example #8
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_overload_fails(self):
        from typing import overload

        with self.assertRaises(RuntimeError):

            @overload
            def blah():
                pass

            blah() 
Example #9
Source File: test_type_annotations.py    From pyflakes with MIT License 5 votes vote down vote up
def test_overload_in_class(self):
        self.flakes("""
        from typing import overload

        class C:
            @overload
            def f(self, x):  # type: (int) -> int
                pass

            @overload
            def f(self, x):  # type: (str) -> str
                pass

            def f(self, x): return x
        """) 
Example #10
Source File: test_type_annotations.py    From pyflakes with MIT License 5 votes vote down vote up
def test_typingExtensionsOverload(self):
        """Allow intentional redefinitions via @typing_extensions.overload"""
        self.flakes("""
        import typing_extensions
        from typing_extensions import overload

        @overload
        def f(s):  # type: (None) -> None
            pass

        @overload
        def f(s):  # type: (int) -> int
            pass

        def f(s):
            return s

        @typing_extensions.overload
        def g(s):  # type: (None) -> None
            pass

        @typing_extensions.overload
        def g(s):  # type: (int) -> int
            pass

        def g(s):
            return s
        """) 
Example #11
Source File: test_type_annotations.py    From pyflakes with MIT License 5 votes vote down vote up
def test_typingOverload(self):
        """Allow intentional redefinitions via @typing.overload"""
        self.flakes("""
        import typing
        from typing import overload

        @overload
        def f(s):  # type: (None) -> None
            pass

        @overload
        def f(s):  # type: (int) -> int
            pass

        def f(s):
            return s

        @typing.overload
        def g(s):  # type: (None) -> None
            pass

        @typing.overload
        def g(s):  # type: (int) -> int
            pass

        def g(s):
            return s
        """) 
Example #12
Source File: test_typing.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_all(self):
        from typing import __all__ as a
        # Just spot-check the first and last of every category.
        assert 'AbstractSet' in a
        assert 'ValuesView' in a
        assert 'cast' in a
        assert 'overload' in a
        assert 'io' in a
        assert 're' in a
        # Spot-check that stdlib modules aren't exported.
        assert 'os' not in a
        assert 'sys' not in a 
Example #13
Source File: test_typing.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_overload_fails(self):
        from typing import overload

        with self.assertRaises(RuntimeError):
            @overload
            def blah():
                pass 
Example #14
Source File: keyutils.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def __iter__(self) -> typing.Iterator[KeyInfo]:
        """Iterate over KeyInfo objects."""
        for key_and_modifiers in self._iter_keys():
            key = Qt.Key(int(key_and_modifiers) & ~Qt.KeyboardModifierMask)
            modifiers = Qt.KeyboardModifiers(  # type: ignore[call-overload]
                int(key_and_modifiers) & Qt.KeyboardModifierMask)
            yield KeyInfo(key=key, modifiers=modifiers) 
Example #15
Source File: microbatch.py    From torchgpipe with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index: int) -> Tensor:
        if not self.atomic:
            return self.tensors[index]

        if index != 0:
            raise IndexError('atomic batch allows index 0 only')

        return self.tensor

    # NOTE(sublee): pyflakes can't detect "overload" instead of "typing.overload". 
Example #16
Source File: microbatch.py    From torchgpipe with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index: int) -> Tensor:
        if not self.atomic:
            return self.tensors[index]

        if index != 0:
            raise IndexError('atomic batch allows index 0 only')

        return self.tensor

    # NOTE(sublee): pyflakes can't detect "overload" instead of "typing.overload". 
Example #17
Source File: microbatch.py    From torchgpipe with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index: int) -> Tensor:
        if not self.atomic:
            return self.tensors[index]

        if index != 0:
            raise IndexError('atomic batch allows index 0 only')

        return self.tensor

    # NOTE(sublee): pyflakes can't detect "overload" instead of "typing.overload". 
Example #18
Source File: microbatch.py    From torchgpipe with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index: int) -> Tensor:
        if not self.atomic:
            return self.tensors[index]

        if index != 0:
            raise IndexError('atomic batch allows index 0 only')

        return self.tensor

    # NOTE(sublee): pyflakes can't detect "overload" instead of "typing.overload". 
Example #19
Source File: microbatch.py    From torchgpipe with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index: int) -> Tensor:
        if not self.atomic:
            return self.tensors[index]

        if index != 0:
            raise IndexError('atomic batch allows index 0 only')

        return self.tensor

    # NOTE(sublee): pyflakes can't detect "overload" instead of "typing.overload". 
Example #20
Source File: microbatch.py    From torchgpipe with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index: int) -> Tensor:
        if not self.atomic:
            return self.tensors[index]

        if index != 0:
            raise IndexError('atomic batch allows index 0 only')

        return self.tensor

    # NOTE(sublee): pyflakes can't detect "overload" instead of "typing.overload". 
Example #21
Source File: microbatch.py    From torchgpipe with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index: int) -> Tensor:
        if not self.atomic:
            return self.tensors[index]

        if index != 0:
            raise IndexError('atomic batch allows index 0 only')

        return self.tensor

    # NOTE(sublee): pyflakes can't detect "overload" instead of "typing.overload". 
Example #22
Source File: api.py    From sentry-python with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def overload(x):
        # type: (T) -> T
        return x 
Example #23
Source File: hub.py    From sentry-python with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def overload(x):
        # type: (T) -> T
        return x 
Example #24
Source File: serverless.py    From sentry-python with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def overload(x):
        # type: (F) -> F
        return x