pylint icon indicating copy to clipboard operation
pylint copied to clipboard

Add check for unused module-level private and protected functions

Open socketpair opened this issue 4 years ago • 4 comments

test.py:

def _unused():
    pass
def __unused():
    pass
class _class():
    pass
_some_constant = 42

They should not be imported from anywhere, so if they are not used in the same module - it's definitely a bug.

This file should generate errors about unused private/protected module-level members. Just like private methods #4504

socketpair avatar Jun 03 '21 05:06 socketpair

I agree that this check would be very useful to have! Would allow developers to remove dead code that isn't referenced anywhere.

One thing to note is that the line with _some_constant = 42 currently triggers the error Constant name "_some_constant" doesn't conform to UPPER_CASE naming style (invalid-name). In this case I believe it would be better to replace the existing message with something like what @socketpair was suggesting, since the variable name starts with _, which indicates the intent of using this as a private variable. Whether we replace the existing message for private variables for a new one or not is up for debate really, but this new warning is definitely needed for private and protected functions and classes.

I would be happy to create a PR for this.

DGarciaMedina avatar Jan 02 '22 17:01 DGarciaMedina

Regarding the issue with the UPPER_CASE naming style it's another issue entirely and should be discussed elsewhere (I think the consensus is to allow both snake_case and upper case naming style for module level variable.

Regarding the unused level module private and protected function, please check what is currently being done in this related MR if you want to help :)

Pierre-Sassoulas avatar Jan 02 '22 17:01 Pierre-Sassoulas

@DGarciaMedina how do you do ?

socketpair avatar Nov 11 '22 08:11 socketpair

pylint 3.0.4
astroid 3.0.3
Python 3.12.2 (main, Feb 21 2024, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)]

Still actual

socketpair avatar Mar 21 '24 16:03 socketpair