flake8-simplify icon indicating copy to clipboard operation
flake8-simplify copied to clipboard

[New Rule] unnecessary argument defaults

Open danieleades opened this issue 1 year ago • 0 comments

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-

  • a non-public function has many arguments with default values, however it is never called without fully-specifying the default values anywhere
  • functions that repeatedly delegate to lower-level functions, but repeat the default arguments redundantly at every level in the hierarchy

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):
    ...

danieleades avatar Dec 31 '22 15:12 danieleades