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

Use Iterator instead of Iterable for specifier filter methods #584

Merged
merged 1 commit into from Aug 17, 2022

Conversation

ichard26
Copy link
Member

Iterable is causing mypy to think next isn't supported on the return result of those methods as the Iterable protocol only requires iter. The filter builtin's return type is Iterator so it (probably) makes sense to follow that.

*fwiw, only Specifier.filter supports next as it returns a generator, SpecifierSet.filter sometimes supports next, but there are situations where it returns a list.

@@ -965,7 +965,7 @@ def filter(
if self._specs:
for spec in self._specs:
iterable = spec.filter(iterable, prereleases=bool(prereleases))
Copy link
Member

Choose a reason for hiding this comment

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

This loop looks weird. Is it essentially running reduce on itself?

prereleases = bool(prereleases)
reduce_iterable = lambda it, spec: spec.filter(it, prereleases=prereleases)
yield from functools.reduce(reduce_iterable, self._specs, iterable)

Not sure if it's really that much more readable, though. 😅

@@ -965,7 +965,7 @@ def filter(
if self._specs:
for spec in self._specs:
iterable = spec.filter(iterable, prereleases=bool(prereleases))
return iterable
return iter(iterable)
Copy link
Member

Choose a reason for hiding this comment

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

Could also do:

Suggested change
return iter(iterable)
yield from iterable

Works in the other cases as well.

@brettcannon
Copy link
Member

@pradyunsg you said you liked this idea on discuss.python.org, correct?

@pradyunsg
Copy link
Member

pradyunsg commented Aug 17, 2022

Discord, but yes. :)

Please feel welcome to land this without waiting on my review!

@brettcannon brettcannon merged commit 917612f into pypa:main Aug 17, 2022
@ichard26 ichard26 deleted the fix-filter-return-annotation branch August 17, 2022 21:35
KOLANICH pushed a commit to KOLANICH-libs/packaging that referenced this pull request Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants