Skip to content

Type check fails for dictionary with different but compatible key type #6496

Closed
@wchill

Description

@wchill

If I have a function that accepts a dictionary that can accept a variety of key types, and then pass in a dictionary whose key type is only a subset, the code fails to type check.

Example code:

def foo(input_dict: Dict[Optional[str], Any]) -> Dict[str, str]:
    return {'bar': 'baz'}

a = {'b': 'c'}    # type: Dict[str, Any]
foo(a)    # fails to type check

This also seems to apply with the following function definition:

def bar(input_dict: Dict[Union[str, bytes], Any]) -> Dict[str, str]:
    return {'bar': 'baz'}

a = {'b': 'c'}    # type: Dict[str, Any]
bar(a)    # fails to type check

I'm using these mypy flags and mypy 0.670:

[mypy]
python_version=3.5
allow_untyped_globals = True
check_untyped_defs = True
disallow_any_generics = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
ignore_missing_imports = True
no_implicit_optional = True
show_column_numbers = True
strict_equality = False
warn_return_any = True
warn_redundant_casts = True
warn_unused_ignores = True

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