Files
typeshed/stdlib/2and3/hmac.pyi
Michael Lee 97d9f2eb2c Ensures overloads are ordered from narrow to broad (#2138)
This commit reorders any overloads where the first overload was
"shadowing" the second, preventing it from ever being matched by type
checkers that work by selecting the first matching overload alternative.

For example, the first overload alternative below is strictly broader
then the second, preventing it from ever being selected:

    class Parent: pass
    class Child(Parent): pass

    @overload
    def foo(x: *int) -> Parent: ...
    @overload
    def foo(x: int, y: int) -> Child: ...

The correct thing to do is to either delete the second overload or
rearrange them to look like this:

    @overload
    def foo(x: int, y: int) -> Child: ...
    @overload
    def foo(x: *int) -> Parent: ...

Rationale: I'm currently [working on a proposal][0] that would amend
PEP 484 to (a) mandate type checkers check overloads in order and
(b) prohibit overloads where an earlier alternative completely shadows
a later one.

  [0]: https://github.com/python/typing/issues/253#issuecomment-389262904

This would prohibit overloads that look like the example below, where
the first alternative completely shadows the second.

I figured it would be a good idea to make these changes ahead of time:
if my proposal is accepted, it'd make the transition smoother. If not,
this is hopefully a relatively harmless change.

Note: I think some of these overloads could be simplified (e.g.
`reversed(...)`), but I mostly stuck with rearranging them in case I was
wrong. The only overload I actually changed was `hmac.compare_digest` --
I believe the Python 2 version actually accepts unicode.
2018-05-17 09:45:33 -04:00

39 lines
1.1 KiB
Python

# Stubs for hmac
from typing import Any, Callable, Optional, Union, overload, AnyStr
from types import ModuleType
import sys
_B = Union[bytes, bytearray]
# TODO more precise type for object of hashlib
_Hash = Any
digest_size: None
if sys.version_info >= (3, 4):
def new(key: _B, msg: Optional[_B] = ...,
digestmod: Optional[Union[str, Callable[[], _Hash], ModuleType]] = ...) -> HMAC: ...
else:
def new(key: _B, msg: Optional[_B] = ...,
digestmod: Optional[Union[Callable[[], _Hash], ModuleType]] = ...) -> HMAC: ...
class HMAC:
if sys.version_info >= (3,):
digest_size = ... # type: int
if sys.version_info >= (3, 4):
block_size = ... # type: int
name = ... # type: str
def update(self, msg: _B) -> None: ...
def digest(self) -> bytes: ...
def hexdigest(self) -> str: ...
def copy(self) -> HMAC: ...
@overload
def compare_digest(a: bytearray, b: bytearray) -> bool: ...
@overload
def compare_digest(a: AnyStr, b: AnyStr) -> bool: ...
if sys.version_info >= (3, 7):
def digest(key: _B, msg: _B, digest: str) -> bytes: ...