mypy icon indicating copy to clipboard operation
mypy copied to clipboard

(🐞) generic `__init__` causes invalid error message "Type application has too few types"

Open KotlinIsland opened this issue 2 years ago • 1 comments

from typing import Generic, TypeVar, _T as T

U = TypeVar("U")

class A(Generic[T]):
    def __init__(self, u: U):
        ...

a: A[int]  # okay
a = A[int]("asdf")  # error: Type application has too few types (2 expected)  [misc]
  • Similar to #13437

KotlinIsland avatar Nov 23 '23 01:11 KotlinIsland

This affects SQLAlchemy with InstrumentedAttribute. Weirdly, you don't even need to instantiate it, simply using the generic class (outside of an annotation) causes the error:

from sqlalchemy.orm import InstrumentedAttribute
foo: InstrumentedAttribute[Any]  # This is fine
foo = InstrumentedAttribute[Any]  # error: Type application has too few types (2 expected)  [misc]

Dreamsorcerer avatar Feb 16 '24 17:02 Dreamsorcerer