microsoft / pyright

Static Type Checker for Python
Other
13.15k stars 1.4k forks source link

`isinstance` narrowing incorrect for `object` filter #8520

Closed erictraut closed 1 month ago

erictraut commented 1 month ago

The type guard isinstance(x, object) should be a no-op because all objects in Python derive from object, but pyright's narrowing logic (in the positive direction) is not treating it as a no-op.

import dataclasses
from typing import reveal_type
def func(x: object) -> None:
    if dataclasses.is_dataclass(x) and isinstance(x, object):
        reveal_type(x)
        print(dataclasses.asdict(x))

This comes from this discussion in typeshed.

erictraut commented 1 month ago

This is addressed in pyright 1.1.374.