Skip to content

Update pkg_resources-stubs for use in pytype_test #9747

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

Merged
merged 10 commits into from
Mar 6, 2023

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented Feb 18, 2023

Fixes a type issue in pytype_test. https://github.com/python/typeshed/blob/main/tests/pytype_test.py#L158
Unfortunately I can't immediately remove the type: ignore comment because typeshed's tests depend on types-setuptools.

@github-actions

This comment has been minimized.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@github-actions

This comment has been minimized.

@overload
def get_distribution(dist: _D) -> _D: ...
@overload
def get_distribution(dist: _PkgReqType) -> DistInfoDistribution: ...
Copy link
Member

Choose a reason for hiding this comment

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

This calls get_provider(), which according to our stub below returns a Distribution, not a DistInfoDistribution. I haven't traced the code to verify which is right, but we should be consistent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think you're right. Didn't look too far into it but might depend on if the distribution was .egg, .egg-info or .dist-info

PKG_INFO: str
PKG_INFO: ClassVar[str]
# Initialized to None, but is not meant to be instantiated directly
egg_info: str
Copy link
Member

Choose a reason for hiding this comment

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

I don't see this attribute at all (on setuptools 67.3.1).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Found it, it's in NullProvider. If you provide no metadata to Distribution, it uses EmptyProvider which is a NullProvider

>>> type(pkg_resources.get_distribution("numpy")._provider)
<class 'pkg_resources.PathMetadata'>
>>> type(Distribution()._provider)
<class 'pkg_resources.EmptyProvider'>
>>> type(get_provider("numpy"))
<class 'pkg_resources.DefaultProvider'>

PathMetadata, EmptyProvider and DefaultProvider all have NullProvider in common

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Avasam Avasam requested review from AlexWaygood and JelleZijlstra and removed request for AlexWaygood February 23, 2023 10:37

class EggMetadata(ZipProvider, IResourceProvider):
loader: types._LoaderProtocol
Copy link
Member

Choose a reason for hiding this comment

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

This comes from the zipimporter argument to __init__ so the types should match.

egg_name: str | None
egg_info: str | None
loader: types._LoaderProtocol | None
module_path: str | None
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
module_path: str | None
module_path: str

The initializer unconditionally sets it to a string.

Copy link
Collaborator Author

@Avasam Avasam Feb 25, 2023

Choose a reason for hiding this comment

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

EmptyProvider sets it explicitely to None and overrides __init__

Should I keep module_path: str | None for sublcassing. Or add # type: ignore[assignment] to EmptyProvider.module_path ?

Especially considering this comment: #9747 (comment)

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@Avasam Avasam requested a review from JelleZijlstra February 27, 2023 16:12
@JelleZijlstra JelleZijlstra merged commit 635493a into python:main Mar 6, 2023
@Avasam Avasam deleted the pytype_test-type-error branch March 6, 2023 00:26
Avasam added a commit to Avasam/typeshed that referenced this pull request Mar 6, 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