-
-
Notifications
You must be signed in to change notification settings - Fork 62
Improve parameter kind handling for argument requirement determination #756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve parameter kind handling for argument requirement determination #756
Conversation
- Add sophisticated parameter kind-aware logic for determining CLI argument requirements - Introduce is_option variable to separate Python semantics from CLI argument style - Ensure KEYWORD_ONLY parameters always use --flag style and are properly marked as required - Fix required attribute logic for parameters that can't be made positional - Maintain full backward compatibility while improving edge case handling
- Add fallback case for programmatically created parameters without kind attribute - Preserves backward compatibility for add_subclass_arguments and similar methods - Fixes test failures in subclasses and link_arguments modules
- Add entry for improved parameter kind handling for argument requirement determination - KEYWORD_ONLY parameters now correctly use --flag style - Add test coverage for positional-only parameter handling
mauvilsa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense what you propose here. I had not considered well the keyword-only cases. But sound more like a bug fix to me.
Thank you for contributing!
|
Thanks for the positive review! I'll have a look at your review and make revision in the near future. |
Co-authored-by: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com>
Co-authored-by: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com>
Co-authored-by: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com>
Co-authored-by: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com>
|
@mauvilsa I finished revision, please have a review again. |
mauvilsa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, but more testing is needed.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #756 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 6884 6895 +11
=========================================
+ Hits 6884 6895 +11 ☔ View full report in Codecov by Sentry. |
mauvilsa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I committed some changes to finish it off.
What does this PR do?
Fixes #757
This PR enhances the logic for determining whether function/method parameters should be treated as required CLI arguments by implementing more sophisticated parameter kind-aware handling.
Problem: The current implementation uses a simple rule (
is_required = default == inspect_empty) which doesn't properly account for Python's different parameter kinds. This conflates two different concepts:--flagstyleThis causes issues particularly with
KEYWORD_ONLYparameters, which should always use the--flagstyle regardless of whether they have defaults, and should be properly marked as required when they don't have defaults. This design matches with CLI project, https://github.com/BrianPugh/cyclopts.Solution: Introduced a more sophisticated parameter kind-aware approach:
--flagstyleBefore/After Comparison
Consider this test function:
Before this PR:
After this PR:
Key improvements:
KEYWORD_ONLYparameters (kwarg,a,b) now correctly use--flagstyle instead of being treated as positional argumentsKEYWORD_ONLYparameters are properly marked with--kwarg KWARG,--a A,--b Bin the usage linePOSITIONAL_OR_KEYWORDparameters (posarg) can still become positional when appropriateUnpack[TypedDict]The core issue was that
KEYWORD_ONLYparameters were incorrectly being treated as positional arguments whenas_positional=True, which violates Python's parameter semantics. Now they correctly always use the--flagstyle as they should.Before submitting