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

Refactor magnet link copy function #8017

Merged
merged 1 commit into from
May 7, 2024
Merged

Refactor magnet link copy function #8017

merged 1 commit into from
May 7, 2024

Conversation

drew2a
Copy link
Contributor

@drew2a drew2a commented May 2, 2024

The function for copying the magnet link has been refactored. The code now is a bit safier.

Fixes #8016

@drew2a drew2a marked this pull request as ready for review May 2, 2024 11:58
@drew2a drew2a requested review from a team and kozlovsky and removed request for a team May 2, 2024 11:58
@drew2a drew2a marked this pull request as draft May 2, 2024 12:25
@drew2a drew2a marked this pull request as ready for review May 2, 2024 13:04
Copy link
Contributor

@kozlovsky kozlovsky left a comment

Choose a reason for hiding this comment

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

I have a mixed feeling about this PR.

First, it looks like the actual problem still needs to be fixed. If I understand correctly, the reason for the TypeError: 'NoneType' object is not subscriptable is that the field self.current_download is None.

At the beginning of other methods of the DownloadsDetailsTabWidget, we can see the following checks:

    def update_pages(self, new_download=False):
        if self.current_download is None:
            return
        ...

It looks to me that a similar check should be added to the beginning of the on_copy_magnet_clicked method.

Second, the resulting code after the PR refactoring does not look clearer than the original code. Originally, we had a 9-line function, and now we have two functions, 32 lines in total, that do the same, just in a more verbose form. Furthermore, now a future code developer should deal with two very similar functions, create_magnet and compose_magnetlink. It may be hard for a developer to understand, which one of these functions should be used in which case.

Can we instead add filtering valid tracker urls directly to the compose_magnetling function, completely avoiding the create_magnet static method?

@drew2a
Copy link
Contributor Author

drew2a commented May 6, 2024

Yes, it is more verbose, but that is not necessarily a bad thing. More lines of code do not always equate to worse code.

It is more verbose because:

  1. The logic was extracted into the function create_magnet, which makes it possible to properly test it. The previous version did not allow this.
  2. The line:
        trackers = [
            tk['url'] for tk in self.current_download['trackers'] if 'url' in tk and tk['url'] not in ['[DHT]', '[PeX]']
        ]

was rewritten to:

        invalid_urls = {'[DHT]', '[PeX]'}
        urls = (t.get('url') for t in trackers)
        valid_urls = [u for u in urls if u and u not in invalid_urls]

This makes it more understandable by splitting a large and complex expression into two expressions with clear purposes:

  1. Mapping the sequence
  2. Filtering the sequence

@drew2a
Copy link
Contributor Author

drew2a commented May 6, 2024

@kozlovsky, thank you for the review. I've addressed most of your comments except for the verbosity. For the explanation, please see above.

Copy link
Contributor

@kozlovsky kozlovsky left a comment

Choose a reason for hiding this comment

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

@drew2a, thank you for addressing the comments; now the code looks clear and understandable!

@drew2a drew2a merged commit 6aa904a into Tribler:main May 7, 2024
20 checks passed
@drew2a drew2a deleted the fix/8016 branch May 7, 2024 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[7.14] TypeError: 'NoneType' object is not subscriptable
2 participants