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

remove unnecessary use of generators for session retrieval #2524

Merged
merged 3 commits into from
Oct 4, 2024

Conversation

rchl
Copy link
Member

@rchl rchl commented Sep 30, 2024

Use of generators for retrieving the session is IMO completely unnecessary (correct me if I'm missing something).

  1. This is not an expensive operation that would benefit from being interrupted early. Getting all sessions and returning a list is not gonna be slower. It will potentially even be faster as I imagine that generators have some overhead.
  2. Passing generators around can lead to errors or at least confusion since the values can only be retrieved once from it. In cases like
    def best_session(view: sublime.View, sessions: Iterable[Session], point: int | None = None) -> Session | None:
    if point is None:
    try:
    point = view.sel()[0].b
    except IndexError:
    return None
    try:
    return max(sessions, key=lambda s: view.score_selector(point, s.config.priority_selector)) # type: ignore
    except ValueError:
    return None
    where we pass generators and disguise it as Iterable[Session], there is no way of knowing that the code will fail if trying to iterate sessions twice.

def best_session(view: sublime.View, sessions: Iterable[Session], point: int | None = None) -> Session | None:
def best_session(view: sublime.View, sessions: list[Session], point: int | None = None) -> Session | None:
Copy link
Member

Choose a reason for hiding this comment

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

I think this change for the parameter type is unnecessary here because list is still an Iterable, so this just makes the function more restrictive where it doesn't need to be. And Iterable would be more inline with the usage in max, which takes an iterable https://docs.python.org/3/library/functions.html#max

But frankly it doesn't really matter and I think nobody payed much intention of this throughout the rest of the code base.

@rchl rchl merged commit d71e11a into main Oct 4, 2024
8 checks passed
@rchl rchl deleted the fix/remove-generators branch October 4, 2024 07:59
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.

2 participants