Skip to content

Possible truthy-bool false positive with := #15685

@sobolevn

Description

@sobolevn

Consider this code with truthy-bool:

class A:
    def method(self) -> bool:
        return True  # whatever


if (a := A()) and a.method():
    print(a)

It will output:

» mypy ex.py --enable-error-code=truthy-bool
ex.py:6: error: "ex.a" has type "A" which does not implement __bool__ or __len__ so it could always be true in boolean context  [truthy-bool]
ex.py:6: error: "ex.A" returns "A" which does not implement __bool__ or __len__ so it could always be true in boolean context  [truthy-bool]

Two questions:

  1. Why two errors?
  2. Maybe we should skip this rule completely for (:=) and ..., because a is not really the main thing we check here?

CC @ikonst

Metadata

Metadata

Assignees

Labels

bugmypy got something wrongtopic-pep-572PEP 572 (walrus operator)

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions