Skip to content

Comments

[fix] PostureManager.get_posture_orientation() could return None#3376

Open
pieleric wants to merge 1 commit intodelmic:masterfrom
pieleric:fix-posturemanager-get_posture_orientation-could-return-none
Open

[fix] PostureManager.get_posture_orientation() could return None#3376
pieleric wants to merge 1 commit intodelmic:masterfrom
pieleric:fix-posturemanager-get_posture_orientation-could-return-none

Conversation

@pieleric
Copy link
Member

It looks like it would always return a dict (position)... but passing a
unsupported posture would cause it to return None implicitly.

Instead, raise an exception in such case, to catch such error directly.

It looks like it would always return a dict (position)... but passing a
unsupported posture would cause it to return None implicitly.

Instead, raise an exception in such case, to catch such error directly.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug in the PostureManager.get_posture_orientation() method where passing an unsupported posture value would cause the method to implicitly return None instead of raising an exception. The fix adds an explicit else clause that raises a KeyError with a descriptive error message when an unsupported posture is provided.

Changes:

  • Added explicit exception handling for unsupported posture values in get_posture_orientation() to catch errors directly instead of returning None implicitly

column_tilt=self.fib_column_tilt)
return {"rx": rx, "rz": md["rz"]}
else:
raise KeyError(f"posture {POSITION_NAMES.get(posture, posture)} not supported for orientation retrieval")
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

ValueError should be used instead of KeyError for invalid parameter values. Throughout this codebase, ValueError is consistently used when an unsupported posture or target position parameter is provided (see lines 775, 778, 921, 949, 1063, etc.). KeyError is typically reserved for missing dictionary keys or axes (see lines 892, 1193, 1369). Since 'posture' is a method parameter that has an invalid value, ValueError is the more appropriate exception type.

Suggested change
raise KeyError(f"posture {POSITION_NAMES.get(posture, posture)} not supported for orientation retrieval")
raise ValueError(f"posture {POSITION_NAMES.get(posture, posture)} not supported for orientation retrieval")

Copilot uses AI. Check for mistakes.
@coderabbitai
Copy link

coderabbitai bot commented Feb 20, 2026

📝 Walkthrough

Walkthrough

The change adds explicit error handling in the MicroscopePostureManager.get_posture_orientation method to raise a KeyError when an unsupported posture is provided. Previously, the method might have returned nothing or behaved unexpectedly for unknown postures. Now it validates that the input posture matches one of five supported values: SEM_IMAGING, FM_IMAGING, LOADING, FIB_IMAGING, or MILLING. If the posture is not recognized, it raises a KeyError with a descriptive message. The handling for supported postures remains unchanged.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main fix: preventing the function from returning None by raising an exception for unsupported postures.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem (implicit None return) and the solution (raising an exception).
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/odemis/acq/move.py`:
- Around line 518-519: Replace the incorrect KeyError raised in the
orientation-retrieval branch with a ValueError so invalid posture argument
values use the canonical exception (change the raise in the else that currently
does raise KeyError(f"posture {POSITION_NAMES.get(posture, posture)} not
supported for orientation retrieval") to raise ValueError with the same
message), and update this function's/docstring to list ValueError as a possible
exception (keeping behavior consistent with other methods like to_posture(),
getTargetPosition(), and _doCryoSwitchSamplePosition()).

Comment on lines +518 to +519
else:
raise KeyError(f"posture {POSITION_NAMES.get(posture, posture)} not supported for orientation retrieval")
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Use ValueError instead of KeyError for an unsupported argument value.

KeyError is semantically reserved for missing dict/mapping keys. Here, posture is an integer argument with an invalid value — the canonical exception for that is ValueError, which is also what every other unsupported-posture path in this file raises (to_posture(), getTargetPosition(), _doCryoSwitchSamplePosition() in all subclasses, etc.).

Using KeyError also risks being silently swallowed: each _doCryoSwitchSamplePosition implementation contains a narrowly scoped except KeyError block that converts a POSITION_NAMES[target] lookup failure into a ValueError. If the call chain happens to cross one of those boundaries, the wrong error message is surfaced.

Additionally, the docstring should be updated to document the newly raised exception.

🛠 Proposed fix
-        else:
-            raise KeyError(f"posture {POSITION_NAMES.get(posture, posture)} not supported for orientation retrieval")
+        else:
+            raise ValueError(f"posture {POSITION_NAMES.get(posture, posture)} not supported for orientation retrieval")

And update the docstring:

     def get_posture_orientation(self, posture: int) -> Dict[str, float]:
         """Get the orientation of the stage for the given posture
         :param posture: the posture to get the orientation for
-        :return: a dict with the orientation of the stage for the given posture"""
+        :return: a dict with the orientation of the stage for the given posture
+        :raises ValueError: if the posture is not supported for orientation retrieval
+        """
🧰 Tools
🪛 Ruff (0.15.1)

[warning] 519-519: Avoid specifying long messages outside the exception class

(TRY003)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/odemis/acq/move.py` around lines 518 - 519, Replace the incorrect
KeyError raised in the orientation-retrieval branch with a ValueError so invalid
posture argument values use the canonical exception (change the raise in the
else that currently does raise KeyError(f"posture {POSITION_NAMES.get(posture,
posture)} not supported for orientation retrieval") to raise ValueError with the
same message), and update this function's/docstring to list ValueError as a
possible exception (keeping behavior consistent with other methods like
to_posture(), getTargetPosition(), and _doCryoSwitchSamplePosition()).

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.

1 participant