-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
pytype_test.py: Handle non-typeshed requirements that are stub-only packages #10723
pytype_test.py: Handle non-typeshed requirements that are stub-only packages #10723
Conversation
Demo (with a venv activated that has >>> import importlib.metadata
>>> import inspect
>>> def _get_pkgs_associated_with_requirement(req_name: str) -> list[str]:
... dist = importlib.metadata.distribution(req_name)
... toplevel_txt_contents = dist.read_text("top_level.txt")
... if toplevel_txt_contents is None:
... if dist.files is None:
... raise RuntimeError("Can't read find the packages associated with requirement {req_name!r}")
... maybe_modules = [f.parts[0] if len(f.parts) > 1 else inspect.getmodulename(f) for f in dist.files]
... packages = [name for name in maybe_modules if name is not None and "." not in name]
... else:
... packages = toplevel_txt_contents.split()
... # https://peps.python.org/pep-0561/#stub-only-packages
... return sorted({package.removesuffix("-stubs") for package in packages})
...
>>> _get_pkgs_associated_with_requirement('pandas-stubs')
['pandas'] |
(Here's what you get with the function as it is on the >>> import inspect
>>> import importlib.metadata
>>> def _get_pkgs_associated_with_requirement(req_name: str) -> list[str]:
... dist = importlib.metadata.distribution(req_name)
... toplevel_txt_contents = dist.read_text("top_level.txt")
... if toplevel_txt_contents is not None:
... return toplevel_txt_contents.split()
... if dist.files is None:
... raise RuntimeError("Can't read find the packages associated with requirement {req_name!r}")
... maybe_modules = [f.parts[0] if len(f.parts) > 1 else inspect.getmodulename(f) for f in dist.files]
... return [name for name in maybe_modules if name is not None and "." not in name]
...
>>> _get_pkgs_associated_with_requirement('pandas-stubs')
['pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs', 'pandas-stubs'] |
That's a lot of pandas-stubs! |
|
This is what I was gonna suggest. |
From my reading of https://peps.python.org/pep-0561/#stub-only-packages, I think this is all the normalisation we need to do here in order to get
pytype_test.py
passing when a typeshed stubs package haspandas-stubs
as a dependency. It's a somewhat more principled solution than the special-casing @hamdanal currently has in https://github.com/python/typeshed/pull/10721/files#diff-b08f977b5767c8fef257d9641aa0e00aa67abbc5fac2ad25824880c7a50995c4R153-R154 :D