Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce conditional values are booleans #16734

Open
valentincalomme opened this issue Jan 2, 2024 · 6 comments
Open

Enforce conditional values are booleans #16734

valentincalomme opened this issue Jan 2, 2024 · 6 comments
Labels

Comments

@valentincalomme
Copy link

Feature

A rule enforcing the strict use of boolean expressions in an if statement. It should be added to the "Miscellaneous strictness flags".

strict_conditional

    TYPE:
        boolean

    DEFAULT:
        False

    Enables or disables strict conditional checks. If True, `mypy` only allows boolean expressions in conditional expressions.

Pitch

I've encountered many hard-to-trace bugs due to non-boolean expressions used in conditionals. For instance, lists, containers, objects, or some literals being checked directly.

if [1,2,3]: # list is not a boolean value, will be treated as False if list is empty or None
    ...

if "Hello world": # str is not a boolean value, will be treated as False if string is empty or None
    ...

if None: # not a boolean, will be treated as False if None
    ...
    
if 2: # not a boolean, might be treated as false if it equals 0
    ...

Very often, the developer's intention was to perform a null-check, or sometimes, an emptiness check, but this syntax yields potential edge cases where the developer did not intend the conditional to yield False.

With strict_conditional enabled, all of the above scenarios should be flagged.

The below examples naturally should always be valid as the expression yields a boolean value.

if x < 0:
    ...

if isinstance(x, int):
    ...

if True:
    ...

if [1,2,3] is None:
    ....

if len({}) == 0:
    ....

This enforces explicit coding practices and better shows the intent of the developer.

@JamesParrott
Copy link

JamesParrott commented Jan 3, 2024

It's not a bad idea to have available in the ecosystem in general. I just question if mypy (or any static type checker) is the best place to enforce programming style rules.

Don't any of the other established tools: e.g. Ruff, Black, Pylint, Pycodestyle (formerly pep8) already have a rule for this (or a similar rule in line with PEP8 / or Google's Python style guide )?

@valentincalomme
Copy link
Author

valentincalomme commented Jan 3, 2024

I've looked around and genuinely couldn't find a thing. I created a similar issue for ruff (astral-sh/ruff#9148), but they replied that it's out of the current scope as it would require type checking.

I just question if mypy (or any static type checker) is the best place to enforce programming style rules.

I would argue that mypy already does this, albeit indirectly. Many flags and options aim to reduce errors created by type mismatches, which lead to enforcing some stylistic rules (i.e., specifically marking as optional due to the strict_optional rule.)

This rule does two things essentially:

  • Make sure that null checks are explicit
  • Reduce some bugs by making some checks explicit for containers (i.e. checking for an empty list)

The second point is indeed more of a bugbear type of situation, but the first could be seen as some sort of strictness flag related to None values.

@JelleZijlstra
Copy link
Member

This isn't practical to do outside a type checker, as correctly noted on the Ruff issue. So this is indeed the correct place to request this feature.

Mypy already does a subset of what you ask for by e.g. warning when using a function in a boolean context (https://mypy.readthedocs.io/en/stable/error_code_list.html#check-that-function-isn-t-used-in-boolean-context-truthy-function). Warning for all non-booleans in a boolean context is too disruptive and too much against standard Python conventions to turn on by default or even in --strict mode, but it could be implemented as an opt-in error code.

@valentincalomme
Copy link
Author

valentincalomme commented Jan 3, 2024

Agreed, it shouldn't be enabled by default.

@hauntsaninja hauntsaninja changed the title Enforce strict boolean checks Enforce conditional values are booleans Jan 4, 2024
@hauntsaninja
Copy link
Collaborator

(renamed the issue to avoid confusion with #8363)

@valentincalomme
Copy link
Author

Oh, cool, interesting to see the discussion in that particular issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants