Skip to content

Support inferring @final TypedDicts to be Mapping[str, Union | Of | Types] #3526

@RobertCraigie

Description

@RobertCraigie

Is your feature request related to a problem? Please describe.

from __future__ import annotations
from typing import Mapping, TypedDict, final

FileTypes = bytes

# this has to be key agnostic
RequestFiles = Mapping[str, FileTypes]

def post(files: RequestFiles | None = None):
    ...

@final
class FileParams(TypedDict):
    file: FileTypes

def foo(files: FileParams):
    # error: Argument of type "FileParams" cannot be assigned to parameter "files" of type "RequestFiles | None" in function "post"
    post(files)

Describe the solution you'd like

Ideally pyright could infer that the FileParams TypedDict could be safely passed to the post function as it is known to only contain keys with the type FileTypes and cannot be subclassed to add more keys because it is decorated with @final.

Additional context

I apologise if there's prior discussion surrounding this, I couldn't find any.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions