Skip to content

Support mypy analysis by adding NoReturn annotation to control flow methods #5361

Closed

Description

@robinmackaij

Currently, the BuildIn keywords that always raise have no return annotation. Due to this, mypy cannot properly determine the flow of the code. Quick adaptation (from openapitools) as an example:

def example() -> Any
    cached_parser = PARSER_CACHE.get(self._source, None)
    if cached_parser:
        return cached_parser

    try:
        parser = ResolvingParser(self._source)

        if parser.specification is None:  # pragma: no cover
            BuiltIn().fatal_error(
                "Source was loaded, but no specification was present after parsing."
            )
        return parser

    except ResolutionError as exception:
        BuiltIn().fatal_error(
            f"ResolutionError while trying to load openapi spec: {exception}"
        )
    except ValidationError as exception:
        BuiltIn().fatal_error(
            f"ValidationError while trying to load openapi spec: {exception}"
        )

mypy flags this code as Missing return statement [return]. This is due to mypy not being aware that the BuildIn().fatal_error() calls in the except clauses will always raise.

Adding NoReturn annotations to fail(), fatal_error(), skip() and pass_execution() will allow mypy to properly infer the control flow in (library) code that uses these BuildIn methods.

Activity

  1. pekkaklarck commented on Mar 11, 2025

    @pekkaklarck
    Member

    First off all, I don't think it's a good idea to use BuiltIn.fatal_error() and other such methods in your own library. The reason is that we have FatalError and other such exceptions exposed via the robot.api.exceptions module for exact that purpose.

    Due to the above reason, I don't think adding return type annotations just for the proposed usage is worth the effort. That said, adding argument and return value typing to all BuiltIn keywords would be great. That would add useful information to Libdoc outputs and it would also allow removing conversion code from various keywords. Are you interested to look at that?

    Even better would be adding type hints to all standard libraries, but doing everything in one go isn't a good idea. It might be a good idea to start from some smaller and simpler library than BuiltIn, though.

  2. robinmackaij commented on Mar 12, 2025

    @robinmackaij
    ContributorAuthor
  3. pekkaklarck commented on Mar 12, 2025

    @pekkaklarck
    Member

    We certainly don't need to add type hints to all standard libraries in one go, but I'd like to avoid partially annotated libraries. Because both Robot and type checkers support stringified annotations like 'int | float' and 'list[int]', I doubt there are much problems other that some libraries are rather big so there's quite a lot of work. Or do you have some particular issues in mind @robinmackaij?

    If we decide that adding type hints to keywords is the way forward, I believe this issue and the associated PR can be closed and a new issue submitted about type hints. If the goal would be adding type hints to all standard libraries in RF 7.3, we could just have one issue listing all libraries. Each library could then get its own PR and the work could easily be shared with different community members. If you @robinmackaij would be interested to lead these efforts, it would be awesome. I can ask about that in the #devel channel if needed as well and would naturally review and merge PRs.

  4. robinmackaij commented on Mar 14, 2025

    @robinmackaij
    ContributorAuthor

    I decided to move to 3.10 as to not be restricted to stringified annotations, but since they support the | operator, I would expect we can cover almost everything; we shouldn't need complex generics etc. for library / keyword annotations.

    On the openapitools repo I see the option to create sub-issues...not sure if that's available for all GitHub repos or an opt-in, but I guess that could work well for an issue for the annotations effort with a sub-issue for each library.

    I can take the lead in this, for as far as that's needed; it seems like an effort that can be split up easily enough. A message in #devel would be good.

  5. pekkaklarck commented on Mar 14, 2025

    @pekkaklarck
    Member

    We most likely don't need anything more complicated than unions, but stringified annotations support also parameterized generics.

    We also have sub issues here. It's a new addition and I haven't yet looked how it works, but it could be useful in this case. Alternatively we can have a simple task list.

    I'll close this issue now. Can you @robinmackaij submit an issue about adding type hints to standard libraries? You could also send the initial message to #devel so we get started.

  6. pekkaklarck commented on Jan 13, 2026

    @pekkaklarck
    Member

    This was actually done as part of adding type hints to standard libraries (#5373).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions