Closed jcrben closed 4 years ago
__init__.py
files exist in Python for a reason: this means the directory represents a package (so that one can import it). Since import nested-2
is obviously a syntax error, package names must be valid Python identifiers.
TBH, I don't see what is not clear in this error message, but if you will make a PR with alternative wording we might consider it.
Hi, what I did:
Now I have to investigate:
It will be nice to print invalid package full name/directory. Or print option name to skip such checking.
Thank you.
I'm getting a similar error where my directory structure is /path/with/some-directory-in-it/pkgdir/file.py
. When I cd to pkgdir
and run mypy file.py
I get an error saying some-directory-in-it
is not a valid Python package. There is a some-directory-in-it/__init__.py
, but I can use other Python tools, like pip install -e .
(there's a setup.py
that specifies a valid Python package name) and python file.py
just fine, so it's confusing that mypy
does not work in this context.
@markfickett I got the same problem. After renaming some-directory-in-it
into some_directory_in_it
, mypy
worked.
I am getting this error for a parent directory which is not coupled with the actual project.
/home/me/my-code/python/my_project_with_mypy/main.py
The my_project_with_mypy
directory is a git repository, and therefore independent of the parent directory.
I could move the project into another directory hierarchy, but then what if someone clones my repo into a directory with dashes?
This definitely seems like an error, but I am new to MyPy so I could just be missing some kind of configuration which will work around this.
@BTripp1986 do you get any response if this is indeed an issue or not?
I ran into the same problem. Checking out a git repository with the name foo-bar
.
It feels so wrong that this is an issue since I execute mypy inside the repository.
I had the same problem, but I managed to make progress by directly pointing to the Python package I needed using the --package option like: mypy -p src
Running into this. I think this is a silly restriction. I'd like to use mypy in heterogenous codebases. Not everything has to adhere perfectly to Python standards. Python can also be a scripting language, and so PEP8 does not have to be a first class citizen on every occasion.
I just ran into this in a situation where I am using a submodule
core-project:
|- .git
|- pyproject.toml
|- __init__.py
|- module.py
client-project:
|- .git
|- README
|- pyproject.toml
|- src
| |- core_project -> git@host:core-project.git
| |- main.py
main.py:
from core_project import module
module.do_stuff()
mypy .
in core-project), I get this errorclient-project/src/core_project/
, but git doesn't really offer an option to do a "partial submodule" that only includes a single directory from another repo. core-project's root does not have a subdirectory like src
, because then you'd have to import core_project.src.module
all over the place, which is ugly and unnecessary. This could be alleviated somewhat by manually importing src.module
in an init.py at the root level, but that would still cause the same issue with mypy, and also means adding every module that you want to import there.core_project
would violate policy. Note that it is named core_project
when added as a submodule, because as mypy points out core-project is not a valid package name.Kindly, why is this issue closed? This makes mypy
effectively unusable within projects that have a naming standard that happens to not be PEP8, for very little reason. A development library forcing naming standards of wrapper directories is an absurd overreach for extremely little reason.
How to disable this rule, if anyone can help please?
Frankly your best option is to just disable mypy completely, that's we ended up doing at my company. It's incredibly slow and it's output is wrong most of the time anyway.
Plus, you'll note the lack of response on tickets like this one.
My advice is to save yourself a heap of time and needless hassle and just chuck mypy in the bin :)
init.py files exist in Python for a reason: this means the directory represents a package (so that one can import it). Since import nested-2 is obviously a syntax error, package names must be valid Python identifiers.
TBH, I don't see what is not clear in this error message, but if you will make a PR with alternative wording we might consider it.
Well, your explanation is actually perfect: thanks for this! 🙂 So for the ~error~ help message, I suggest something based on your explanation:
<invalid-name> is not a valid Python package name. Hint: a parent or child directory might contain a ' __init__.py' file when it shouldn't.
Low cost to change, and it's going to help hundreds of people. 😎 Thanks!
Note that this message appears for a couple other reported issues, but it's not clear that they are the same: https://github.com/python/mypy/issues/8229 and https://github.com/python/mypy/issues/6003
Please provide more information to help us understand the issue:
Bug
Code is this in test1.py:
Directory layout is more critical: test -> nested -> nested-2 -> test1.py
Next to test1.py is
__init__.py
- other directories are empty.When I run
~/.local/pyenv/versions/3.7.2/bin/python -m mypy /Users/bencreasy/test/nested/nested-2/test1.py
I get:nested-2 is not a valid Python package name
If I remove
__init__.py
, the message goes away.Don't see why it can't run the file, but if it cannot, a better error message.
Python: Python 3.7.2 mypy: mypy 0.761
Yes, after running
pip install git+git://github.com/python/mypy.git@a07dbd00638c35729e30dfc4ae441a8985183563
- same message.None.