Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Optional "no truth value testing" mode #1730

Closed
@tigrfhluerh

Description

@tigrfhluerh

Feature request:
Make it possible to tell mypy to treat the following as errors:

if None:
    pass
if 1:
    pass
if [1, 2, 3]:
    pass
if "asdf":
    pass

class A:
    def __bool__(self):
        return True
a = A()
if a:
    pass

(This list is based on the "Truth Value Testing" section in https://docs.python.org/3/library/stdtypes.html)

The goal is to force the developer to use explicit coercions like:

if x is not None:
    pass
if x != 0:
    pass
if len(x) > 0:
    pass
if bool(x):
    pass

Of course libraries and stubs should not be affected by this restriction.

The rationale is that while truth value testing is a valuable shortcut for many people, it also causes bugs, so some projects might want to force their developers to use explicit coercions.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions