Pylint: Impossible to disable R0401 with comments.

Created on 27 Aug 2013  路  5Comments  路  Source: PyCQA/pylint

Originally reported by: Anonymous


In two files with a circular import, putting R0401 anywhere in the file - at the top, after the line of code in question, etc - would not disable the output. It had to be added to the pylintrc global disable.


bug

Most helpful comment

I've started implementing it. Since self.import_graph is used to generate dependencies graph, it'll be kept untouched. Additional private variable will be added, self._ignored_edges where subgraph of ignored edges will be kept. At the end, cycles will be searched in graph where _ignored_edges was removed from original one.

All 5 comments

_Original comment by_ Sylvain Th茅nault (BitBucket: sthenault, GitHub: @sthenault?):


indeed. This is because while analysing the code, pylint blindly update an import graph, then compute cycle at the end of the analyze if R0401 is not disabled globally.

What we should do is to omit addition to the import graph of import nodes where R0401 is locally disabled.

_Original comment by_ BitBucket: NeilGirdhar, GitHub: @NeilGirdhar?:


@sthenault Actually, it would be better to omit edges rather than nodes: Specifically, imports in a scope marked by the disable command should not create an edge in the graph.

I've started implementing it. Since self.import_graph is used to generate dependencies graph, it'll be kept untouched. Additional private variable will be added, self._ignored_edges where subgraph of ignored edges will be kept. At the end, cycles will be searched in graph where _ignored_edges was removed from original one.

I'm dealing with this today in 1.6.5. I have a file called sample_package/__init__.py and containing

import abc


class Interface(object):

    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def method(self):
        raise NotImplementedError()


def interface(parameter):
    from sample_package import _implementation  # pylint: disable=cyclic-import
    return _implementation.InterfaceImplementation(parameter)

and a file called sample_package/_implementation.py and containing

import sample_package


class InterfaceImplementation(sample_package.Interface):

    def __init__(self, parameter):
        self._field = parameter

    def method(self):
        return self._field

and am seeing python -m pylint -rn sample_package/ emit

No config file found, using default configuration
************* Module sample_package
I: 14, 0: Locally disabling cyclic-import (R0401) (locally-disabled)
C:  1, 0: Missing module docstring (missing-docstring)
C:  4, 0: Missing class docstring (missing-docstring)
C:  9, 4: Missing method docstring (missing-docstring)
R:  4, 0: Too few public methods (1/2) (too-few-public-methods)
C: 13, 0: Missing function docstring (missing-docstring)
************* Module sample_package._implementation
C:  1, 0: Missing module docstring (missing-docstring)
C:  4, 0: Missing class docstring (missing-docstring)
R:  4, 0: Too few public methods (1/2) (too-few-public-methods)
R:  1, 0: Cyclic import (sample_package -> sample_package._implementation) (cyclic-import)

. Can you confirm that the cyclic-import messages would not be emitted if I were using a pylint built from master and that all I really need to do to solve my problem is wait for a 1.7-or-later release?

Sure, see 7df8caa for implementation and tests. Feel free to check it yourself. I usually do it by activating tox virtualenv and running pylint from there.

Was this page helpful?
0 / 5 - 0 ratings