Closed coderabbitai[bot] closed 4 weeks ago
Python 3 Compatibility:
Supported Syntax: In Python 3.x, this syntax is fully supported. You can define functions with variable positional arguments (
*args
) followed by keyword-only arguments with default values. This allows you to enforce that certain arguments must be provided as keyword arguments.Official Documentation:
Python 3 Documentation - Function Definitions: This section explains the syntax of function definitions, including the use of
*args
and keyword-only arguments.PEP 3102 – Keyword-Only Arguments: This Python Enhancement Proposal introduced keyword-only arguments in Python 3.0, allowing arguments following
*args
to be specified as keyword-only.Python 2 Compatibility:
- Syntax Limitation: In Python 2.x, the syntax
def someFunction(*args, optional_non_positional_arg=None):
is not supported. Placing a parameter after*args
without using**kwargs
will result in aSyntaxError
.
_Originally posted by @coderabbitai[bot] in https://github.com/reactive-firewall/multicast/pull/127#discussion_r1774331992_
Basically we want to go with:
def timePythonCommand(*args, stderr=None):
if not args or args[0] is None:
raise RuntimeError("args must be an array of positional arguments")
return checkPythonCommand(args, stderr)
and
def checkPythonFuzzing(*args, stderr=None):
if not args or args[0] is None:
raise RuntimeError("args must be an array of positional arguments")
return checkPythonCommand(args, stderr)
and see #128
def checkCovCommand(*args):
... docstrings go here ...
if not args or args[0] is None:
raise RuntimeError("args must be an array of positional arguments")
elif str("coverage") in args[0]:
... rest of function
[!TIP] For best results, initiate chat on the files or code changes.
@reactive-firewall, thank you for the clarification and the examples provided. I see that the refactoring aligns with the objectives outlined in PR #127 and the follow-up in #128. The use of *args
and keyword-only arguments (stderr=None
) is a great approach to ensure flexibility while enforcing the necessary checks for positional arguments.
I'll proceed with the following:
Refactoring:
timePythonCommand
, checkPythonFuzzing
, and checkCovCommand
functions as per your examples.Testing:
None
arguments.Documentation:
Let me know if you'd like me to open a new issue for tracking this refactor or if you'd prefer to handle it within the current PR.
Also, I'll make sure to retain the learning about your preference for using *args
and raising exceptions for missing arguments instead of using args=None
.
Summary
In the discussion on PR #127 (https://github.com/reactive-firewall/multicast/pull/127) (see comment https://github.com/reactive-firewall/multicast/pull/127#discussion_r1774288385 by @reactive-firewall), we agreed to refactor the
timePythonCommand
andcheckPythonFuzzing
functions to use*args
and keyword-only arguments. This change avoids mutable default arguments, aligns with the intended functionality, and improves debugging.Plan for Next Steps
Refactoring:
timePythonCommand
andcheckPythonFuzzing
functions to use*args
and keyword-only arguments (stderr=None
).RuntimeError
ifargs
is empty or the first argument isNone
.checkPythonCommand
can handleargs
as a tuple or convert it to a list if necessary.Testing:
args
is empty or containsNone
.Documentation:
References