Skip to content

Commit 02cf495

Browse files
committed
refactor: introduce RF_VERSION constant and remove RF < 5.0 dead code
- Add module-level RF_VERSION constant to robotcode.robot.utils, resolved once at import time instead of repeated lru_cache lookups - Remove dead code paths for Robot Framework < 5.0 (no longer supported), including the internal tidy-based formatter and obsolete version branches - Replace all ~134 get_robot_version() call sites across 27 files with the RF_VERSION constant
1 parent 0c347fd commit 02cf495

File tree

27 files changed

+266
-367
lines changed

27 files changed

+266
-367
lines changed

bundled/tool/utils/check_robot_version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ def update_sys_path(path_to_add: str, strategy: str) -> None:
2727
print("Robot Framework not installed", file=sys.stderr)
2828
sys.exit(1)
2929

30-
from robotcode.robot.utils import get_robot_version
30+
from robotcode.robot.utils import RF_VERSION
3131

32-
print(get_robot_version() >= (4, 1))
32+
print(RF_VERSION >= (5, 0))

packages/debugger/src/robotcode/debugger/debugger.py

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
from robot.errors import VariableError
3737
from robot.output import LOGGER
3838
from robot.running import EXECUTION_CONTEXTS, Keyword, TestCase, TestSuite
39+
from robot.running.model import Try
40+
from robot.utils import Matcher as RobotMatcher
3941
from robot.variables import evaluate_expression
4042

4143
from robotcode.core.event import event
4244
from robotcode.core.utils.logging import LoggingDescriptor
43-
from robotcode.robot.utils import get_robot_version
45+
from robotcode.robot.utils import RF_VERSION
4446

4547
from .dap_types import (
4648
Breakpoint,
@@ -72,16 +74,12 @@
7274
)
7375
from .id_manager import IdManager
7476

75-
if get_robot_version() >= (5, 0):
76-
from robot.running.model import Try
77-
from robot.utils import Matcher as RobotMatcher
78-
79-
if get_robot_version() >= (7, 0):
77+
if RF_VERSION >= (7, 0):
8078
from robot.running import UserKeyword as UserKeywordHandler
8179
else:
8280
from robot.running.userkeyword import UserKeywordHandler
8381

84-
if get_robot_version() >= (6, 1):
82+
if RF_VERSION >= (6, 1):
8583

8684
def internal_evaluate_expression(expression: str, variable_store: Any) -> Any:
8785
return evaluate_expression(expression, variable_store)
@@ -303,7 +301,7 @@ def __init__(self) -> None:
303301
self.steps: List[Any] = []
304302

305303

306-
if get_robot_version() < (7, 0):
304+
if RF_VERSION < (7, 0):
307305

308306
class DebugLogger(DebugLoggerBase):
309307
def start_keyword(self, kw: Any) -> None:
@@ -1119,7 +1117,7 @@ def end_test(self, name: str, attributes: AttributeDict) -> None:
11191117

11201118
self.remove_stackframe_entry(longname, type, source, line_no)
11211119

1122-
if get_robot_version() >= (7, 0):
1120+
if RF_VERSION >= (7, 0):
11231121

11241122
def get_current_keyword_handler(self, name: str) -> UserKeywordHandler:
11251123
return EXECUTION_CONTEXTS.current.namespace.get_runner(name).keyword
@@ -1252,7 +1250,7 @@ def _should_run_except(self, branch: Any, error: str) -> bool:
12521250

12531251
return False
12541252

1255-
if get_robot_version() >= (7, 0):
1253+
if RF_VERSION >= (7, 0):
12561254

12571255
def _get_step_data(self, step: Any) -> Any:
12581256
return step
@@ -1262,31 +1260,23 @@ def _get_step_data(self, step: Any) -> Any:
12621260
def _get_step_data(self, step: Any) -> Any:
12631261
return step.data
12641262

1265-
if get_robot_version() < (5, 0):
1266-
1267-
def is_not_caugthed_by_except(self, message: Optional[str]) -> bool:
1268-
if not message:
1269-
return True
1270-
return False
1271-
else:
1263+
def is_not_caugthed_by_except(self, message: Optional[str]) -> bool:
1264+
if not message:
1265+
return True
12721266

1273-
def is_not_caugthed_by_except(self, message: Optional[str]) -> bool:
1274-
if not message:
1275-
return True
1267+
# TODO resolve variables in exception message
12761268

1277-
# TODO resolve variables in exception message
1278-
1279-
if self.debug_logger:
1280-
if self.debug_logger.steps:
1281-
for branch in [
1282-
self._get_step_data(f)
1283-
for f in reversed(self.debug_logger.steps)
1284-
if isinstance(self._get_step_data(f), Try)
1285-
]:
1286-
for except_branch in branch.except_branches:
1287-
if self._should_run_except(except_branch, message):
1288-
return False
1289-
return True
1269+
if self.debug_logger:
1270+
if self.debug_logger.steps:
1271+
for branch in [
1272+
self._get_step_data(f)
1273+
for f in reversed(self.debug_logger.steps)
1274+
if isinstance(self._get_step_data(f), Try)
1275+
]:
1276+
for except_branch in branch.except_branches:
1277+
if self._should_run_except(except_branch, message):
1278+
return False
1279+
return True
12901280

12911281
def end_keyword(self, name: str, attributes: AttributeDict) -> None:
12921282
if self.state == State.CallKeyword:
@@ -1602,7 +1592,7 @@ def _create_variable(
16021592

16031593
return Variable(name=name, value=self.debug_repr.repr(value), type=repr(type(value)))
16041594

1605-
if get_robot_version() >= (7, 0):
1595+
if RF_VERSION >= (7, 0):
16061596

16071597
def get_handler_args(self, handler: UserKeywordHandler) -> Any:
16081598
return handler.args
@@ -1764,7 +1754,7 @@ def _get_variables_named(
17641754
SPLIT_LINE: ClassVar = re.compile(r"(?= {2,}| ?\t)\s*")
17651755
CURRDIR: ClassVar = re.compile(r"(?i)\$\{CURDIR\}")
17661756

1767-
if get_robot_version() >= (7, 0):
1757+
if RF_VERSION >= (7, 0):
17681758

17691759
def _run_keyword(self, kw: Keyword, context: Any) -> Any:
17701760
return kw.run(context.steps[-1][1], context)
@@ -1774,7 +1764,7 @@ def _run_keyword(self, kw: Keyword, context: Any) -> Any:
17741764
def _run_keyword(self, kw: Keyword, context: Any) -> Any:
17751765
return kw.run(context)
17761766

1777-
if get_robot_version() >= (7, 2):
1767+
if RF_VERSION >= (7, 2):
17781768

17791769
@staticmethod
17801770
def check_message_is_logged(listener: Any, msg: Any) -> bool:
@@ -1963,7 +1953,7 @@ def run_kw() -> Any:
19631953
result = None
19641954

19651955
if len(test.body):
1966-
if get_robot_version() >= (7, 3):
1956+
if RF_VERSION >= (7, 3):
19671957
result = self._execute_keywords_with_delayed_logging_v73(test.body, evaluate_context)
19681958
else:
19691959
result = self._execute_keywords_with_delayed_logging_legacy(test.body, evaluate_context)
@@ -2003,7 +1993,7 @@ def _execute_keywords_with_delayed_logging_legacy(self, keywords: Any, evaluate_
20031993
result = e
20041994
break
20051995
finally:
2006-
if get_robot_version() <= (7, 2):
1996+
if RF_VERSION <= (7, 2):
20071997
self._process_delayed_log_messages()
20081998
return result
20091999

@@ -2161,7 +2151,7 @@ def set_exception_breakpoints(
21612151

21622152
return result or None
21632153

2164-
if get_robot_version() >= (7, 0):
2154+
if RF_VERSION >= (7, 0):
21652155

21662156
def _get_keywords_from_lib(self, lib: Any) -> Any:
21672157
return lib.keywords

packages/debugger/src/robotcode/debugger/launcher/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from robotcode.core.utils.logging import LoggingDescriptor
1212
from robotcode.jsonrpc2.protocol import rpc_method
1313
from robotcode.jsonrpc2.server import JsonRPCServer
14-
from robotcode.robot.utils import get_robot_version
14+
from robotcode.robot.utils import RF_VERSION
1515

1616
from ..cli import DEBUGGER_DEFAULT_PORT, DEBUGPY_DEFAULT_PORT
1717
from ..dap_types import (
@@ -194,7 +194,7 @@ async def _launch(
194194

195195
run_args = []
196196

197-
if get_robot_version() >= (6, 0) and languages:
197+
if RF_VERSION >= (6, 0) and languages:
198198
for lang in languages:
199199
run_args += ["--language", lang]
200200

packages/language_server/src/robotcode/language_server/robotframework/parts/code_action_refactor.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from robotcode.core.utils.inspect import iter_methods
2828
from robotcode.core.utils.logging import LoggingDescriptor
2929
from robotcode.robot.diagnostics.model_helper import ModelHelper
30-
from robotcode.robot.utils import get_robot_version
3130
from robotcode.robot.utils.ast import (
3231
get_node_at_position,
3332
get_nodes_at_position,
@@ -152,36 +151,31 @@ def resolve(self, sender: Any, code_action: CodeAction) -> Optional[CodeAction]:
152151

153152
def get_valid_nodes_in_range(self, model: ast.AST, range: Range, also_return: bool = False) -> List[ast.AST]:
154153
from robot.parsing.lexer.tokens import Token as RobotToken
155-
from robot.parsing.model.blocks import Block, Keyword, TestCase
154+
from robot.parsing.model.blocks import Block, Keyword, TestCase, Try
156155
from robot.parsing.model.statements import (
156+
Break,
157157
Comment,
158+
Continue,
158159
Documentation,
159160
ElseHeader,
160161
ElseIfHeader,
161162
EmptyLine,
162163
End,
164+
ExceptHeader,
165+
FinallyHeader,
163166
Fixture,
164167
ForHeader,
165168
IfHeader,
166169
KeywordName,
167170
MultiValue,
171+
ReturnStatement,
168172
SingleValue,
169173
TemplateArguments,
170174
TestCaseName,
175+
TryHeader,
176+
WhileHeader,
171177
)
172178

173-
if get_robot_version() >= (5, 0, 0):
174-
from robot.parsing.model.blocks import Try
175-
from robot.parsing.model.statements import (
176-
Break,
177-
Continue,
178-
ExceptHeader,
179-
FinallyHeader,
180-
ReturnStatement,
181-
TryHeader,
182-
WhileHeader,
183-
)
184-
185179
if not isinstance(model, (Keyword, TestCase)):
186180
return []
187181

@@ -205,7 +199,7 @@ def get_valid_nodes_in_range(self, model: ast.AST, range: Range, also_return: bo
205199
KeywordName,
206200
TemplateArguments,
207201
),
208-
) or (also_return and get_robot_version() >= (5, 0, 0) and isinstance(node, ReturnStatement)):
202+
) or (also_return and isinstance(node, ReturnStatement)):
209203
return []
210204

211205
result.append(node)
@@ -241,11 +235,11 @@ def get_valid_nodes_in_range(self, model: ast.AST, range: Range, also_return: bo
241235
n
242236
for n in result
243237
if isinstance(n, (IfHeader, ElseIfHeader, ElseHeader, ForHeader, End))
244-
or (get_robot_version() >= (5, 0) and isinstance(n, (WhileHeader, TryHeader, ExceptHeader, FinallyHeader)))
238+
or isinstance(n, (WhileHeader, TryHeader, ExceptHeader, FinallyHeader))
245239
):
246240
return []
247241

248-
if get_robot_version() >= (5, 0, 0) and any(
242+
if any(
249243
n
250244
for n in result
251245
if isinstance(n, (Continue, Break))

0 commit comments

Comments
 (0)