Skip to content

Incorrect generic recursive code handling in 0.981 #13931

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
racinmat opened this issue Oct 21, 2022 · 1 comment
Closed

Incorrect generic recursive code handling in 0.981 #13931

racinmat opened this issue Oct 21, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@racinmat
Copy link

Bug Report

Recursive generic code has broken inference

from typing import TypeVar, List, Dict, Any

V = TypeVar("V", List[Any], Dict[Any, Any], float, int, str, bool)

def normalize_keys(value: V) -> V:
    if isinstance(value, list):
        return [normalize_keys(v) for v in value]
    elif isinstance(value, dict):
        return {_normalize_key(k): normalize_keys(v) for k, v in value.items()}
    else:
        return value

def _normalize_key(key: str) -> str:
    return key.lower().strip("_").replace(".", "_")

To Reproduce

MWE in mypy sandbox

Expected Behavior

It should pass as this is valid code.
It passes in mypy 0.971, but reports error in mypy 0.981 and 0.982.

Actual Behavior

Throws error

main.py:10: error: Incompatible return value type (got "Dict[str, Any]", expected "float")

Your Environment

see the sandbox

@racinmat racinmat added the bug mypy got something wrong label Oct 21, 2022
@ilevkivskyi
Copy link
Member

This currently works as expected (because we applied a hack, but there is #13800 to track more permanent solution).

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

No branches or pull requests

2 participants