Skip to content

Optional "no truth value testing" mode #1730

Closed

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.

Activity

  1. rwbarton commented on Jun 22, 2016

    @rwbarton
    Contributor

    This would be technically pretty easy to implement, perhaps you'd like to give it a shot?

    I tend to be a fan of the "conditions must be bools" style you propose, though I'm not sure how popular it is among Python programmers in general. In some cases, though, mypy could reasonably be more opinionated even by default. For example, if the programmer writes user.is_admin thinking is_admin is a property when it's actually a method returning bool, it's almost certainly a bug.

  2. tigrfhluerh commented on Jun 22, 2016

    @tigrfhluerh
    Author

    Yes, I'd like to give it a shot. I expect to have some free time in early July. Once I know roughly what changes I want to make, I will outline them here to see if they make sense. Thanks!

  3. added this to the milestone on Jun 23, 2016
  4. afrieder commented on Jan 19, 2017

    @afrieder
    Contributor

    @tigrfhluerh are you still planning on working on this? If not, I'd be happy to do it.

  5. Herst commented on Mar 20, 2017

    @Herst
    Contributor

    Sorry for hijacking this issue, but does using flag lead to good style or not?

    After all it goes against PEP 8, e.g.:

    For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
    
    Yes: if not seq:
         if seq:
    
  6. gvanrossum commented on Mar 20, 2017

    @gvanrossum
    Member
  7. gvanrossum commented on Mar 20, 2017

    @gvanrossum
    Member
  8. ddfisher commented on Mar 21, 2017

    @ddfisher
    Collaborator

    --strict is a weird beast. It's only meant to be used if you want all possible strictness checks (which apparently some people do). I really want to avoid it becoming "all recommended strictness checks", because I don't think that's generally meaningful (as it really depends on the project) and because I don't want us to have another place where we have to make editorial decisions.

  9. gvanrossum commented on Mar 21, 2017

    @gvanrossum
    Member

    Yeah, so IMO it's useless. Maybe we should get rid of it.

  10. ddfisher commented on Mar 22, 2017

    @ddfisher
    Collaborator

    We seem to have several enthusiastic users of --strict as-is, though!

  11. gvanrossum commented on Apr 18, 2017

    @gvanrossum
    Member

    We're considering killing this option, as it complicates maintenance and it's an unpythonic requirement. See discussion in #3185.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions