Skip to content

Unable to run nbqa on notebooks present in directory without __init__.py #164

Closed
@girip11

Description

@girip11

OS: Ubuntu 18.04
nbqa version: 0.1.21

I have a notebook present in a directory which is not a python package. When i try to run the following command nbqa black <my_notebook_file>, I get a StopIteration exception traceback. I looked into the code and I was able to figure out why it is raising this exception.

def _create_blank_init_files(notebook: Path, tmpdirname: str) -> None:
    parts = notebook.resolve().relative_to(Path.cwd()).parts

    for idx in range(1, len(parts)):
        # next on directory with no __init__.py raises StopIteration exception
        init_file = next(Path(os.path.join(*parts[:idx])).glob("__init__.py"))
        if init_file is not None:
            Path(tmpdirname).joinpath(init_file).parent.mkdir(
                parents=True, exist_ok=True
            )
            Path(tmpdirname).joinpath(init_file).touch()

I fixed the code locally and ran isort, black, flake8 and mypy. This is the fix I made locally

import contextlib

def _create_blank_init_files(notebook: Path, tmpdirname: str) -> None:
    parts = notebook.resolve().relative_to(Path.cwd()).parts

    for idx in range(1, len(parts)):
        with contextlib.suppress(StopIteration):
            init_file = next(Path(os.path.join(*parts[:idx])).glob("__init__.py"))
            if init_file is not None:
                Path(tmpdirname).joinpath(init_file).parent.mkdir(parents=True, exist_ok=True)
                Path(tmpdirname).joinpath(init_file).touch()

Are you mandating the presence of __init__.py file in the directory of notebooks to run nbqa?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions