Skip to content

allow TypedDict instance literals #820

@sslivkoff

Description

@sslivkoff

would like ty to allow literal instantiation of TypedDict's, as in:

class Config(typing.TypedDict):
    version: str
    tracked_tables: list[TrackedTable]


def get_default_config() -> absorb.Config:
    # doesn't work
    return {
        'version': '0.1.0',
        'tracked_tables': [],
    }

it gives this error:

error[invalid-return-type]: Return type does not match returned value
  --> absorb/ops/config.py:12:29
   |
12 |   def get_default_config() -> Config:
   |                               ------------- Expected `Config` because of return type
13 |       return {
   |  ____________^
14 | |         'version': '0.1.0',
15 | |         'tracked_tables': [],
16 | |     }
   | |_____^ expected `Config`, found `dict[Unknown, Unknown]`
   |
info: rule `invalid-return-type` is enabled by default

instead I had to convert many parts of my codebase to using this style to pass ty's checks:

def get_default_config() -> Config:
    return Config(
        version=absorb.__version__,
        tracked_tables=[],
    )

this is undesirable because:

  1. one of the advantages of TypedDict's is being able to use them with normal simple dict syntax
  2. mypy (which I usually use) supports this style of TypedDict instance literals
  3. the two syntaxes are nearly functionally equivalent

Metadata

Metadata

Assignees

No one assigned

    Labels

    typing semanticstyping-module features, spec compliance, etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions