Skip to content

Add type information to TestSuite structure #4570

Closed

Description

@pekkaklarck

The TestSuite structure would benefit from more type hints that would allow IDEs to provide better completion support. That would help with writing code using the visitor API and listener API v3, especially if #4569 and #4568 are implemented.

For most parts adding type info ought to be pretty easy and at least those parts should preferably be done already in RF 6.1. It is possible that TestSuite code using our @setter is too dynamic to editors to understand and just adding type info to isn't enough. If that's the case, we need to consider changing such code so that it uses the standard @property.setter instead. That may be too big a task for RF 6.1, though.

Activity

  1. added this to the v6.1 milestone on Dec 19, 2022
  2. added a commit that references this issue on Mar 21, 2023
  3. pekkaklarck commented on Mar 21, 2023

    @pekkaklarck
    MemberAuthor

    I added Mypy compatible type hints to our setter in 810afc8. Based on discussion our Slack with @Serhiy1, that helps also with VSCode that uses pyrigh. Typing can be still enhanced (should at least use setter(Generic[T, V]) instead of setter(Generic[V])), but this is a good start nevertheless.

    Unfortunately the added typing didn't help at all with PyCharm and I submitted issue PY-59658 about that to their tracker. That issue was closed as a duplicate of PY-29523 and there seemed to be also other related issues.

    PyCharm also has problems with descriptors being used together with __slots__. Basically when we use __slots__ and have a descriptor like

    @setter
    def source(self, source) -> Path:
        return Path(source) if isinstance(source, str) else source

    then assignment like self.source = source in __init__ cause errors. I was about to submit an issue about that as well, but it turned out that there's already PY-29770 about that.

    I'm not surprised with PyCharm having bugs, but it's pretty worrying that both PY-29523 and PY-29770 have been reported already five years ago and there hasn't been much interest towards them. We cannot thus expect them to be fixed in the near future. If you are PyCharm user and interested in better experience when developing Robot Framework or tooling using its APIs, I recommend you to vote the referenced issues. You need to log in to Youtrack to vote, but you can authenticate using your GitHub account. I don't have high hopes for voting to help, but there's not much else we can do.

  4. pekkaklarck commented on Apr 4, 2023

    @pekkaklarck
    MemberAuthor

    Type hints added to robot.model.testsuite by @Serhiy1 in 984c2e1 (PR #4678).

  5. added
    acknowledge
    on Apr 4, 2023
  6. added a commit that references this issue on Apr 5, 2023
  7. 4 remaining items

  8. pekkaklarck commented on May 31, 2023

    @pekkaklarck
    MemberAuthor

    @Serhiy1 and I have been working hard to get proper type hinting to the whole TestSuite structure. The challenge has been that we have robot.running.TestSuite structure, robot.result.TestSuite structure, and their base classes in robot.model.TestSuite structure. The common functionality is naturally implemented in robot.model, and getting typing to work with extending classes was challenging. Another challenge is that RF 6.1 still support Python 3.6.

    After all the hard work typing now works pretty well. For example, when the following code is opened in VSCode, it shows correct intellisense for all objects:

    from robot.running import TestSuite as RunningSuite
    from robot.result import TestSuite as ResultSuite
    
    s = RunningSuite()
    t = s.tests.create()
    k = t.body.create_keyword()
    
    s = ResultSuite()
    t = s.tests.create()
    k = t.body.create_keyword()

    As I mentioned above, PyCharm unfortunately has issues with typing and intellisense in the above example doesn't work at all. There's not much else we can do that hope they get the biggest issues fixed at some point.

    After the latest changes @Serhiy1 and I consider TestSuite typing to be in good enough shape for RF 6.1. We already have some ideas how to enhance it in the future when we don't anymore need to care about Python 3.6 and 3.7, though.

    Huge thanks for your help @Serhiy1!

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

    acknowledgeeffort: mediumpriority: highrc 1

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions