Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicate-code in Inheritance and implementation classes #4350

Closed
whg517 opened this issue Apr 14, 2021 · 6 comments
Closed

duplicate-code in Inheritance and implementation classes #4350

whg517 opened this issue Apr 14, 2021 · 6 comments
Labels
Duplicate 🐫 Duplicate of an already existing issue Question

Comments

@whg517
Copy link

whg517 commented Apr 14, 2021

question

When I inherited and implemented my superclass, PyLint detected duplication of part of the code that I defined for method parameters, and I wondered if PyLint could disable this.

What I'm saying is not to completely disallow duplicate-code, but to prevent PyLint from throwing warnings triggered by the above situation.

Specific description:

When I have a base class that contains some public methods and some abstract methods, subclasses need to implement the abstract methods. In some cases, you may also override methods already implemented by the parent class.

So my subclass will integrate the base class and then implement the methods of the parent class. In general, there is no problem.

But when I use type annotations, I annotate the types for those parameters, and I annotate the return value of the method. The method definition is too long, so I have to use it

The indentation. The part of the method definition becomes multiline. This part of the code also needs to be reused for subclass integration. (When a subclass implements or overrides a parent's method, the method's parameters can be looser than the parent's, and the return value can be stricter.)

This causes PyLint to detect that multiple lines of code in the method definition section are duplicated. This part of the code should not actually throw a warning.

Of course I also tried to put this code on one line, and running PyLint doesn't throw a warning about duplicate code, but it does throw a warning that the trip is too long.

So I wanted to know how to solve this part of the problem.

Specific cases:

base.py

"""
Base
"""
import asyncio
from asyncio import AbstractEventLoop
from typing import Optional

class BaseExecutor:
    """
    Base executor.
    """
    def __init__(
        self,
        name: str,
        pid: str,
        loop: Optional[AbstractEventLoop] = None,
    ):
        self.name = name
        self.pid = pid
        self._loop = loop

    @property
    def loop(self) -> AbstractEventLoop:
        """Current event loop"""
        if self._loop:
            return self._loop
        return asyncio.get_running_loop()

    def run(self) -> None:
        """Run executor."""
        raise NotImplementedError

executors.py

"""
Executors.
"""
from asyncio import AbstractEventLoop
from typing import Optional

from my_project.base import BaseExecutor


class DockerExecutor(BaseExecutor):
    """
    Docker Executor
    """
    def __init__(
        self,
        name: str,
        pid: str,
        loop: Optional[AbstractEventLoop] = None,
    ):
        super().__init__(name, pid, loop)

        self._client = 'Fake docker client'

    def run(self) -> None:
        """Run executor."""
        print(f'{id(self.loop)} Run executor')

Pylint output:

(my_project) ➜  my_project pylint src tests
************* Module tests.tests
tests/tests.py:1:0: R0801: Similar lines in 2 files
==src.my_project.base:11
==src.my_project.executors:13
    def __init__(
        self,
        name: str,
        pid: str,
        loop: Optional[AbstractEventLoop] = None,
    ): (duplicate-code)

------------------------------------------------------------------
Your code has been rated at 9.63/10 (previous run: 9.63/10, +0.00)

I'm also a little confused about the output. Why does tests/tests.py show that the two files are similar? I don't have any code under my tests.

@Pierre-Sassoulas
Copy link
Member

You can use:

[SIMILARITIES]
# min-similarity= (more than the default 4 ?)
ignore-imports=yes

@Pierre-Sassoulas
Copy link
Member

Feel free to reopen if that does not answer the question.

@whg517
Copy link
Author

whg517 commented Apr 14, 2021

Hi @Pierre-Sassoulas

Thank you for your reply.

This configuration is literally set to ignore imports, and it should not work in the case I mentioned above. Because the warning thrown in the above case is not related to the import code. As I suspected, I added the parameter ignore-imports = yes and the warning was not ignored.

Here is my sample repository where I put the above code and automatically run PyLint through GitHub Action. In the pipeline, I first looked at the configuration of PyLint, and I did enable it, but the warning still appears.

Here is pylint config:

https://github.com/whg517/demo/blob/45b499dfa66235d5579802be95a38cfcb2dfb767/setup.cfg#L46

@whg517
Copy link
Author

whg517 commented Apr 14, 2021

Hi @Pierre-Sassoulas . Could you please open it again? I have no authority to open the ISSUE you closed. Thanks a lot.

@Pierre-Sassoulas
Copy link
Member

Sorry, I confused import with signature so in fact this is a dupe of #3619. You can disable similarities for this file as long as ignore-signatures this is not an available option in pylint.

@Pierre-Sassoulas Pierre-Sassoulas added the Duplicate 🐫 Duplicate of an already existing issue label Apr 14, 2021
@whg517
Copy link
Author

whg517 commented Apr 14, 2021

I see, thank you, and hope the community can improve this feature as soon as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue Question
Projects
None yet
Development

No branches or pull requests

2 participants