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

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

Closed
girip11 opened this issue Aug 16, 2020 · 6 comments · Fixed by #167
Closed

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

girip11 opened this issue Aug 16, 2020 · 6 comments · Fixed by #167
Labels
bug Something isn't working

Comments

@girip11
Copy link
Contributor

girip11 commented Aug 16, 2020

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?

@girip11 girip11 changed the title Unable to format notebooks present in directory without __init__.py Unable to format notebooks present in directory without __init__.py Aug 16, 2020
@girip11 girip11 changed the title Unable to format notebooks present in directory without __init__.py Unable to run nbqa on notebooks present in directory without __init__.py Aug 16, 2020
@MarcoGorelli MarcoGorelli added the bug Something isn't working label Aug 16, 2020
@MarcoGorelli
Copy link
Collaborator

Hi @girip11 ,

Massive thanks for reporting the issue and for using nbqa - it warms my heart to see someone trying it out! 🙏

This was not intentional - what I wanted to do was ensure that any init.py files would be copied across (so that mypy could continue working as normal), I did not intend for an error to be thrown. So, this is a bug.

If you have any other feedback / suggestions please do let me know!

@girip11
Copy link
Contributor Author

girip11 commented Aug 16, 2020

First of all thanks a lot for your effort to create such a useful tool. Sure. I am actively using nbqa. I will report if I face any more issues.

@MarcoGorelli
Copy link
Collaborator

That's very kind of you! If you see ways to improve this tool, then pull requests would absolutely be welcome of course.

I've pushed a patch release, if the issue reported isn't fixed please do let me know

@girip11
Copy link
Contributor Author

girip11 commented Aug 17, 2020

Thanks @MarcoGorelli . I updated to latest release (0.1.22) and I didn't face this issue.

@MarcoGorelli
Copy link
Collaborator

Hey @girip11 - I've noticed you've made some quality commits in your fork of this library. Nice! Do you want to open some pull requests? It's easier to review small and focused PRs than large ones, so if e.g. fixing a mypy type hint is ready, then it's easier if you make a PR just for that fix and keep the other fixes separate

@girip11
Copy link
Contributor Author

girip11 commented Sep 8, 2020

Hey @MarcoGorelli, I am interested in contributing to this library. I use VSCode as IDE. I was going through nbqa library dev ecosystem, setting up its dependencies in VSCode. I have made some minor changes during this process. I have my local setup ready now, I will soon be sending out smaller PRs as mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants