Python Linter to catch user shadowed function names

116 Views Asked by At

Assume the following scenario:

def my_decorator(func):
   @functools.wraps(func)
   def wrapper(*args, **kwargs):
       # Code
   return wrapper

def f():
   # function code
   return

f = my_decorator(f)

f = my_decorator(f) shadows the function f and that will lead to unexpected behavior on f() calls down the line.

Linters that I tried so far and results:

  • MyPy: Can catch the shadowing if f changes type completely (ex. f = "I am a string now") but not the shadowing in question.
  • Pylint: Same as MyPy
  • Ruff: Same as MyPy

Also all the linters mentioned are able to catch built-in variable shadowing.
So the linters are failing in the scenario of interest because f ultimately doesn't change type passing though the decorator method.

Questions:

  • Is there any tweak that I am missing for any of those linters than can possibly make them more pedantic/strict?
  • Is there any alternative that may work and I have not considered?
0

There are 0 best solutions below