microsoft / pyright

Static Type Checker for Python
Other
13.18k stars 1.41k forks source link

Feature Request: Warning for Pattern Matching on Typed Dict #9078

Open max-muoto opened 3 days ago

max-muoto commented 3 days ago

Pyright currently will raise a warning if you try and do an isinstance check on a typed dict, however it will not do the same if you try and pattern match on typed dictionaries:

from typing import TypedDict

class User(TypedDict):
    name: str
    age: int

class AdminUser(User):
    is_admin: bool

def process_users(users: list[User | AdminUser]):
    for user in users:
        match user:
            case AdminUser(): # No warning, runtime error
                print("admin user")
            case User():
                print("standard user")

isinstance(User(name="Max", age=25), User) # Raises a warning

Pyright Playground

erictraut commented 3 days ago

I think this is a reasonable enhancement request.

You may have noticed this already, but mypy also doesn't include a check for this condition.