Introduction
Robot Framework is one of the most widely adopted open-source test automation frameworks in the world, used by thousands of engineering teams-including those in government, healthcare, and regulated industries. While contributing to its codebase, I discovered a critical bug in the TypeInfoParser that caused either a silent UnboundLocalError crash or an infinite loop when parsing certain type parameter expressions like list[int | str]. This post walks through the diagnosis, root cause, and fix.
PR: robotframework/robotframework#5651
The Bug: What Was Happening
When Robot Framework parsed keyword argument type hints containing a pipe (|) character inside nested type parameters-such as list[int | str] - the TypeInfoParser.params() method would either:
- Raise an UnboundLocalError (accessing a variable before assignment), or
- Enter an infinite loop, hanging the test execution silently. This affected any RF user on Python 3.10+ using union types inside collection type hints-a pattern that has become increasingly common as Python's modern type system gains adoption.
Diagnosing the Root Cause
The TypeInfoParser.params() method iterates over tokens to extract type parameters. The issue was a variable used inside the loop that was conditionally assigned but unconditionally referenced in a branch that could be reached before the assignment occurred.
Before (buggy code):
python
def params(self):
# ... token parsing setup ...
while self._has_more():
token = self._next_token()
if token == ']':
break
# current_param used BEFORE assignment in some paths
if token == '|':
current_param.append(token) # UnboundLocalError!
# ... no depth guard = infinite loop risk with unbalanced brackets
Under specific token sequences (particularly | appearing before any parameter boundary), the control flow exited the normal path without initializing the variable. Additionally, the loop's exit condition was not guaranteed to trigger when nested brackets were unbalanced-leading to infinite iteration.
The Fix
The fix involved two critical changes:
- Initialize the variable at the top of the loop scope to prevent UnboundLocalError in all execution paths.
- Add a bracket-depth guard to ensure the loop terminates correctly even with malformed or edge-case input.
After (fixed code):
python
def params(self):
current_param = [] # 1️⃣ Always initialized
depth = 0 # 2️⃣ Depth guard
while self._has_more() and depth < 100: # Safety net
token = self._next_token()
if token == '[':
depth += 1
if token == ']':
depth -= 1
if depth == 0:
break
if token == '|':
current_param.append(token)
# ... rest of logic unchanged
Edge case tests were added to cover:
- | appearing as the first token
- Nested generics with union types like dict[str, int | None]
- Unclosed bracket sequences
**Why This Matters at Scale
Robot Framework has over 9,000 GitHub stars and is actively used in automation pipelines across government agencies, financial institutions, and healthcare systems. A silent hang or crash in the type parser can cause entire test suites to stall without meaningful error output-a serious reliability issue in CI/CD pipelines where unattended execution is the norm. Fixing edge cases in foundational parsing logic strengthens the reliability of a tool that thousands of teams depend on daily.
PR and References
Final Thoughts
Open-source contributions at the parser/core level require understanding both the language runtime (Python's type system evolution) and the framework's internal architecture. This fix is a small but meaningful improvement to a tool relied upon by the global automation community.
If you're using Robot Framework with modern Python type hints, upgrade to the version containing this fix. Star the PR, try it out, or drop a comment if you've hit this bug!
Top comments (0)