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

Initial attempt to create base GUI classes #113

Merged
merged 14 commits into from
Nov 27, 2024
Merged

Conversation

dalthviz
Copy link
Member

@dalthviz dalthviz commented Nov 7, 2024

Description

Create a set of base GUI classes so the logic implement can be reused while also keeping an implementation for napari

Notes

  • Although more work needs to be done here (somehow improve coverage/add test to the base classes for example). Marking as ready to review to get some feedback on the approach used here. Let me know what you think @jaimergp !

@dalthviz dalthviz self-assigned this Nov 7, 2024
Copy link

codecov bot commented Nov 7, 2024

Codecov Report

Attention: Patch coverage is 93.63538% with 59 lines in your changes missing coverage. Please review.

Project coverage is 93.41%. Comparing base (07702d6) to head (7d66c11).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
napari_plugin_manager/base_qt_plugin_dialog.py 93.64% 56 Missing ⚠️
napari_plugin_manager/qt_plugin_dialog.py 93.33% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #113      +/-   ##
==========================================
- Coverage   93.48%   93.41%   -0.07%     
==========================================
  Files          10       11       +1     
  Lines        1856     1943      +87     
==========================================
+ Hits         1735     1815      +80     
- Misses        121      128       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dalthviz dalthviz marked this pull request as ready for review November 13, 2024 18:34
PYPI = 'PyPI'


class BasePackageMetadata(Protocol):
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this just for the types? Should it be guarded behind TYPE_CHECKING? Not saying that's the best practice, I honestly don't know, but it was weird to see the same schema in this protocol class and also in the named tuple below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Checking, I think this is needed as it is so it can be used for the Qt Slot decorator (putting this definition under a TYPE_CHECKING guard raises an error - NameError: name 'BasePackageMetadata' is not defined when using Slot). However, if there is a better way to handle this let me know

Copy link
Contributor

Choose a reason for hiding this comment

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

If that's the case let's add a comment about in the docstring, thanks! I'm not aware of any other alternatives.

self.install_info_button.addWidget(self.info_choice_wdg)
self.info_choice_wdg.show()

def _handle_plugin_api_version(self, plugin_api_version):
Copy link
Contributor

Choose a reason for hiding this comment

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

As a rule of thumb, if the base class method is raising NotImplementedError, I think we should explain what's the expected behavior of this method with a docstring.

Copy link
Contributor

Choose a reason for hiding this comment

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

Docstring still missing. What should a subclass implement here?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is kind of like a hook to customize things for a plugin item just before it finishes being setup (the call for this method is one of the last things done before exiting the class constructor). On the napari implementation, this checks the plugin API and depending on that sets the icon and text that goes besides the plugin name

Comment on lines 39 to 40
CONDA = 'Conda'
PYPI = 'PyPI'
Copy link
Contributor

Choose a reason for hiding this comment

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

These two globals are also available in the base module. Shall we delete here?

@jaimergp
Copy link
Contributor

I like it a lot @dalthviz. Just added some comments about documenting the pieces that are meant to be subclassed. The base class docstring should detail how to get to a functional plugin manager in the subclass; e.g. "make sure to subclass all the methods that raise NotImplementedError, details are available in each method docstring".

@dalthviz dalthviz requested a review from jaimergp November 19, 2024 18:10
@dalthviz
Copy link
Member Author

I think this is ready for a new review @jaimergp Let me know what you think!

@jaimergp jaimergp requested a review from goanpeca November 20, 2024 16:23
@jaimergp jaimergp linked an issue Nov 20, 2024 that may be closed by this pull request
"""License information of the package being represented."""


class _BasePackageMetadata(NamedTuple):
Copy link
Contributor

Choose a reason for hiding this comment

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

Add docstring here about intent of this NamedTuple, why it is private and how it relates to the protocol above. I wonder: can we use a dataclass for both things and have a single object?

Copy link
Member Author

@dalthviz dalthviz Nov 22, 2024

Choose a reason for hiding this comment

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

The main reason why I used the Protocol class + Actual class implementation is that when I tried to subclass the basic class implementation while also subclassing npe2.PackageMetadata errors appeared. Will check if using dataclass instead of inheriting from NamedTuple could help with this.

In the meantime, I think a better selection of names can be done. So something like PackageMetadataProtocol and BasePackageMetadata as well as some info over the doctring as you mention 👍

Edit: Tried using dataclass for the base class but was unable to make it work (subclassing the base class along side npe2.PackageMetadata) :/

"""
Movie to use to indicate something is loading.

This should return an instance of `QMovie` with a scaled size fo 18x18
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add the type to the signature?

----------
info : str
Info message to be shown.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are these extra blank lines by choice, or enforced by the formatter? If not enforced, I think we should remove them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will remove them 👍

"""
raise NotImplementedError

def _trans(self, text, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it return str?

Copy link
Member Author

@dalthviz dalthviz Nov 22, 2024

Choose a reason for hiding this comment

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

Yep, or at least a subclass of str so I guess this could be set to describe that it returns something like TypeVar('T', bound=str)? I think that should correspond with what napari trans._ returns (Union[TranslationString, str]), right? 🤔

Edit: Actually, I think using AnyStr should be enough? For the moment I leave str but let me know!

Copy link
Contributor

@jaimergp jaimergp left a comment

Choose a reason for hiding this comment

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

Another round of comments. Let's focus on making sure that the user has the necessary information to subclass the base class. Meaning that all methods meant to be subclassed have a docstring and a properly type-annotated function signature, included return types, if necessary. I think we'll be ready to merge after that <3. Thanks!

"""
Protocol class defining the minimum atributtes/properties needed for package metadata.

This class is meant for type checking propuses as well as to provide a type to use with
Copy link
Contributor

Choose a reason for hiding this comment

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

propuses -> purposes

@goanpeca
Copy link
Contributor

goanpeca commented Nov 26, 2024

Thanks for working on this, looks good, found a typo.

Should we wait for #51 to be merged (looks good to go BTW!) , so @dalthviz can update this one @jaimergp ?

@jaimergp
Copy link
Contributor

Gonzalo and I were talking that we should probably wait for #51 to land first, and then merge this one, WDYT?

@dalthviz
Copy link
Member Author

we should probably wait for #51 to land first

That makes sense 👍

@jaimergp
Copy link
Contributor

#51 is in @dalthviz. Let's fix the conflicts and we can merge here too!

@jaimergp jaimergp merged commit cccd4d1 into napari:main Nov 27, 2024
12 checks passed
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.

Audit napari utilities imports on this package
3 participants