-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Find library stubs in __pypackages__ #10633
Comments
I've tried setting MYPY_PATH to |
Is there a way to ignore such errors in the mean time? UPDATE: the following configuration seems to do the trick. [mypy-jinja2.*]
ignore_missing_imports = True |
The variable to set is But it still does not work:
It appears to work then creating a separate directory and linking
/cc @frostming |
I would love it if mypy became PEP 582 compatible. You can install things like mypy and black elsewhere (e.g. globally with pipx) so they are not in I'm not sure if this has other side effects, though. It certainly type checks everything unless you set it to skip following all imports, which seems equivalent to ignoring missing imports. But you can silence following imports to give added safety. Please feel free to comment on this approach. Best wishes! |
Using @blueyed solution, I've been able to automate that temporary directory creation with symlinks to packages of interest. It's implemented as a duty (my task runner), but could easily be rewritten for other task runners. UPDATE: also support .pth files ( @duty
def check_types(ctx):
# NOTE: the following code works around this issue:
# https://github.com/python/mypy/issues/10633
# compute packages directory path
py = f"{sys.version_info.major}.{sys.version_info.minor}"
pkgs_dir = Path("__pypackages__", py, "lib").resolve()
# build the list of available packages
packages = {}
for package in pkgs_dir.glob("*"):
if package.suffix not in {".dist-info", ".pth"} and package.name != "__pycache__":
packages[package.name] = package
# handle .pth files
for pth in pkgs_dir.glob("*.pth"):
with suppress(OSError):
for package in Path(pth.read_text().splitlines()[0]).glob("*"):
if package.suffix != ".dist-info":
packages[package.name] = package
# create a temporary directory to assign to MYPYPATH
with tempfile.TemporaryDirectory() as tmpdir:
# symlink the stubs
ignore = set()
for stubs in (path for name, path in packages.items() if name.endswith("-stubs")):
Path(tmpdir, stubs.name).symlink_to(stubs, target_is_directory=True)
# try to symlink the corresponding package
# see https://www.python.org/dev/peps/pep-0561/#stub-only-packages
pkg_name = stubs.name.replace("-stubs", "")
if pkg_name in packages:
ignore.add(pkg_name)
Path(tmpdir, pkg_name).symlink_to(packages[pkg_name], target_is_directory=True)
# create temporary mypy config to ignore stubbed packages
newconfig = Path("config", "mypy.ini").read_text()
newconfig += "\n" + "\n\n".join(f"[mypy-{pkg}.*]\nignore_errors=true" for pkg in ignore)
tmpconfig = Path(tmpdir, "mypy.ini")
tmpconfig.write_text(newconfig)
# set MYPYPATH and run mypy
os.environ["MYPYPATH"] = tmpdir
ctx.run(f"mypy --config-file {tmpconfig} {PY_SRC}", title="Type-checking", pty=PTY) There's no |
Any updates on this? It would be really helpful |
Does anyone have a workaround for modules that ship their own types? I can't figure out how to get mypy to not either throw
or to throw tons of errors for the code in the torch package itself. I've tried linking the torch package to a directory that I then set to MYPYPATH. |
I'm working on PEP582 globally and confused about the following error message after adding
maybe a side effects? |
Facing this issue with PEP582 ( |
Do you have this package installed by any chance? https://pypi.org/project/types-typing-extensions/ |
it exists at |
@iyume removing it should fix the error :) |
What if it is a sub-dependency, and it does not shadow anything? ( |
Looking at the PEP discussion on discourse, it seems that a) the actual structure of the So I don't think we can or should implement PEP 582 support. However, I think #11143 should add support for |
[off-topic]
I think you mean pre-mature. Sorry to ping everyone for this, but I like atmosphere in the Python community. Wouldn't want someone to zone out because their idea was called immature. |
Absolutely, thank you for catching that, I have edited my comment. |
This was merged in. Is it in 0.960/0.961? If so, does anyone know if it works? |
That PR was recently merged, it will likely be in 0.970. |
I'm running mypy 0.970+dev.914297e9486b141c01b34593938fdf423d892cef and it seems the issue's solved for me. |
It supports __pypackages__. See python/mypy#10633
0.971 allows mypy to look at sys.path. I don't think it's worth implementing any further support for PEP 582. PEP 582 is woefully underspecified, discussion seems stalled (draft PEP means very little), and in my opinion it's not the best approach anyway. |
Feature
It seems mypy cannot find library stubs when developing a project using PEP582, for example through PDM. Not opening as a bug but rather as a feature request because PEP582 is still quite recent and therefore expected not to be supported everywhere.
Pitch
Support running mypy in projects managed by tools like PDM that take advantage of PEP582.
The text was updated successfully, but these errors were encountered: