# Argument Typing in User Keywords

**URL:** <https://forum.robotframework.org/t/argument-typing-in-user-keywords/7824>\
**Category:** Robot Framework\
**Created:** [18 September 2024 07:58 UTC](https://forum.robotframework.org/t/argument-typing-in-user-keywords/7824 "2024-09-18T07:58:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayzerkan](https://avatars.discourse-cdn.com/v4/letter/k/8e8cbc/32.png) [@kayzerkan](https://forum.robotframework.org/u/kayzerkan)\
**Post date:** [18 September 2024 07:58 UTC](https://forum.robotframework.org/t/argument-typing-in-user-keywords/7824/1 "2024-09-18T07:58:36Z")

</div>

Hello Everyone!

is it possible to explicitly set a type to my arguments in User Keywords? I know its possible to do in libraries but I’d like to avoid that.

So far I’ve checked the official docs and tried googling. It seems its not possible do to so, but i just want to be sure and ask 🙂

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [18 September 2024 09:01 UTC](https://forum.robotframework.org/t/argument-typing-in-user-keywords/7824/2 "2024-09-18T09:01:24Z")

</div>

Hi Volkan,

Start with [Using variables](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#using-variables)

> the normal scalar variable syntax `${var}`, how to use variables in list and dictionary contexts like `@{var}` and `&{var}`, respectively, and how to use environment variables like `%{var}`

By default if you declare a scalar variable it’s a string, e.g.:

```python
    VAR ${stringscalar}= 3

```

if you need a numeric value you can declare them like this:

```python
    VAR ${numericscalar}= ${3}

```

For other variable types you may need to use [Evaluate](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Evaluate) to get python to return the type you need.

If your still let us know what type of variable you need to create and we can try to help,

Dave.

---

<div class="post-metadata">

**Author:** ![kayzerkan](https://avatars.discourse-cdn.com/v4/letter/k/8e8cbc/32.png) [@kayzerkan](https://forum.robotframework.org/u/kayzerkan)\
**Post date:** [18 September 2024 11:19 UTC](https://forum.robotframework.org/t/argument-typing-in-user-keywords/7824/3 "2024-09-18T11:19:28Z")

</div>

Thanks for your answer!

Sadly this is not what I’m looking for. For Example the browserlib has the library keyword click:

`def click(self, selector: str, button: MouseButton = MouseButton.left):`

button is only allowed to be of the enum type MouseButton. I’d like to do that with a user keyword and without going through the hassle of coding it in python.

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [18 September 2024 11:37 UTC](https://forum.robotframework.org/t/argument-typing-in-user-keywords/7824/4 "2024-09-18T11:37:21Z")

</div>

Hi Volkan,

Ah I see what you mean, unfortunately I don’t know the answer, hopefully someone else can help 🤞

However I’m also not sure if you need to do that? for example when I look at the documentation for [Click](https://marketsquare.github.io/robotframework-browser/Browser.html#Click), I see in the examples that you just pass the string `left` or `right` so perhaps try something like this:

```python
***Keywords***
My Click
    [Arguments] ${selector} ${button}=left
    Click ${selector} ${button}

```

Dave.

---

<div class="post-metadata">

**Author:** ![FrancisG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/francisg/32/4247_2.png) [@FrancisG](https://forum.robotframework.org/u/FrancisG)\
**Post date:** [18 September 2024 20:11 UTC](https://forum.robotframework.org/t/argument-typing-in-user-keywords/7824/5 "2024-09-18T20:11:35Z")

</div>

I believe that @pekkaklarck mentioned in this video: [RoboCon 2024 - What’s new in Robot Framework 7.0 and why you should update!](https://www.youtube.com/watch?v=_9dbPs5lQJU), that literals can be used to enforce certain values in keywords implemented in Python, but that it is not yet possible for keyword implemented in RobotFramework.  
Maybe it will come in a future release? If no enhancement request is already open on the topic we should definitly open one!

---

<div class="post-metadata">

**Author:** ![pekkaklarck](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/pekkaklarck/32/1019_2.png) [@pekkaklarck](https://forum.robotframework.org/u/pekkaklarck)\
**Post date:** [19 October 2024 21:54 UTC](https://forum.robotframework.org/t/argument-typing-in-user-keywords/7824/6 "2024-10-19T21:54:59Z")

</div>

This is not possible yet, but we have the following issue about variable type conversion that covers also user keyword argument:

> <https://github.com/robotframework/robotframework/issues/3278>
>
> RF 3.1 added automatic argument conversion with library keywords using function …annotations (#2890), default values (#2932), and some other means. That has worked very well, and it would be nice if something like that would work also with variables defined in test data. We probably could re-use the underlying argument conversion logic which ought to make it relatively easy to implement this. We also got a similar request related to variables passed from the command line #2946.
> 
> Automatically converting numbers like \`42\` and \`3.14\` to actual integers/floats would be handy, even when we already support them as variables like \`${42}\` and \`${3.14}\`. Being able to use datatime objects, bytes, and other types supported by the argument conversion that don't have direct variable alternatives would naturally be even more useful (but also less commonly needed). I would expect this to be most useful with lists and dictionaries and especially with nested list and dicts that cannot easily be constructed in Robot's data.
> 
> My proposed syntax for this functionality is supporting "type postfix" like \`:int\` in variable names like this:
> 
> \`\`\`robotframework
> \*\*\* Variables \*\*\*
> ${COUNT:int} 42
> ${GOOD IDEA:bool} Yes # Converted to Boolean True
> ${ANIMALS:list} \['cat', 'dog', 'horse'\] # Python syntax
> ${NESTED:dict} {'a': 1, 'b': \[{'x': 2}, {'x': 3}\]} # Great with JSON
> \`\`\`
> 
> This syntax is derived from Python's function annotation syntax and also the same syntax that was decided to be used with command line variables in #2946. Created variable names would naturally omit the type suffix and look like \`${COUNT}\` and \`${GOOD IDEA}\`.
> 
> In addition to being available in the variable section, the new syntax should also be available also when creating variable otherwise, including in keyword arguments:
> 
> \`\`\`robotframework
> \*\*\* Keywords \*\*\*
> Example
> \[Arguments\] ${count:int}
> ${nested:dict} = Set Variable {'a': ${count}, 'b': \[{'x': 2}, {'x': 3}\]}
> Set Test Variable ${DATE:datetime} 2019-08-23 12:03:42
> \`\`\`
> 
> There are some design decision still to be made like should \`${X:dict}\` create a \`DotDict\` instance that allows \`${X.key}\` syntax and how to handle possible backwards incompatibility issues. For most parts the design looks pretty good, though, and I think this would be a valuable addition to Robot Framework. If there's enough interest, we can consider adding this already to RF 3.2 scope. To show your interest, vote with :+1: or :-1: and/or add comments.
