Skip to content

Commit

Permalink
Fix false negative for protected-access on functions (#5990)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored Mar 27, 2022
1 parent 6279ab1 commit 5c8384e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ Release date: TBA

Closes #5803

* Fix a false negative regression in 2.13.0 where ``protected-access`` was not
raised on functions.

Fixes #5989


What's New in Pylint 2.13.1?
============================
Expand Down
5 changes: 4 additions & 1 deletion pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ def _check_unused_private_attributes(self, node: nodes.ClassDef) -> None:
if attribute.attrname != assign_attr.attrname:
continue

if self._is_type_self_call(attribute.expr):
if isinstance(attribute.expr, nodes.Call):
continue

if assign_attr.expr.name in {
Expand Down Expand Up @@ -2106,6 +2106,7 @@ def _is_mandatory_method_param(self, node: nodes.NodeNG) -> bool:
"""Check if nodes.Name corresponds to first attribute variable name.
Name is `self` for method, `cls` for classmethod and `mcs` for metaclass.
Static methods return False.
"""
if self._first_attrs:
first_attr = self._first_attrs[-1]
Expand All @@ -2116,6 +2117,8 @@ def _is_mandatory_method_param(self, node: nodes.NodeNG) -> bool:
)
if closest_func is None:
return False
if not closest_func.is_bound():
return False
if not closest_func.args.args:
return False
first_attr = closest_func.args.args[0].name
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/p/protected_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ def nargs(self):
class Application(metaclass=MC):
def __no_special__(cls):
nargs = obj._nargs # [protected-access]


class Light:
@property
def _light_internal(self) -> None:
return None

@staticmethod
def func(light) -> None:
print(light._light_internal) # [protected-access]


def func(light: Light) -> None:
print(light._light_internal) # [protected-access]
2 changes: 2 additions & 0 deletions tests/functional/p/protected_access.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
protected-access:17:0:17:9::Access to a protected member _teta of a client class:UNDEFINED
protected-access:29:16:29:26:Application.__no_special__:Access to a protected member _nargs of a client class:UNDEFINED
protected-access:39:14:39:35:Light.func:Access to a protected member _light_internal of a client class:UNDEFINED
protected-access:43:10:43:31:func:Access to a protected member _light_internal of a client class:UNDEFINED

0 comments on commit 5c8384e

Please sign in to comment.