Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wchill opened this issue Mar 1, 2019 · 2 comments
Closed

Comments

@wchill
Copy link

wchill commented Mar 1, 2019

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
@wchill
Copy link
Author

wchill commented Mar 1, 2019

Never mind, I didn't read the documentation on covariant types. Please ignore.

@wchill wchill closed this as completed Mar 1, 2019
@wchill
Copy link
Author

wchill commented Mar 1, 2019

More accurately, this is a dupe of #1114

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant