Skip to content

invalid-key not detected for intersections that include TypedDicts #2591

@mudgett06

Description

@mudgett06

Summary

Summary

After narrowing a TypedDict | None type via if not x: raise, ty no
longer detects invalid key access. The invalid-key rule works correctly when the variable is directly typed as the TypedDict.

Reproduction

from typing import TypedDict                                             
                                                                         
class Person(TypedDict):                                                 
    name: str                                                            
    age: int                                                             
                                                                         
def get_person() -> Person | None:                                       
    return None                                                          
                                                                         
def test() -> None:                                                      
    p = get_person()                                                     
    if not p:                                                            
        raise ValueError("No person")                                    
    # After narrowing, p should be Person - but invalid-key not triggered
    print(p["nonexistent"])  # No error (should be invalid-key)          
                                                                         
def test_direct(p: Person) -> None:                                      
    # Direct parameter works correctly                                   
    print(p["nonexistent"])  # Error: invalid-key ✓                      
                                                                         

Expected Behavior

Both test() and test_direct() should emit invalid-key for
p["nonexistent"].

Actual Behavior

Only test_direct() emits the error. After narrowing from Person | None,
the TypedDict key information is lost.

Related

Possibly related to TypedDict tracking issue #154

Version

0.0.13

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions