pylint icon indicating copy to clipboard operation
pylint copied to clipboard

No type narrowing takes place in `or` statements following negated `isinstance`

Open socketpair opened this issue 4 years ago • 3 comments

Bug description

session: aiohttp.ClientSession
try:
    async with session.get('...') as resp:
        await resp.text()
except Exception as exc:
    if not isinstance(exc, aiohttp.ClientResponseError) or exc.status != 404:
        xxx()
    else:
        yyy()

Configuration

No response

Command used

pylint a.py

Pylint output

E1101: Instance of 'Exception' has no 'status' member (no-member)

Expected behavior

no error here

Pylint version

pylint 2.9.6
astroid 2.6.6
Python 3.9.6 (default, Jul 16 2021, 00:00:00) 
[GCC 11.1.1 20210531 (Red Hat 11.1.1-3)]

OS / Environment

Fedora 34

Additional dependencies

No response

socketpair avatar Aug 27 '21 08:08 socketpair

pylint 3.0.4
astroid 3.0.3
Python 3.12.2 (main, Feb 21 2024, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)]

Still actual

socketpair avatar Mar 21 '24 16:03 socketpair

It appears this affects and without not, as well as or not:

import random


class Foo:
    __slots__ = ()


class Bar(Foo):
    __slots__ = ("x",)
    x: int

    def __init__(self, x: int) -> None:
        self.x = x


def make_foo() -> Foo:
    return Foo()


def main() -> None:
    obj = make_foo()
    if isinstance(obj, Bar) and obj.x > 1: # E1101: Instance of 'Foo' has no 'x' member (no-member)
        print(obj.x) # E1101: Instance of 'Foo' has no 'x' member (no-member)


main()

Hawk777 avatar Jul 25 '24 04:07 Hawk777