fix: recover handling of typing.Optional[X]#90
fix: recover handling of typing.Optional[X]#90johanneskoester merged 4 commits intosnakemake:mainfrom
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRelaxed type handling and improved error reporting in the plugin registry's type-parsing logic; widened Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/snakemake_interface_common/plugin_registry/plugin.py (1)
348-352:args[1] == None.__class__is order-sensitive and thenoqacomment is misapplied.Two minor concerns:
Order sensitivity:
get_args(Optional[int])always yields(int, type(None)), soargs[1]is reliablyNoneTypefortyping.Optional. However, an explicitUnion[None, int]preserves declaration order →args[0]would beNoneTypeand the check would fall into theelsebranch, incorrectly raisingInvalidPluginException. Checking membership rather than position makes this robust:
noqa: E711: Flake8's E711 fires onx == None(comparison to the singleton).None.__class__istype(None)(a type object), notNone, so E711 does not apply here. The suppression comment is misleading. The idiomatic form also prefersisfor 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.
Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>
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
Refactor
Tests