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

pytype_test.py: Handle non-typeshed requirements that are stub-only packages #10723

Merged
merged 1 commit into from
Sep 20, 2023

Conversation

AlexWaygood
Copy link
Member

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 has pandas-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

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Sep 17, 2023

Demo (with a venv activated that has pandas-stubs installed):

>>> 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']

@AlexWaygood
Copy link
Member Author

(Here's what you get with the function as it is on the main branch:)

>>> 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']

@hamdanal
Copy link
Contributor

_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!

@AlexWaygood
Copy link
Member Author

That's a lot of pandas-stubs!

🐼 * 200!

@Avasam
Copy link
Collaborator

Avasam commented Sep 20, 2023

This is what I was gonna suggest.
Since the *-stubs suffix as per PEP-561 only applies to the packages, not the distribution (case in point: typeshed's own types-*), it is indeed necessary to check the actual package name.
So this looks good to me!
(PS: Release all those pandas! 🐼)

@AlexWaygood AlexWaygood merged commit 53422f8 into python:main Sep 20, 2023
61 checks passed
@AlexWaygood AlexWaygood deleted the pytype-test-stubs-packages branch September 20, 2023 19:03
@AlexWaygood
Copy link
Member Author

@Avasam Avasam mentioned this pull request Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants