Skip to content

Fix Remove lambdas when updating menus from context in Window#6740

Merged
DragaDoncila merged 6 commits intonapari:mainfrom
lucyleeow:qmodelmenu
Apr 16, 2024
Merged

Fix Remove lambdas when updating menus from context in Window#6740
DragaDoncila merged 6 commits intonapari:mainfrom
lucyleeow:qmodelmenu

Conversation

@lucyleeow
Copy link
Copy Markdown
Contributor

@lucyleeow lucyleeow commented Mar 13, 2024

References and relevant issues

Follow on from #4991

Description

Removes lambdas when updating from context during add_menu _qt_main_window.py.

Each menu function just gets layerlist and does not use a helper function anymore. Prevents any use of strings now. Not 100% this is better. There is a lot of repetition but there was previously as well. Open to change.

@lucyleeow
Copy link
Copy Markdown
Contributor Author

ping @Czaki @DragaDoncila

@github-actions github-actions Bot added the qt Relates to qt label Mar 13, 2024
@lucyleeow lucyleeow added this to the 0.5.0 milestone Mar 13, 2024
@lucyleeow lucyleeow added the maintenance PR with maintance changes, label Mar 13, 2024
@github-actions github-actions Bot added the tests Something related to our tests label Mar 13, 2024
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 13, 2024

Codecov Report

Attention: Patch coverage is 87.50000% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 92.34%. Comparing base (3cb47b5) to head (f55f745).
Report is 28 commits behind head on main.

Files Patch % Lines
napari/_qt/qt_main_window.py 80.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6740      +/-   ##
==========================================
- Coverage   92.39%   92.34%   -0.06%     
==========================================
  Files         615      615              
  Lines       54854    54861       +7     
==========================================
- Hits        50684    50660      -24     
- Misses       4170     4201      +31     

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

@DragaDoncila
Copy link
Copy Markdown
Contributor

Hmm yeah @lucyleeow I can see what you mean about the repetition, and I don't love it. I don't personally see much issue with using the strings... even if we make them constants at the top of the file or something. Could we instead use partials to parameterize the update method with the right menu name before connecting it?

@lucyleeow
Copy link
Copy Markdown
Contributor Author

Yeah I think that is what we wanted originally and I am happy to change to that!

@DragaDoncila
Copy link
Copy Markdown
Contributor

not sure if @Czaki has a preference

Comment on lines -816 to -814
def _update_plugin_menu_state(self):
self._update_menu_state('plugins_menu')
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually previously we were defining update function, not using partial. Did we think partial was problematic?

I think @Czaki said string is fine as long as we type via Literal.

So partial for each mene or defining a method for each update function? @Czaki ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ping @Czaki 😬

Copy link
Copy Markdown
Collaborator

@Czaki Czaki Apr 2, 2024

Choose a reason for hiding this comment

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

I think @Czaki said string is fine as long as we type via Literal.

Yes. Because most often we use in codebase:

def fun(label: str):
    if label == "aa":
        # something
    elif label == "ab":
        # something
    elif label == "ac":
        # something
    else:
        # something

When allowed values are "aa", "ab", "ac", "ad". But the above code will also work for "ae". There will be no error for such typo. Using literal allow to detect such error using mypy at least for part of cases (also IDE my highlight).

Another option is to use Enum to enforce consistency. StrEnum may allow having runtime exception for inproper values.

So partial for each mene or defining a method for each update function? @Czaki ?

Separate method. Partial connected to signal will keep hard reference to the object and keep it alive when it should be deleted.

@lucyleeow
Copy link
Copy Markdown
Contributor Author

@DragaDoncila @Czaki

Okay I have used Literal and each is their own method (and avoided the previous repetition, though still a lot of repetition).

Let me know if this is okay with you, thanks!

Copy link
Copy Markdown
Contributor

@DragaDoncila DragaDoncila left a comment

Choose a reason for hiding this comment

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

LGTM thanks @lucyleeow

@lucyleeow
Copy link
Copy Markdown
Contributor Author

ping @Czaki 😬

Copy link
Copy Markdown
Collaborator

@Czaki Czaki left a comment

Choose a reason for hiding this comment

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

looks ok

@lucyleeow lucyleeow added the ready to merge Last chance for comments! Will be merged in ~24h label Apr 12, 2024
@DragaDoncila DragaDoncila merged commit c5846e8 into napari:main Apr 16, 2024
@DragaDoncila DragaDoncila removed the ready to merge Last chance for comments! Will be merged in ~24h label Apr 16, 2024
@lucyleeow lucyleeow deleted the qmodelmenu branch April 16, 2024 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance PR with maintance changes, qt Relates to qt tests Something related to our tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants