frequenz-floss / frequenz-repo-config-python

Frequenz repository setup tools and common configuration for Python
https://frequenz-floss.github.io/frequenz-repo-config-python/
MIT License
4 stars 7 forks source link

Lint examples in `__init__.py` files #113

Open llucax opened 1 year ago

llucax commented 1 year ago

What happened?

Examples in __init__.py files are not being linted, these files are completely skipped.

What did you expect instead?

Examples in __init__.py files should be checked.

Extra information

This happens because we are explicitly excluding those files because Sybil fails to import __init__.py files, raising an error like:

self = <sybil.document.PythonDocStringDocument object at 0x7f36e8644390>
example = <Example path=/home/luca/devel/repo-config/src/frequenz/repo/config/nox/__init__.py line=12 column=1 using <bound meth...eBlockParser.evaluate of <frequenz.repo.config.pytest.examples._CustomPythonCodeBlockParser object at 0x7f36e8db7b50>>>

    def evaluator(self, example: Example) -> Optional[str]:
        """
        Imports the document's source file as a Python module when the first
        :class:`~sybil.example.Example` from it is evaluated.
        """
>       module = import_path(Path(self.path))

.nox/pytest_min/lib/python3.11/site-packages/sybil/document.py:153: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

path = PosixPath('/home/luca/devel/repo-config/src/frequenz/repo/config/nox/__init__.py')

    def import_path(path: Path):
        container = path
        while True:
            container = container.parent
            if not (container / INIT_FILE).exists():
                break
        relative = path.relative_to(container)
        if relative.name == INIT_FILE:
            parts = tuple(relative.parts)[:-1]
        else:
            parts = tuple(relative.parts)[:-1]+(relative.stem,)
        module = '.'.join(parts)
        try:
            return importlib.import_module(module)
        except ImportError as e:
>           raise ImportError(
                f'{module!r} not importable from {path} as:\n{type(e).__name__}: {e}'
            ) from None
E           ImportError: 'config.nox' not importable from /home/luca/devel/repo-config/src/frequenz/repo/config/nox/__init__.py as:
E           ModuleNotFoundError: No module named 'config'

.nox/pytest_min/lib/python3.11/site-packages/sybil/python.py:40: ImportError

We need to investigate this and open an issue in Sybil if appropriate.

llucax commented 6 months ago

Sybil had a couple of releases since we observed this issue and disabled extracting examples from __init__.py files, maybe it would be worth trying if the latest versions fixed this.

Marenz commented 1 month ago

I did a quick test with sybil==6.1.1 and found no issues using linting in an example in an init file

Marenz commented 1 month ago

As a first experiment, I'll disable the exclusion in a few repos https://github.com/frequenz-floss/frequenz-client-dispatch-python/pull/96