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

--package flag does not work with installed packages #7087

Closed
apljungquist opened this issue Jun 27, 2019 · 10 comments
Closed

--package flag does not work with installed packages #7087

apljungquist opened this issue Jun 27, 2019 · 10 comments
Labels

Comments

@apljungquist
Copy link

apljungquist commented Jun 27, 2019

In short, I expect the below to not fail

mypy -p mypy
# Can't find package 'mypy'

The way I read the documentation and related issues such as 6394, the --package option should find named package among installed packages and check it recursively. For instance, I expect mypy to be be able to type check itself. As demonstrated this Is not the case.

The --module option on the other hand works fine:

mypy -m mypy
# no output

Explicitly setting the MYPYPATH resolves the issue:

MYPYPATH=venv/lib/python3.7/site-packages mypy -p mypy
# lots of output

However, this solution does not work inside tox where mypy will complain .tox/py37-mypypath-m/lib/python3.7/site-packages is in the MYPYPATH. Please remove it.

It would be nice to be able to check typing on the package under development as an installed package because this would ensure that the package appears as typing enabled to third parties.

I uploaded a small package that also reproduces the problem here

OS: MacOS 10.14.5
Python: 3.7.2 (via pyenv)
Mypy: 0.720+dev.17b68c6b7eaa76853422544e32b1d6c5c3acc55a

@ilevkivskyi
Copy link
Member

Hm, I would say this should work (unless I am missing something).

cc @gvanrossum @ethanhs

@gvanrossum
Copy link
Member

Unfortunately the -p option doesn't use the same code as the import-following code, and currently only the latter supports searching site-packages (etc.) for packages with a py.typed marker in them (which mypy has). It looks like the code for -m does use the import-following code (by calling BuildSource(None, m, None)).

Agreed we should fix this. If you can come up with a PR that would be awesome!

apljungquist added a commit to apljungquist/sprig that referenced this issue Mar 12, 2020
Unfortunately, to my knowledge, it is not possible to run the type checking on the
built and installed package, see github issue below for details:

python/mypy#7087
@bhrutledge
Copy link

I discovered this while working on pypa/twine#618, and thinking that it would be nice to run mypy against the installed package. See the comments for more details (including some proposed tox configurations).

Poking around a bit, it seems like this is the entry point to the import-following code for -p and -m:

mypy/mypy/main.py

Lines 900 to 917 in 22e504b

# Set target.
if special_opts.modules + special_opts.packages:
options.build_type = BuildType.MODULE
search_paths = SearchPaths((os.getcwd(),), tuple(mypy_path() + options.mypy_path), (), ())
targets = []
# TODO: use the same cache that the BuildManager will
cache = FindModuleCache(search_paths, fscache, options, special_opts.packages)
for p in special_opts.packages:
if os.sep in p or os.altsep and os.altsep in p:
fail("Package name '{}' cannot have a slash in it.".format(p),
stderr)
p_targets = cache.find_modules_recursive(p)
if not p_targets:
fail("Can't find package '{}'".format(p), stderr)
targets.extend(p_targets)
for m in special_opts.modules:
targets.append(BuildSource(None, m, None))
return targets, options

@apljungquist
Copy link
Author

This appears to be working now. Big thanks to whoever fixed it.

@fitzymj
Copy link

fitzymj commented May 1, 2021

It's not working for me! I'm on mypy-0.812.

@apljungquist
Copy link
Author

@fitzymj Curious. In what sense does it not work for you?

I set up a somewhat controlled job to sanity check myself and my conclusion is still that it works. Notice how the older version fails because it does not find the package to type check whereas the new version fails because the type check fails.

@fitzymj
Copy link

fitzymj commented May 2, 2021

Could be because I'm on Python 3.7, could be because I'm on Windows? This shows it pretty clearly:

c:\>python -V
Python 3.7.4

c:\>mypy -V
mypy 0.812

c:\>mkdir mypackage

c:\>mkdir mypackage\testpkg

c:\>echo from . import test  1>mypackage\testpkg\__init__.py

c:\>echo print("test.py imported")  1>mypackage\testpkg\test.py

c:\>echo from setuptools import setup; setup(packages=["testpkg"])  1>mypackage\setup.py

c:\>pip install -e mypackage
Obtaining file:///C:/mypackage
Installing collected packages: UNKNOWN
  Found existing installation: UNKNOWN 0.0.0
    Uninstalling UNKNOWN-0.0.0:
      Successfully uninstalled UNKNOWN-0.0.0
  Running setup.py develop for UNKNOWN
Successfully installed UNKNOWN

c:\>python -c "import testpkg"
test.py imported

c:\>mypy -p testpkg
Can't find package 'testpkg'

@apljungquist
Copy link
Author

Is py.typed included among the files installed for your package?

Look in a path like venv/lib/python3.7/site-packages/testpkg, changing venv as appropriate for your installation.

This is the relevant part of the docs.

@fitzymj
Copy link

fitzymj commented May 4, 2021

That did it, thanks! I went back to 0.782 and it did not fix it, so I feel a little better.

Ditto on the thanks to whomever fixed it in 0.812!

@apljungquist
Copy link
Author

You are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants