python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.49k stars 2.83k forks source link

<directory> is not a valid Python package name #8400

Closed jcrben closed 4 years ago

jcrben commented 4 years ago

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:

def greeting(name: str) -> str:
    return "Hello " + name

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.

ilevkivskyi commented 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.

abcdenis commented 4 years ago

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.

markfickett commented 2 years ago

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.

wangkuiyi commented 1 year ago

@markfickett I got the same problem. After renaming some-directory-in-it into some_directory_in_it, mypy worked.

BTripp1986 commented 1 year ago

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.

stevleibelt commented 6 months ago

@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.

elmago commented 5 months ago

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

caldempsey commented 5 months ago

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.

AntiSol commented 3 months ago

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()
koljapluemer commented 2 months ago

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.

sabpamdev1 commented 4 weeks ago

How to disable this rule, if anyone can help please?

AntiSol commented 4 weeks ago

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 :)

klalumiere commented 3 weeks 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.

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!