Skip to content

Add type information to the visitor API #4569

Closed
Closed

Description

@pekkaklarck

Visitor methods currently look like

class SuiteVisitor:

    def visit_suite(self, suite):
        ...

but we should change them to

from robot.model import TestSuite

class SuiteVisitor:

    def visit_suite(self, suite: TestSuite):
        ...

The main benefit is getting automatic completion by IDEs.

Because visitors can be used both with robot.running and robot.result models, the documentation should mention that concrete visitor implementations may want to import more detailed types:

from robot.api import SuiteVisitor
from robot.result import TestCase

class TestTimePrinter(SuiteVisitor):

    def visit_test(test: TestCase):
        print(f'{test.name}: {test.elapsedtime}')

This ought to be so easy that can be easily done in RF 6.1. Adding needed imports to the top of the module is likely to create a circular import, but we can avoid that by using if TYPE_CHECKING.

Activity

  1. added this to the v6.1 milestone on Dec 19, 2022
  2. pekkaklarck commented on Feb 14, 2023

    @pekkaklarck
    MemberAuthor

    Type hints added by the commit above. Needed to use if TYPE_HINTING with some imports and that then required using forwards references (i.e. strings) like 'TestSuite'instead of actual classes. A bit annoying but not much we can do about that. Having TestSuite import behind if TYPE_HINTING also makes Sphinx to throw some errors due to it getting confused where TestSuite actually is imported from. Seems to be a known issue in Sphinx so nothing we can do about that either. Anyway, the results look pretty good and I believe type hints in public APIs like this are useful.

  3. pekkaklarck commented on Feb 14, 2023

    @pekkaklarck
    MemberAuthor

    Because visitors can be used both with running and result suite models, the types used in type hints are common base classes in robot.model. I added a separate section about type hints to the visitor API docs, and mentioned that when creating visitors, you may want to import more detailed types from robot.running.model or robot.result.model. I initially wrote that you can import them from robot.running or robot.result, but I then noticed that only TestSuite and some other top level classes were exposed on that level and majority actually needs to be imported from the robot.xxx.model submodule.

    The question is, should we expose all running and result model objects directly from robot.running and robot.result package level? My first thought was that it would be a good idea, but after thinking this a bit more I don't see much/any harm in requiring imports from the model submodule. That's a bit more explicit than importing from the package level, and exposing all model objects, there are many, on the package level could add quite a lot of noise.

  4. pekkaklarck commented on Feb 14, 2023

    @pekkaklarck
    MemberAuthor

    @yanne and I discussed about this and agreed that it's enough that running and result model objects are exposed via robot.running.model and robot.result.model, respectively. I'll enhance the docstrings of these modules a bit to make that explicit.

  5. pekkaklarck commented on Feb 16, 2023

    @pekkaklarck
    MemberAuthor

    With listener v3 we need both running and result objects in the signature, and when implementing #4568 it turned out the best way to type that is def start_suite(data: running.TestSuite, result: result.TestSuite). To be able to use this approach with all model objects, it's best to expose all of them via robot.running and robot.result. It also turned out that we already do that with robot.result. Need to do that with robot.running as well. Documentation related to this needs to be updated as well.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions