Skip to content

Incorrect type narrow/infer when creating dict with dict(key=value) (not for {"key":value}) #960

@DanielYang59

Description

@DanielYang59

Summary

Seems like a bug (if I understand everything correctly) regarding type narrowing of dict with dict() interface.

from typing import Any


name: str = "hello"
d: dict[str, Any] = dict(name=name)  # {"name": name} wouldn't
d["age"] = 0

Triggers:

error[invalid-assignment]: Method `__setitem__` of type `bound method dict[str, str].__setitem__(key: str, value: str, /) -> None` cannot be called with a key of type `Literal["age"]` and a value of type `Literal[0]` on object of type `dict[str, str]`
 --> debug.py:6:1
  |
4 | name: str = "hello"
5 | d: dict[str, Any] = dict(name=name)  # {"name": name} wouldn't
6 | d["age"] = 0
  | ^
  |
info: rule `invalid-assignment` is enabled by default

Found 1 diagnostic

Version

ty 0.0.1-alpha.17 (1712284 2025-08-06)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bidirectional inferenceInference of types that takes into account the context of a declared type or expected typegenericsBugs or features relating to ty's generics implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions