Type hints convert arguments
Chapter lab: Python you need
This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.
Download ch17-python-basics.zip
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch17-python-basics.zip && unzip -q ch17-python-basics.zip && rm ch17-python-basics.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch17-python-basics.zip -OutFile ch17-python-basics.zip; Expand-Archive ch17-python-basics.zip -DestinationPath .; Remove-Item ch17-python-basics.zipOpen the folder in VS Code with code rfe-ch17-python-basics (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 3.13 or newer is required; uv installs it when it is missing.
Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.
What's inside
Directoryrfe-ch17-python-basics/
Directory.vscode/
- extensions.json
Directorychecks/
- smoke.robot
Directorylibraries/
- README.md
Directoryvariables/
- README.md
- .gitignore
- .python-version
- LAB.md
- pyproject.toml
- README.md
- requirements.txt
- uv.lock
In the first lesson you passed ${42} to send an integer, because arguments
arrive from a suite as text by default. There is a cleaner way: type
hints.
When a function parameter is annotated — price: float — Robot Framework
converts the incoming argument for you. A suite can pass a plain 80 and your
function receives the float 80.0. Type hints also document intent and make the
keyword’s signature clear in the generated docs (next chapter).
Task 1 — Let Robot Framework convert the numbers
Section titled “Task 1 — Let Robot Framework convert the numbers”Goal: create libraries/pricing.py with a function
discounted_price(price: float, percent: float) -> float that returns the price
after a percentage discount, rounded to two decimals.
Here is the same shape over different data — adding tax instead of taking a discount. Adapt it to subtract a discount.
Worked example — adapt it, don’t copy it
def with_tax(amount: float, rate: float) -> float: return round(amount * (1 + rate / 100), 2)Done when
libraries/pricing.py defines a function discounted_price.
Hint 1
Annotate both parameters with : float so Robot Framework converts the incoming
text to numbers — otherwise price * ... would try to do maths on strings.
Hint 2
Take the percentage off and round: return round(price * (1 - percent / 100), 2).
Solution
def discounted_price(price: float, percent: float) -> float: return round(price * (1 - percent / 100), 2)Task 2 — Import it and run it green
Section titled “Task 2 — Import it and run it green”Create checks/pricing.robot. Notice the arguments are plain 80 and 25 —
no ${...} — because the type hints convert them.
*** Settings ***Library ../libraries/pricing.py
*** Test Cases ***Discount Is Applied ${net}= Discounted Price 80 25 Should Be Equal As Numbers ${net} 60Run it:
uv run robot checks/pricing.robotrobot checks/pricing.robotpoetry run robot checks/pricing.robotDone when
the test Discount Is Applied passes.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
A parameter annotated
price: floattells Robot Framework to…Type hints drive argument conversion, so a suite can pass
80and the function receives80.0.Show the answer
Answer: convert the incoming text to a float — Type hints drive argument conversion, so a suite can pass
80and the function receives80.0.Without a type hint,
80passed to a function arrives as…Unannotated arguments stay as text; maths on them fails until converted or hinted.
Show the answer
Answer: the string
"80"— Unannotated arguments stay as text; maths on them fails until converted or hinted.
Key terms
- type hintsconcept
- Annotations like
price: floatdrive Robot Framework's argument conversion.