Skip to content

Mypy inappropriately infers class type as _typeshed.DataclassInstance #14941

Closed
@binishkaspar

Description

@binishkaspar

Bug Report
In the following code snippet, mypy incorrectly infers the type of typ as _typeshed.DataclassInstance. This issue was not present in previous versions of mypy

To Reproduce

from dataclasses import dataclass, is_dataclass
from typing import TypeVar, Any, Type, cast


@dataclass
class A:
  a: int


T = TypeVar('T')


def parse(typ: Type[T], raw: dict[str, Any]) -> T:
  if not is_dataclass(typ):
    raise Exception('Unsupported type')

  parsed = typ(**raw)  # type: ignore[call-arg]
  return parsed


parse(A, {'a': 2})

Expected Behavior

There should be no type error

Actual Behavior

Incompatible return value type (got "DataclassInstance", expected "T") \[return-value\]

Your Environment

  • Mypy version used: 1.1.1
  • Python version used: 3.11

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