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

Running mypy on the current directory without some OS-specific stuff #9677

Closed
Holt59 opened this issue Oct 31, 2020 · 1 comment · Fixed by #9742
Closed

Running mypy on the current directory without some OS-specific stuff #9677

Holt59 opened this issue Oct 31, 2020 · 1 comment · Fixed by #9742
Labels

Comments

@Holt59
Copy link

Holt59 commented Oct 31, 2020

Feature

Have a way to pass the current directory to mypy. I can run mypy dir, but mypy . fails because . is not a valid Python package name.

Pitch

Would be very useful to have generic CI commands. I'm currently figuring out how I can pass the full path to the current directory in an OS-dependent manner. Other tools (flake8, black, ...) have no issue with "."

@Holt59 Holt59 added the feature label Oct 31, 2020
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 31, 2020

Note that mypy . does work, it just usually needs to be run in the folder above your package (or maybe you have a stray __init__.py). That said, I do agree that mypy could be more forgiving here; see my comment at #8726 (comment) for more

hauntsaninja added a commit that referenced this issue Dec 12, 2020
This is the successor to #9632. Things should basically be as discussed in that PR. Since #9616 is merged, this should now resolve #5759.

We leave the Bazel integration with `--package-root` almost entirely untouched, save for a) one change that's a bugfix / doesn't affect the core of what `--package-root` is doing, b) another drive by bugfix that's not related to this PR.
Change a) fixes the package root `__init__.py` hackery when passed absolute paths. Change b) fixes the validation logic for package roots above the current directory; it was broken if you passed `..` as a package root

Since we're leaving `--package-root` alone for now, I named the new flag `--explicit-package-base` to try and avoid confusion. Doing so also matches the language used by BuildSource a little better.

The new logic is summarised in the docstring of `SourceFinder.crawl_up`.

Some commentary:
- I change `find_sources_in_dir ` to call `crawl_up` directly to construct the BuildSource. This helps codify the fact that files discovered will use the same module names as if you passed them directly.
- Doing so keeps things DRY with the more complicated logic and means, for instance, that we now do more sensible things in some cases when we recursively explore directories that have invalid package names.
- Speaking of invalid package names, if we encounter a directory name with an invalid package name, we stop crawling. This is necessary because with namespace packages there's no guarantee that what we're crawling was meant to be a Python package. I add back in a check in the presence of `__init__.py` to preserve current unit tests where we raise InvalidSourceList.
- The changes to modulefinder are purely cosmetic and can be ignored (there's some similar logic between the two files and this just makes sure they mirror each other closely)
- One notable change is we now always use absolute paths to crawl. This makes the behaviour more predictable and addresses a common complaint: fixes #9677, fixes #8726 and others.
- I figured this could use more extensive testing than a couple slow cmdline tests. Hopefully this test setup also helps clarify the behaviour :-)

Co-authored-by: hauntsaninja <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants