Skip to content

Dict[str, Callable[...]] Not Handled Properly #7329

@ameily

Description

@ameily

I'm trying to annotate a dict of supported hashing algorithms.

from typing import Callable, Optional, Dict
import hashlib
from _hashlib import HASH

#: Hash algorithm, as returned by hashlib.new()
HashAlgorithm = Callable[[Optional[bytes]], HASH]

ALGORITHMS = {
    'md5': hashlib.md5,
    'sha1': hashlib.sha1,
    'sha224': hashlib.sha224,
    'sha256': hashlib.sha256,
    'sha384': hashlib.sha384,
    'sha512': hashlib.sha512
}  # type: Dict[str, HashAlgorithm]

This is resulting in a weird / confusing error message (ignore line numbers).

file.py823: error: Dict entry 0 has incompatible type "str": "Callable[[Union[bytes, bytearray, memoryview]], _Hash]"; expected "str": "Callable[[Optional[bytes]], Any]"
file.py:823: error: Dict entry 1 has incompatible type "str": "Callable[[Union[bytes, bytearray, memoryview]], _Hash]"; expected "str": "Callable[[Optional[bytes]], Any]"
# same error for each entry in the dict

Switching the value type to just Callbable fixes the issue, but it seems to me that there's an underlying problem here.

Version info:

python -V
python 3.7.3

mypy -V
mypy 0.720

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions