Skip to content

Possible regression when narrowing using isinstance #16462

@cmeyer

Description

@cmeyer

Bug Report

Type narrowing using isinstance with 1.7 behaves differently from 1.6 for seemingly valid code.

To Reproduce

import typing

class A:
    def write(self) -> str:
        return "A"

    # succeeds
    def eq1(self, other: typing.Any) -> bool:
        if other:
            if isinstance(other, self.__class__):
                return self.write() == other.write()
        return False

    # fails
    def eq2(self, other: typing.Any) -> bool:
        if other and isinstance(other, self.__class__):
            return self.write() == other.write()
        return False

Expected Behavior

The eq1 and eq2 should both be valid code. eq2 fails under 1.7 but succeeded under 1.6.

Actual Behavior

file.py:15: error: Returning Any from function declared to return "bool"  [no-any-return]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.7.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
# Global options:

[mypy]
ignore_missing_imports = True
follow_imports = silent
strict = True
  • Python version used:

Python 3.12 macos-arm64

Metadata

Metadata

Assignees

Labels

bugmypy got something wrong

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions