MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
185 stars 19 forks source link

[New Rule] unnecessary argument defaults #165

Open danieleades opened 1 year ago

danieleades commented 1 year ago

Explanation

Functions that are not part of the public API which have default values for arguments, but which are never used without specifying values for those arguments, can be simplified by removing the default values.

i'm working in a large, public python codebase. I have a general feeling that this codebase has massive overuse of argument defaults in methods and I would love to lint for this. This happens in a couple of different ways-

it's the second case here that causes the biggest headaches in this particular codebase. The function signatures are very noisy. Also (in my case) the default value is almost always None, and it takes some serious archaeology to try and figure out what it means to leave out any particular arg (None is often semantically different to not None).

Example

Bad

def public_method(arg1: Optional[str] = None, arg2: str = "some string"):
    module_method(arg1, arg2)

def module_method(arg1: Optional[str] = None, arg2: str = "some string"):
    submodule_method(arg1, arg2)

def submodule_method(arg1: Optional[str] = None, arg2: str = "some string"):
    ...

Good

def public_method(arg1: Optional[str] = None, arg2: str = "some string"):
    module_method(arg1, arg2)

def module_method(arg1: Optional[str], arg2: str):
    submodule_method(arg1, arg2)

def submodule_method(arg1: Optional[str], arg2: str):
    ...