Skip to content

Commit

Permalink
Fix/undefined argument value (#1165)
Browse files Browse the repository at this point in the history
* Changed `unused-keyword` introduction version to 5.3.0

* Rule no longer triggers on non-obvious arg names

* Improved docs to make workaround more explicit
  • Loading branch information
Lakitna authored Jan 9, 2025
1 parent d6178db commit c638b98
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
25 changes: 22 additions & 3 deletions robocop/checkers/misc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Miscellaneous checkers"""

import ast
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -882,12 +883,20 @@ def comma_separated_list(value: str) -> list[str]:
misreading your keyword arguments, explicitly state that the value is empty using the
built-in ``${EMPTY}`` variable.
If your argument is falsly flagged by this rule, escape the ``=`` character in your argument
value by like so: ``\\=``.
Example of a rule violation::
My Amazing Keyword argument_name=
Positional arguments that end with a ``=`` character can be falsly flagged by this rule. You
can fix this by making the situation more explicit:
1. Escape the ``=`` character::
Log value\\=
2. Make it a named argument instead::
Log message=value=
""",
),
}
Expand Down Expand Up @@ -1997,6 +2006,7 @@ class UndefinedArgumentDefaultChecker(VisitorChecker):
"undefined-argument-default",
"undefined-argument-value",
)
valid_argument_name = re.compile(r"[a-zA-Z0-9-_ ]+")

def visit_Arguments(self, node: Arguments): # noqa: N802
for token in node.get_tokens(Token.ARGUMENT):
Expand Down Expand Up @@ -2034,10 +2044,19 @@ def visit_KeywordCall(self, node: KeywordCall): # noqa: N802
# `=` is escaped
continue

if arg_name.endswith(" "):
# Space before `=` is not a named arg
continue

if default_val != "":
# Has a value
continue

is_plain_var_name = self.valid_argument_name.fullmatch(arg_name)
if is_plain_var_name is None:
# Argument name includes invalid chars
continue

# Falsly triggers if a positional argument ends with `=`
# The language server has the same behavior
self.report(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ test.robot:3:12 [E] 0933 Undefined argument value, use message=${EMPTY} instead
test.robot:4:12 [E] 0933 Undefined argument value, use message=${EMPTY} instead
test.robot:4:24 [E] 0933 Undefined argument value, use level=${EMPTY} instead
test.robot:5:27 [E] 0933 Undefined argument value, use level=${EMPTY} instead
test.robot:6:12 [E] 0933 Undefined argument value, use A great log message=${EMPTY} instead
test.robot:7:12 [E] 0933 Undefined argument value, use A great log message =${EMPTY} instead
test.robot:6:12 [E] 0933 Undefined argument value, use A great log message=${EMPTY} instead
7 changes: 7 additions & 0 deletions tests/atest/rules/misc/undefined_argument_value/test.robot
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ With escaped equals sign
With defined values
Log Hello = world
Log message=Hello world
Log message=Hello=
Log message==
Log =
Log = amazing!

Additional edge cases
# https://github.com/MarketSquare/robotframework-robocop/issues/1160
Push Buttons C${expression}=
Get Text xpath=(//h4)[5] *= min
Get Text xpath=(//h4)[5] == min

0 comments on commit c638b98

Please sign in to comment.