MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / test_register_argparse_argument_parameter

Function test_register_argparse_argument_parameter

tests/test_argparse_utils.py:293–334  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

291
292
293def test_register_argparse_argument_parameter() -> None:
294 # Test successful registration
295 param_name = "test_unique_param"
296 register_argparse_argument_parameter(param_name)
297
298 assert param_name in argparse_utils._CUSTOM_ACTION_ATTRIBS
299 assert hasattr(argparse.Action, f"get_{param_name}")
300 assert hasattr(argparse.Action, f"set_{param_name}")
301
302 # Test duplicate registration
303 expected_err = "already registered"
304 with pytest.raises(KeyError, match=expected_err):
305 register_argparse_argument_parameter(param_name)
306
307 # Test invalid identifier
308 expected_err = "must be a valid Python identifier"
309 with pytest.raises(ValueError, match=expected_err):
310 register_argparse_argument_parameter("invalid name")
311
312 # Test collision with standard argparse.Action attribute
313 expected_err = "conflicts with an existing attribute on argparse.Action"
314 with pytest.raises(KeyError, match=expected_err):
315 register_argparse_argument_parameter("format_usage")
316
317 # Test collision with existing accessor methods
318 try:
319 argparse.Action.get_colliding_param = lambda self: None
320 expected_err = "Accessor methods for 'colliding_param' already exist on argparse.Action"
321 with pytest.raises(KeyError, match=expected_err):
322 register_argparse_argument_parameter("colliding_param")
323 finally:
324 delattr(argparse.Action, "get_colliding_param")
325
326 # Test collision with internal attribute
327 try:
328 attr_name = constants.cmd2_private_attr_name("internal_collision")
329 setattr(argparse.Action, attr_name, None)
330 expected_err = f"The internal attribute '{attr_name}' already exists on argparse.Action"
331 with pytest.raises(KeyError, match=expected_err):
332 register_argparse_argument_parameter("internal_collision")
333 finally:
334 delattr(argparse.Action, attr_name)
335
336
337def test_subcommand_attachment() -> None:

Callers

nothing calls this directly

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…