Skip to content

Commit 4f6925f

Browse files
committed
Reject untyped NA variable declarations
1 parent 148c59b commit 4f6925f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/pinescript_validator/ast_validator.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ def validate_statement(self, statement: AST.Statement, scope: Scope, conditional
198198
self.validate_type_annotation(statement.type_annotation, scope, statement.line, statement.column)
199199
if statement.init is not None:
200200
self.validate_expression(statement.init, scope, conditional_context)
201+
if statement.type_annotation is None and self.is_na_literal(statement.init):
202+
self.untyped_na_declaration_error(statement.name_line, statement.name_column, statement.name)
201203
if self.is_bool_type_annotation(statement.type_annotation) and self.is_na_literal(statement.init):
202204
self.bool_na_error(statement.init.line, statement.init.column)
203205
return
@@ -1281,6 +1283,18 @@ def bool_na_error(self, line: int, column: int) -> None:
12811283
)
12821284
)
12831285

1286+
def untyped_na_declaration_error(self, line: int, column: int, name: str) -> None:
1287+
self.errors.append(
1288+
Diagnostic(
1289+
line=line,
1290+
column=column,
1291+
length=len(name),
1292+
message="Value with NA type cannot be assigned to a variable that was defined without type keyword",
1293+
severity=Severity.ERROR,
1294+
source="ast",
1295+
)
1296+
)
1297+
12841298
def lookup_accessible_symbol(
12851299
self,
12861300
scope: Scope,

tests/test_validator.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ def test_unused_variable(self) -> None:
5050
diagnostics = self.validator.validate_text('indicator("Test")\nvalue = close')
5151
self.assertTrue(any("never used" in diagnostic.message for diagnostic in diagnostics))
5252

53+
def test_untyped_variable_declaration_cannot_use_na(self) -> None:
54+
diagnostics = self.validator.validate_text("x = na")
55+
self.assertTrue(
56+
any(
57+
"Value with NA type cannot be assigned to a variable that was defined without type keyword" in diagnostic.message
58+
for diagnostic in diagnostics
59+
)
60+
)
61+
62+
def test_typed_variable_declaration_can_use_na(self) -> None:
63+
diagnostics = self.validator.validate_text("float x = na")
64+
self.assertFalse(
65+
any(
66+
"Value with NA type cannot be assigned to a variable that was defined without type keyword" in diagnostic.message
67+
for diagnostic in diagnostics
68+
)
69+
)
70+
5371
def test_invalid_named_argument(self) -> None:
5472
diagnostics = self.validator.validate_text('plot(close, invalid_param=true)')
5573
self.assertTrue(any("Invalid parameter 'invalid_param'" in diagnostic.message for diagnostic in diagnostics))

0 commit comments

Comments
 (0)