robotframework / robotframework/robotframework

Boolean values are not converted to integers if argument is typed like `arg: int`

Open
#5,643 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backwards incompatible effort: small priority: medium
Dominant language
Python
Stars
11.9k
Forks
2.6k
Avg merge
1d 18h
Merged PRs (30d)
10

Description

This was originally reported as part of #5622. As discussed in that issue, the reason is that in Python bool is a sub type of int (i.e. isinstance(True, int) is true) and Robot argument conversion in general allows sub types to be used. This Python design is quite widely considered a mistake and it certainly is confusing in Robot's context. Changing the argument conversion behavior could thus make sense.

A problem with changing the behavior is that changes like this are backwards incompatible. It can be argued, and I would agree, that if you have a keyword like

def example(arg: int):
    ...

it's very unlikely that you would expect the argument to be a Boolean. If what would be the case, changing the implementation to more explicit

def example(arg: int | bool):
    ...

would preserve the current functionality.

The change would also affect the case where argument conversion is done based on the default value type like

def example(arg=1):
    ...

In this case preserving the current behavior would require adding type hints, which may be a bigger ask than updating current type hints. It is, however, somewhat unlikely that there are lot of cases where argument conversion is done based on an integer type hint and the value would be passed in as a Boolean.

Overall I consider backwards incompatibility issues so small with this particular change, that I don't see big problems doing it in a feature released (i.e. RF 7.5) instead of waiting for a major version (i.e. RF 8.0) where backwards incompatible changes in general are tolerated more. That said, I don't think Boolean values are that often passed to functions expecting integer values and I doubt that such usage even causes problems too often. Thus waiting for RF 8.0 to get this small issue fixed shouldn't be a big problem either.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Robot's argument conversion for parameters annotated as int and for arguments whose default value is an integer. Add coverage for Boolean values passed to both cases, then verify that explicit int-or-bool annotations preserve Boolean conversion while plain integer expectations do not.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.