microsoft / pyright

Static Type Checker for Python
Other
13.04k stars 1.39k forks source link

`self` type is incorrect after isinstance call #8350

Closed Hunterlige closed 1 month ago

Hunterlige commented 1 month ago

Describe the bug Hi

isinstance(self, type) no longer updates the type of the self. The behavior change between v1.1.369 and v1.1.370.

Code or Screenshots Playground reproduction: https://pyright-play.net/?pyrightVersion=1.1.370&pythonVersion=3.13&strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMogCmAboQIYA2A%2BvAoQLABQTAxhWQM4dQBCnhACgCUALiZQJUACaFgsQhxgCOhCsFHjJWmXJgKlADxFQA8gg2MtVyQhCpDQzdaRyUYfEg6pFZFC0EqagA0puZiltZWIGSehFAAapQAroQAoiDgIMJOUSTk1LQBquo5WnqKysWOjGyc3ACCAnwqFloIdazsXLxN-K02HcyMZlAAvFAC9VAAPrzVTAtDi4tAA

from typing import reveal_type

class Base():
    def test(self):
        def test(x: Op):
            print(x)
        if not isinstance(self, Op):
            raise ValueError()
        reveal_type(self)
        test(self)
class A(Base):
    pass
class B(Base):
    pass

Op = (A | B)

Works with pyright v1.1.369, but does not work with pyright v1.1.370

VS Code extension or command-line Command-line, pyright v1.1.370

erictraut commented 1 month ago

Although it may not be immediately obvious, this is a duplicate of #8302. A fix is in place and will be included in the next release. Apologies for the regression.

Hunterlige commented 1 month ago

Thank you for your detailed explanation and for taking the time to explain the underlying issue.

erictraut commented 1 month ago

This is addressed in pyright 1.1.371.