Skip to content

Incorrect generic recursive code handling in 0.981 #13931

Closed
@racinmat

Description

@racinmat

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions