Skip to content

Incorrect inference for dict() constructor argument types #401

Closed as duplicate

Description

@analog-cbarber

Describe the Bug

Using pyrefly 0.17.1

When I use the dict constructor with keyword arguments like:

dict(foo = 4, bar = "xxx", ...)

then it appears that pyrefly incorrectly infers that the type signature of dict is **kwargs: int rather than **kwargs: Any. I get an error like:

Keyword argument `bar` with type `str` is not assignable to parameter `**kwargs: with type `int` in function `dict.__init__`

It seems that it just assumes that the type of the first keyword value should be the type of all remaining values.

As a workaround, you can cast the first keyword value to Any:

dict(foo = cast(Any,4), bar = "xxx", ...)

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCCKcANFAMYCGAzjAFB0AmS5MAFHVF1AB5QC8UACwlO3OAKgBGAHQAmUdygAvSQCIemtaICUDZqw5K+gqrTZFSwnYvGTZCsV1WCNW3QyA

(Only applicable for extension issues) IDE Information

No response

Activity

  1. yangdanny97 commented on May 29, 2025

    @yangdanny97
    Collaborator

    Thanks for reporting! This looks like the same root cause as #105

  2. added theissue type on May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions