Skip to content

Support dict(name=x, ...) #239

Closed
Closed

Description

@JukkaL

Support dict(name=x, ...) in the type checker. Infer key type as str and value type based on keyword argument types.

We can't represent the signature of dict precisely using current functionality alone, i.e. we need some new type checker functionality.

Activity

  1. ghost assigned on Dec 24, 2013
  2. removed their assignment
    on Jul 25, 2014
  3. JukkaL commented on Oct 12, 2014

    @JukkaL
    CollaboratorAuthor

    The common case should actually be pretty easy to implement by just special casing this in the semantic analyzer and using the analyzed attribute of the dict(...) CallExpr to store a DictExpr when the call only uses keyword arguments. The dict literal keys should be string literals generated from the keyword argument names, and the dict values are just the argument expressions.

    The **args case is trickier, but this seems rare so we don't need to support it yet.

  4. JukkaL commented on Aug 23, 2015

    @JukkaL
    CollaboratorAuthor

    Increasing priority as this is actually a pretty common issue.

  5. gvanrossum commented on Nov 22, 2015

    @gvanrossum
    Member

    Yeah, I see this a lot in our latest demo project.

  6. JukkaL commented on Nov 22, 2015

    @JukkaL
    CollaboratorAuthor

    This now works but for heterogeneous dictionaries the inferred type will likely be Dict[str, object] unless there is an explicit type annotation, so this isn't super useful yet.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions