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

Relax VenvInspectInformation attribute typing #1087

Merged
merged 5 commits into from
Oct 20, 2023

Conversation

aanghelidi
Copy link
Contributor

Description

This PR aims to remove the explicit call to list() made in venv_inspect.py by using the type that importlib.metadata.distributions() returns (an Iterator from itertools.chain.from_iterable, see) from wrapping Distribution.discover(**kwargs).

However, the code logic would be flawed if we used an Iterator (in this case skipping the explicit list() call) because we could only iterate on it once and need to do it multiple times in _dfs_package_apps to retrieve the dependencies for a given package correctly.

Therefore, an Iterable was used as a more generic type hint for more flexibility, and tuple was used instead of list since we are not modifying VenvInspectInformation.distributions.

  • I have added an entry to docs/changelog.md

Summary of changes

  • Import Iterable from typing
  • Replace all the corresponding type hints
  • Swap the explicit list() call to a tuple() call

Test plan

Tested by running

nox -s lint
nox -s tests-3.11

Resolves: #629

Use Iterable as a more generic type hint.

* Import Iterable from typing
* Replace all the corresponding type hints
* Swap the explicit list() call to a tuple() call since we are not
  modifying the `VenvInspectInformation.distributions`

Resolves: pypa#629
@aanghelidi aanghelidi changed the title enh: use Iterable in VenvInspectInformation use Iterable in VenvInspectInformation Oct 15, 2023
@aanghelidi aanghelidi changed the title use Iterable in VenvInspectInformation Use Iterable in VenvInspectInformation Oct 15, 2023
Copy link
Member

@dukecat0 dukecat0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your description, and it looks good to me generally. 👍

CHANGELOG.md Outdated Show resolved Hide resolved
@uranusjr
Copy link
Member

I would prefer Collection here, since Iterable sort of hints you are only expected to interate through this at most once (which is not the case).

@aanghelidi
Copy link
Contributor Author

Thank you for your feedback @uranusjr 👍 The last commit reflects this change.

@uranusjr uranusjr changed the title Use Iterable in VenvInspectInformation Relax VenvInspectInformation attribute typing Oct 18, 2023
@dukecat0
Copy link
Member

Looks like some tests are failed with this error: TypeError: 'ABCMeta' object is not subscriptable

@aanghelidi
Copy link
Contributor Author

As described here collections.abc.Collection can only be subscripted since 3.9, hence this error. The last commit should fix this issue. Let me know what you think.

@uranusjr
Copy link
Member

Doing a sys.version_info check just for this seems a bit redundant. I’d just use the typing entry for now, or add a from __future__ import annotations to make the annotation not resolve at runtime instead.

@aanghelidi
Copy link
Contributor Author

I added typing.Collection as I'm less familiar with from __future__ import annotations.

@dukecat0 dukecat0 merged commit 1dfd295 into pypa:main Oct 20, 2023
11 checks passed
@dukecat0
Copy link
Member

Thanks!

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.

Change VenvInspectInformation.distributions to be type Iterable[metadata.Distribution]
3 participants