Skip to content

fix: recover handling of typing.Optional[X]#90

Merged
johanneskoester merged 4 commits intosnakemake:mainfrom
veprbl:pr/fix_optional
Mar 13, 2026
Merged

fix: recover handling of typing.Optional[X]#90
johanneskoester merged 4 commits intosnakemake:mainfrom
veprbl:pr/fix_optional

Conversation

@veprbl
Copy link
Copy Markdown
Contributor

@veprbl veprbl commented Feb 24, 2026

This fixes downstream bug bouweandela/snakemake-storage-plugin-rucio#41 which prevented type.Optional[int] settings from performing a type conversion.

Turns out that was a regression from
#59
@jjjermiah

typing.Union[X] is not instance of type. The logic of the function is about checking the type of value, so specifying a narrow annotation forces duplicating function logic around its calls.

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling for type conversions in plugin settings with clearer, descriptive user-facing errors when conversion fails.
  • Refactor

    • Broadened plugin type handling to accept more complex type representations and enforce valid Union usage (disallowing non-Optional unions).
  • Tests

    • Added tests covering integer conversion and Optional[int] handling for plugin settings, plus example plugin/settings scaffolding.

typing.Union[X] is not instance of type. The logic of the function is
about checking the type of `value`, so specifying a narrow annotation
forces duplicating function logic around its calls.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 24, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 73865ae0-04df-48d9-8f95-b02bde077a9d

📥 Commits

Reviewing files that changed from the base of the PR and between 4c6100b and 12f3317.

📒 Files selected for processing (1)
  • tests/tests.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/tests.py

📝 Walkthrough

Walkthrough

Relaxed type handling and improved error reporting in the plugin registry's type-parsing logic; widened _parse_type/apply_type signatures to Any, added validation for typing.Union (only Optional[T] allowed), and added tests and test plugin/settings to verify string→int and Optional[int] conversions.

Changes

Cohort / File(s) Summary
Type parsing logic refinement
src/snakemake_interface_common/plugin_registry/plugin.py
Removed strict isinstance(thefield.type, type) check; changed _parse_type and nested apply_type parameter types from Type to Any; wrapped type conversions in try/except to raise WorkflowError with descriptive messages; added typing.Union handling to allow only Optional[T] and raise InvalidPluginException for other unions.
Tests and test plugin scaffolding
tests/tests.py
Added TestSettings dataclass with required_int: int and optional_int: Optional[int]; added TestPlugin class and tests test_settings_int_conversion and test_settings_optional_int_none to verify string-to-int conversion and Optional[int] None handling; expanded imports and test harness for CLI-arg simulation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.08% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main fix: restoring support for typing.Optional[X] type handling, which is the primary objective of the PR.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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
Copy Markdown

@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.

🧹 Nitpick comments (1)
src/snakemake_interface_common/plugin_registry/plugin.py (1)

348-352: args[1] == None.__class__ is order-sensitive and the noqa comment is misapplied.

Two minor concerns:

  1. Order sensitivity: get_args(Optional[int]) always yields (int, type(None)), so args[1] is reliably NoneType for typing.Optional. However, an explicit Union[None, int] preserves declaration order → args[0] would be NoneType and the check would fall into the else branch, incorrectly raising InvalidPluginException. Checking membership rather than position makes this robust:

  2. noqa: E711: Flake8's E711 fires on x == None (comparison to the singleton). None.__class__ is type(None) (a type object), not None, so E711 does not apply here. The suppression comment is misleading. The idiomatic form also prefers is for type identity.

♻️ Proposed fix
-            if len(args) == 2 and args[1] == None.__class__:  # noqa: E711
+            if len(args) == 2 and type(None) in args:
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/snakemake_interface_common/plugin_registry/plugin.py` around lines 348 -
352, The current Optional detection in the block using typing.get_origin/thetype
and indexing args[1] is order-sensitive and misuses noqa; change the logic in
that Union handling to detect Optional by checking that len(args) == 2 and that
one of the args is type(None) (e.g. any(arg is type(None) for arg in args)),
then pick the non-None arg (the other element of args) and call
apply_type(value, non_none_arg); remove the unnecessary noqa E711 suppression
and use identity (is) when comparing to type(None); ensure
InvalidPluginException is still raised for non-Optional unions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/snakemake_interface_common/plugin_registry/plugin.py`:
- Around line 348-352: The current Optional detection in the block using
typing.get_origin/thetype and indexing args[1] is order-sensitive and misuses
noqa; change the logic in that Union handling to detect Optional by checking
that len(args) == 2 and that one of the args is type(None) (e.g. any(arg is
type(None) for arg in args)), then pick the non-None arg (the other element of
args) and call apply_type(value, non_none_arg); remove the unnecessary noqa E711
suppression and use identity (is) when comparing to type(None); ensure
InvalidPluginException is still raised for non-Optional unions.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1b3449f and 9fa20ce.

📒 Files selected for processing (1)
  • src/snakemake_interface_common/plugin_registry/plugin.py

Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>
@johanneskoester johanneskoester merged commit 62cf62e into snakemake:main Mar 13, 2026
5 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.

2 participants