-
Notifications
You must be signed in to change notification settings - Fork 253
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
Conversation
@@ -965,7 +965,7 @@ def filter( | |||
if self._specs: | |||
for spec in self._specs: | |||
iterable = spec.filter(iterable, prereleases=bool(prereleases)) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also do:
return iter(iterable) | |
yield from iterable |
Works in the other cases as well.
@pradyunsg you said you liked this idea on discuss.python.org, correct? |
Discord, but yes. :) Please feel welcome to land this without waiting on my review! |
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.