Skip to content

NameError in ParamUnknownKeyError due to undefined variable valid_key #10405

@Dreamstick9

Description

@Dreamstick9

Describe the bug

When the CLI attempts to raise a ParamUnknownKeyError (typically during map/shorthand validation), a NameError is thrown instead. This occurs due to a typo in awscli/argprocess.py (line 58), where valid_key is used instead of the locally defined valid_keys variable.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

The CLI should successfully format and raise a ParamUnknownKeyError, displaying a clear error message that lists the invalid key alongside the valid choices.

Current Behavior

A NameError is thrown because valid_key is not defined in the local scope, masking the intended CLI parsing error.

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from awscli.argprocess import ParamUnknownKeyError; ParamUnknownKeyError('BadKey', ['GoodKey1', 'GoodKey2'])
                                                        ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kushagargargsmacbook/aws-cli/awscli/argprocess.py", line 58, in __init__
    f"Unknown key '{key}', valid choices are: {valid_key}"
                                               ^^^^^^^^^
NameError: name 'valid_key' is not defined. Did you mean: 'valid_keys'?

Reproduction Steps

This bug can be cleanly isolated and reproduced directly by instantiating the error class in a Python REPL:

python3 -c "from awscli.argprocess import ParamUnknownKeyError; ParamUnknownKeyError('BadKey', ['GoodKey1', 'GoodKey2'])"

Possible Solution

Change the variable name from valid_key to valid_keys on line 58 of awscli/argprocess.py.

# Current
f"Unknown key '{key}', valid choices are: {valid_key}"

# Proposed Fix
f"Unknown key '{key}', valid choices are: {valid_keys}"

Additional Information/Context

I have tested this fix locally and will be opening a Pull Request shortly to address it.

CLI version used

aws-cli/1.45.29 Python/3.12.1 botocore/1.43.29

Environment details (OS name and version, etc.)

macOS / Linux (6.8.0-1052-azure)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions