Skip to content

Type hints convert arguments

Chapter lab: Python you needch17-python-basics.zip · 4 KB

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:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch17-python-basics.zip && unzip -q ch17-python-basics.zip && rm ch17-python-basics.zip

Open 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:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 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”
Edit

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)
Run

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} 60

Run it:

Terminal window
uv run robot checks/pricing.robot
Terminal window
robot checks/pricing.robot
Terminal window
poetry run robot checks/pricing.robot

Done 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.

  1. A parameter annotated price: float tells Robot Framework to…

    Show the answer

    Answer: convert the incoming text to a float — Type hints drive argument conversion, so a suite can pass 80 and the function receives 80.0.

  2. Without a type hint, 80 passed to a function arrives as…

    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: float drive Robot Framework's argument conversion.

All terms in the glossary →