8000 [mypy] Allow variable typed `type[T]` as generic type variable · Issue #11636 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

[mypy] Allow variable typed type[T] as generic type variable #11636

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
3 of 13 tasks
ROCKTAKEY opened this issue Mar 27, 2025 · 1 comment
Closed
3 of 13 tasks

[mypy] Allow variable typed type[T] as generic type variable #11636

ROCKTAKEY opened this issue Mar 27, 2025 · 1 comment

Comments

@ROCKTAKEY
Copy link

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

For pydantic, generic type information is needed at runtime. Thus, we must recieve type as a function argument and pass as generic type to create generic pydantic model. However, the code bellow causes error on mypy:

from pydantic import BaseModel


class C[T](BaseModel): ...


def f[T](t: type[T]) -> C[T]:
    return C[t]()  # b.py:8: error: Variable "t" is not valid as a type  [valid-type]

This issue is also known on mypy, because pyright does not cause this error, though mypy does not fix it for a long time.

Probably, this problem occurs only with libraries using generic type at runtime, such as Pydantic. I think a solution with the mypy plugin provided by pydantic could be considered.

I'm sorry if it is impossible, I do not know much about mypy plugin limitation.

Thanks!

Affected Components

@Viicos
Copy link
Member
Viicos commented Mar 28, 2025

I think a solution with the mypy plugin provided by pydantic could be considered.

It is not the job of the Pydantic plugin to fix non Pydantic-related issues. It is probably also not be possible to do so, as the mypy plugin interface exposes limited capabilities, and this issue is probably present in mypy's core logic.

@Viicos Viicos closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
0