DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
http://docs.basedpyright.com/
Other
613 stars 13 forks source link

Support inferring `@final` `TypedDict`s to be `Mapping[str, Union | Of | Types]` #385

Open DetachHead opened 1 month ago

DetachHead commented 1 month ago

original issue: https://github.com/microsoft/pyright/issues/3526

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.