pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Generic `dict` subclass constructed with keyword arguments has an `Unknown` key type

Open crypt0lith opened this issue 3 weeks ago • 0 comments

Constructing a generic dict subclass with keyword arguments leaves the key type Unknown, although the same call on dict infers str:

class D[K, V](dict[K, V]): ...

reveal_type(dict(key=1))
reveal_type(D(key=1))
:3:13 - information: Type of "dict(key=1)" is "dict[str, int]"
:4:13 - information: Type of "D(key=1)" is "D[Unknown, int]"

D inherits dict.__init__, so I'd expect D[str, int], which is what mypy, ty and pyrefly infer. The str comes from the overload's self: dict[str, _VT] annotation, and pyright applies a self annotation's type arguments only when the class being constructed is the one that declares __init__.

Command-line pyright 1.1.414.

crypt0lith avatar Nov 08 '26 13:11 crypt0lith