Skip to content

Fix some typing in napari.components#6203

Merged
melonora merged 5 commits intonapari:mainfrom
dstansby:components-typing
Sep 17, 2023
Merged

Fix some typing in napari.components#6203
melonora merged 5 commits intonapari:mainfrom
dstansby:components-typing

Conversation

@dstansby
Copy link
Copy Markdown
Contributor

@dstansby dstansby commented Sep 3, 2023

Description

Fixes some typing in napari.components.

@codecov
Copy link
Copy Markdown

codecov bot commented Sep 3, 2023

Codecov Report

Merging #6203 (ef9c0ed) into main (52eb3b6) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #6203   +/-   ##
=======================================
  Coverage   91.67%   91.68%           
=======================================
  Files         583      583           
  Lines       51203    51205    +2     
=======================================
+ Hits        46942    46947    +5     
+ Misses       4261     4258    -3     
Files Changed Coverage Δ
napari/components/_layer_slicer.py 99.02% <100.00%> (+<0.01%) ⬆️
napari/components/_viewer_key_bindings.py 96.15% <100.00%> (+0.04%) ⬆️

... and 1 file with indirect coverage changes

@dstansby dstansby marked this pull request as ready for review September 3, 2023 16:46
Copy link
Copy Markdown
Member

@andy-sweet andy-sweet left a comment

Choose a reason for hiding this comment

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

Thanks for the latest typing updates! I'm pleasantly surprised that most of _layer_slicer passed typing checks, but not surprised on the needed updates - I made an alternative suggestion there that I think expresses the intent more clearly and simply.

Otherwise, looks good!

_SliceRequest = Callable[[], _SliceResponse]


class _SliceRequest(abc.ABC, Generic[_SliceResponse]):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd prefer this be a protocol instead of a generic because this ABC isn't actually inherited anywhere. See https://github.com/dstansby/napari/pull/3 for my suggestion for doing that keeps mypy happy and also simplifies the typing here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I made this update.

Comment on lines +153 to +154
if previous_mode != (pan_zoom := selected_layer._modeclass.PAN_ZOOM): # type: ignore[attr-defined]
selected_layer.mode = pan_zoom
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: I think the walrus operator hurts readability here over the previous form because the reader has to unpack the two expressions in the condition now. I guess you need two ignores in that case though, so I don't care too much.

More generally, typing of Mode feels a bit broken and this is just one symptom. Do we have an issue to track a longer term fix for that?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree. This walrus operator is obsolete as this part of the code does not contain any heavy calculations that need to be cached.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I unpacked the walrus operator and added a comment to explain why we need the type: ignore.

We do have an issue to track the more general problem with layer modes: #6005

Copy link
Copy Markdown
Member

@andy-sweet andy-sweet left a comment

Choose a reason for hiding this comment

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

Due to @dstansby's current absence, I'm going to try to take this over the line.

@Czaki : let me know if my updates aren't clear.

@andy-sweet
Copy link
Copy Markdown
Member

Since, this is pretty small, I'll probably merge this after the weekend.

@andy-sweet andy-sweet added this to the 0.5.0 milestone Sep 15, 2023
@andy-sweet andy-sweet added maintenance PR with maintance changes, ready to merge Last chance for comments! Will be merged in ~24h labels Sep 15, 2023
@melonora melonora merged commit 7099b18 into napari:main Sep 17, 2023
@dstansby dstansby deleted the components-typing branch September 17, 2023 19:21
kne42 added a commit to kne42/napari that referenced this pull request Sep 19, 2023
* main: (26 commits)
  Fix some typing in napari.components (napari#6203)
  Use class name for object that does not have qt name (napari#6222)
  test: [Automatic] Constraints upgrades: `hypothesis`, `magicgui`, `psygnal`, `tensorstore`, `tifffile`, `tqdm`, `virtualenv` (napari#6143)
  Replace more np.all( ... = ...) with np.array_equal (napari#6213)
  remove np.all(... == ...) in test_surface.py (napari#6218)
  Ensure pandas Series is initialized with a list as data (napari#6226)
  Stop using temporary directory for store array for paint test (napari#6191)
  Bugfix: ensure thumbnail represents canvas when multiscale (napari#6200)
  cleanup np.all(... == ...) from test_points.py (napari#6217)
  [pre-commit.ci] pre-commit autoupdate (napari#6221)
  use app-model for file menu (napari#4865)
  Add tests to cover slicing behavior when changing layers and data (napari#4819)
  [pre-commit.ci] pre-commit autoupdate (napari#6128)
  Add test coverage for async slicing of labels (napari#5325)
  Add collision check when set colors for labels layer (napari#6193)
  Update "toggle ndview" text (napari#6192)
  Prevent layer controls buttons changing layout while taking screenshots with flash effect on (napari#6194)
  Fix typing in napari.utils.perf (napari#6132)
  Add GUI test coverage for changes to Labels.show_selected_label (napari#5372)
  Fix types in 'napari.utils.colormaps.categorical_colormap' (napari#6154)
  ...
Carreau pushed a commit that referenced this pull request Sep 19, 2023
#6203 was merged without updating the latest changes from `main` and now
the typing checks in PRs fails due to updates in `napari.components`. We
don't run those changes on `main`, though maybe we should given that we
don't requires PR branches to be up-to-date before merging (only that
git reports no conflicts).

This PR fixes the typing failure by casting the type away from the
possible value of `None`, which is a side-effect of calling
`_open_preferences_dialog`. An alternative fix is to have
`_open_preferences_dialog` to return `PreferencesDialog`, which I like
but felt like a bigger change than needed. Of course, we can also use `#
type: ignore`, but I don't there's a need here.
@Czaki Czaki removed the ready to merge Last chance for comments! Will be merged in ~24h label Sep 23, 2023
@Carreau Carreau added the topic:typing PR that focus on typing improvement label Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance PR with maintance changes, topic:typing PR that focus on typing improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants