Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 134 additions & 12 deletions docs/rules_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -13351,29 +13351,19 @@ Robot Framework 7.3 introduced type conversion for variables. This rule
enforces that variables in the ``*** Variables ***`` section have explicit
type annotations for better code clarity and automatic type conversion.

This rule also checks ``VAR`` statements and assignment expressions
(``${var} = Keyword``).
Variables defined in keywords and tests are checked by the separate
``missing-keyword-variable-type`` and ``missing-test-variable-type`` rules.
Comment on lines +13354 to +13355

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agent artifact without meaningful documentation value


Incorrect code example (when rule is enabled):

*** Variables ***
${NUMBER} 42

*** Keywords ***
Example
VAR ${local} value
${result} = Some Keyword

Correct code:

*** Variables ***
${NUMBER: int} 42

*** Keywords ***
Example
VAR ${local: str} value
${result: list} = Some Keyword

This rule is disabled by default.

**Parameters**:
Expand Down Expand Up @@ -13595,3 +13585,135 @@ The VAR syntax needs to be used instead:
"set-keyword-with-type.severity=E"
]
```

---

### ANN05: missing-keyword-variable-type

> Rule is disabled by default. Enable it by using ``--select missing-keyword-variable-type`` option.

Added: `v9.5.0`

Supported RF version `>=7.3`

Fix availability: There is no automatic fix.

**Message**:

`Variable '{variable_name}' is missing type annotation`

**Documentation**:

Keyword variable without type annotation.

Robot Framework 7.3 introduced type conversion for variables. This rule
enforces that variables created inside keywords with ``VAR`` statements and
assignment expressions (``${var} = Keyword``) have explicit type annotations
for better code clarity and automatic type conversion.

Incorrect code example (when rule is enabled):

*** Keywords ***
Example
VAR ${local} value
${result} = Some Keyword
Comment on lines +13618 to +13619

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be separate rules?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally leaning towards no right now. But if, at some point, types can be inferred from the keyword return value, this should maybe be split.


Correct code:

*** Keywords ***
Example
VAR ${local: str} value
${result: list} = Some Keyword

This rule is disabled by default.

**Parameters**:

??? example "severity"

Rule severity (e = error, w = warning, i = info)

**Default value:** I

**Type:** severity

=== ":octicons-command-palette-24: cli"

``` bash
robocop check --configure missing-keyword-variable-type.severity=I
```

=== ":material-file-cog-outline: toml"

``` toml
[tool.robocop.lint]
configure = [
"missing-keyword-variable-type.severity=I"
]
```

---

### ANN06: missing-test-variable-type

> Rule is disabled by default. Enable it by using ``--select missing-test-variable-type`` option.

Added: `v9.5.0`

Supported RF version `>=7.3`

Fix availability: There is no automatic fix.

**Message**:

`Variable '{variable_name}' is missing type annotation`

**Documentation**:

Test variable without type annotation.

Robot Framework 7.3 introduced type conversion for variables. This rule
enforces that variables created inside tests or tasks with ``VAR`` statements and
assignment expressions (``${var} = Keyword``) have explicit type annotations
for better code clarity and automatic type conversion.

Incorrect code example (when rule is enabled):

*** Test Cases ***
Example
VAR ${local} value
${result} = Some Keyword

Correct code:

*** Test Cases ***
Example
VAR ${local: str} value
${result: list} = Some Keyword

This rule is disabled by default.

**Parameters**:

??? example "severity"

Rule severity (e = error, w = warning, i = info)

**Default value:** I

**Type:** severity

=== ":octicons-command-palette-24: cli"

``` bash
robocop check --configure missing-test-variable-type.severity=I
```

=== ":material-file-cog-outline: toml"

``` toml
[tool.robocop.lint]
configure = [
"missing-test-variable-type.severity=I"
]
```
28 changes: 24 additions & 4 deletions src/robocop/linter/checkers/variable_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

if TYPE_CHECKING:
from robot.parsing.model import File
from robot.parsing.model.blocks import For, KeywordSection, VariableSection
from robot.parsing.model.blocks import For, KeywordSection, TestCaseSection, VariableSection
from robot.parsing.model.statements import Arguments, KeywordCall, Var, Variable


Expand All @@ -24,6 +24,8 @@ class VariablesChecker(VisitorChecker):
no_test_variable: variables.NoTestVariableRule
set_keyword_with_type: typing.SetKeywordWithTypeRule
missing_section_variable_type: typing.MissingSectionVariableTypeRule
missing_keyword_variable_type: typing.MissingKeywordVariableTypeRule
missing_test_variable_type: typing.MissingTestVariableTypeRule
missing_argument_type: typing.MissingArgumentTypeRule
missing_for_loop_variable_type: typing.MissingForLoopVariableTypeRule
inconsistent_assignment: misc.InconsistentAssignmentRule
Expand All @@ -41,10 +43,14 @@ def __init__(self) -> None:
self.keyword_expected_sign_type: str | None = None
self.variables_expected_sign_type: str | None = None
self.check_empty_variable = True
self.missing_body_variable_type: (
typing.MissingKeywordVariableTypeRule | typing.MissingTestVariableTypeRule | None
) = None
super().__init__()

def visit_File(self, node: File) -> None: # noqa: N802
self.check_empty_variable = True
self.missing_body_variable_type = None
self.detect_assignment_sign(node)
self.generic_visit(node)

Expand All @@ -70,12 +76,26 @@ def visit_VariableSection(self, node: VariableSection) -> None: # noqa: N802
self.check_empty_variable = True

def visit_KeywordSection(self, node: KeywordSection) -> None: # noqa: N802
self.visit_body_section(node, self.missing_keyword_variable_type)

def visit_TestCaseSection(self, node: TestCaseSection) -> None: # noqa: N802
self.visit_body_section(node, self.missing_test_variable_type)

def visit_body_section(
self,
node: KeywordSection | TestCaseSection,
missing_type_rule: typing.MissingKeywordVariableTypeRule | typing.MissingTestVariableTypeRule,
) -> None:
# ``empty-variable`` is only reported here when the VAR syntax is one of its configured sources
self.check_empty_variable = "var" in self.empty_variable.variable_source
self.missing_body_variable_type = missing_type_rule
self.generic_visit(node)
self.check_empty_variable = True
self.missing_body_variable_type = None

visit_TestCaseSection = visit_KeywordSection # noqa: N815
def check_body_variable_type(self, node: Var | KeywordCall, token: Token) -> None:
if self.missing_body_variable_type is not None:
self.missing_body_variable_type.check(node, token)

def visit_Variable(self, node: Variable) -> None: # noqa: N802
if self.check_empty_variable:
Expand All @@ -93,7 +113,7 @@ def visit_Var(self, node: Var) -> None: # noqa: N802
return
variable = node.get_token(Token.VARIABLE)
if variable:
self.missing_section_variable_type.check(node, variable)
self.check_body_variable_type(node, variable)
self.check_var_assignment_sign(variable)

def check_var_assignment_sign(self, variable: Token) -> None:
Expand All @@ -119,7 +139,7 @@ def check_var_scope(self, node: Var) -> None:
def visit_KeywordCall(self, node: KeywordCall) -> None: # noqa: N802
self.check_assignment_sign(node)
for token in node.get_tokens(Token.ASSIGN):
self.missing_section_variable_type.check(node, token)
self.check_body_variable_type(node, token)
self.check_set_variable_keyword(node)

def check_assignment_sign(self, node: KeywordCall) -> None:
Expand Down
Loading
Loading