Skip to content
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

Generic type falling back to object - possibly related to type[ Union ]? #18265

Open
JakeSummers opened this issue Dec 8, 2024 · 1 comment · May be fixed by #18266
Open

Generic type falling back to object - possibly related to type[ Union ]? #18265

JakeSummers opened this issue Dec 8, 2024 · 1 comment · May be fixed by #18266
Labels
bug mypy got something wrong

Comments

@JakeSummers
Copy link

Bug Report

The following code produces a type error but it shouldn't.

from dataclasses import dataclass
from typing import TypeVar, Mapping, reveal_type


@dataclass
class A:
    foo: str = "a"

@dataclass
class B:
    bar: str = "b"

lookup_table: Mapping[str, type[A] | type[B]] = {
    "a": A,
    "b": B
}

reveal_type(lookup_table)  # note: Revealed type is "typing.Mapping[builtins.str, Union[type[simple.A], type[simple.B]]]"

T = TypeVar("T")

def load(lookup_table: Mapping[str, type[T]], lookup_key:str) -> T:
    con: type[T] = lookup_table[lookup_key]
    instance: T = con()
    return instance

example_a: A | B = load(lookup_table, "a")  # error: Incompatible types in assignment (expression has type "object", variable has type "A | B")
print(example_a)

Error:

error: Incompatible types in assignment (expression has type "object", variable has type "A | B")  [assignment]

Your Environment

  • Mypy version used: 1.13.0 (latest on mypy Playground)
  • Mypy command-line flags: None.
  • Python version used: 3.11, 3.12, 3.13
@JakeSummers JakeSummers added the bug mypy got something wrong label Dec 8, 2024
@sterliakov
Copy link
Contributor

Duplicates #12115

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

Successfully merging a pull request may close this issue.

2 participants