Skip to content

Commit

Permalink
Fix false positive for arguments-differ when overriding `__init_s…
Browse files Browse the repository at this point in the history
…ubclass__`.

Closes #8919
  • Loading branch information
mbyrnepr2 committed Aug 5, 2023
1 parent 64cc5c2 commit bab248d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8919.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix false positive for ``arguments-differ`` when overriding `__init_subclass__`.

Closes #8919
16 changes: 8 additions & 8 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,6 @@ def _different_parameters(
if different_kwonly:
output_messages += different_kwonly

if original.name in PYMETHODS:
# Ignore the difference for special methods. If the parameter
# numbers are different, then that is going to be caught by
# unexpected-special-method-signature.
# If the names are different, it doesn't matter, since they can't
# be used as keyword arguments anyway.
output_messages.clear()

# Arguments will only violate LSP if there are variadics in the original
# that are then removed from the overridden
kwarg_lost = original.args.kwarg and not overridden.args.kwarg
Expand All @@ -386,6 +378,14 @@ def _different_parameters(
if kwarg_lost or vararg_lost:
output_messages += ["Variadics removed in"]

if original.name in PYMETHODS:
# Ignore the difference for special methods. If the parameter
# numbers are different, then that is going to be caught by
# unexpected-special-method-signature.
# If the names are different, it doesn't matter, since they can't
# be used as keyword arguments anyway.
output_messages.clear()

return output_messages


Expand Down
10 changes: 10 additions & 0 deletions tests/functional/a/arguments_differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,13 @@ def method(self, *, arg1):
class ClassWithNewNonDefaultKeywordOnly(AClass):
def method(self, *, arg2, arg1=None): # [arguments-differ]
...


# Exclude `__init_subclass__` from the check:
class InitSubclassParent:
def __init_subclass__(cls, *args, **kwargs):
...

class InitSubclassChild(InitSubclassParent):
def __init_subclass__(cls, /, **kwargs: Any) -> None:
super().__init_subclass__(**kwargs)

0 comments on commit bab248d

Please sign in to comment.