PyCQA/flake8-bugbear

Couple new rule suggestions

Offen

#440 geöffnet am 17.12.2023

 (2 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Python (116 Forks)github user discovery
enhancementhelp wanted

Repository-Metriken

Stars
 (1.116 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 4h 17m) (3 gemergte PRs in 30 T)

Beschreibung

I had a few thoughts for some more linter checks I'd like to have. Would be happy to implement them here if you guys are interested.

1. return <value> or yield <?from> <?value> in __init__()

Example:

class Example:
    def __init__():
        return 1  # bad
        yield  # bad
        yield 1  # bad
        yield from ()  # bad
        return  # ok

Any returning of values, or yielding, in an __init__() would result in a runtime error, but not until you try to instantiate an object of the class. So I think this should be a reasonably safe on-by-default error.

2. assert <constant or fixed value>

Normal asserts:

assert condition
assert condition, "message"

There's already a flake8 check for asserting on a tuple specifically - e.g. accidentally adding parens around (condition, message). And there's a bugbear check for assert False warning that it's disabled by -O. But forgetting the condition (assert "message") or asserting on any other literal value or list/tuple will either always pass or always fail - and is probably unintentional?

3. Inconsistent returns in function

def func(x) -> int | None:
    if x:
        return 0
    # should return None here

This one's more opinionated, but if a function returns something in some case, it should explicitly return something (None) in all other cases.

Contributor Guide