microsoft / pyright

Static Type Checker for Python
Other
13.48k stars 1.48k forks source link

`Optional[TypedDict] and SomeOtherType` should be inferred to be `Optional[SomeOtherType]` #9435

Closed mgedmin closed 1 week ago

mgedmin commented 2 weeks ago

Describe the bug

Pyright seems to not understand that a TypedDict with at least one required field will be considered true in a boolean context, and therefore the type of a_typed_dict_or_none and some_other_thing will either be NoneType or the type of some_other_thing.

Code or Screenshots

from typing import TypedDict

class Item(TypedDict):
    data: int

def get_data(item: Item | None) -> int | None:
    return item and item['data']

which produces a

$ pyright pywrong.py
/home/mg/tmp/pywrong.py
  /home/mg/tmp/pywrong.py:9:12 - error: Type "Item | int | None" is not assignable to return type "int | None"
    Type "Item | int | None" is not assignable to type "int | None"
      Type "Item" is not assignable to type "int | None"
        "Item" is not assignable to "int"
        "Item" is not assignable to "None" (reportReturnType)
1 error, 0 warnings, 0 informations 

Mypy has no issues with this code.

VS Code extension or command-line Are you running pyright as a VS Code extension, a language server in another editor, integrated into Pylance, or the command-line tool? Which version?

Command-line tool, version 1.1.388

erictraut commented 2 weeks ago

I think this is a reasonable enhancement request. It appears that mypy has support for this, although it doesn't deal with all edge cases correctly.

I'll look at adding this to pyright.

erictraut commented 2 weeks ago

This will be included in the next release.

erictraut commented 1 week ago

This is addressed in pyright 1.1.389