Skip to content

Add some type hinting to the classes inside models. - #4678

Merged
pekkaklarck merged 1 commit into
robotframework:masterfrom
Serhiy1:dev/type-hinting
Apr 4, 2023
Merged

pekkaklarck merged 1 commit into
robotframework:masterfrom
Serhiy1:dev/type-hinting

Conversation

@Serhiy1

@Serhiy1 Serhiy1 commented Mar 10, 2023 •

Copy link
Copy Markdown
Contributor

Completes type hinting in the test suite structure #4570

The files that got type hints added, where mainly files that I have interacted with as an end user.

The PR is fully Compatible with Compatible with python 3.6 onwards.

@Serhiy1
Serhiy1 force-pushed the dev/type-hinting branch 13 times, most recently from c11a342 to cba9584 Compare March 19, 2023 15:46
@Serhiy1
Serhiy1 force-pushed the dev/type-hinting branch 7 times, most recently from b056429 to 17896a5 Compare March 21, 2023 18:09
@Serhiy1
Serhiy1 marked this pull request as ready for review March 21, 2023 20:50
@Serhiy1
Serhiy1 force-pushed the dev/type-hinting branch 8 times, most recently from 993d541 to 4702ed0 Compare March 22, 2023 20:27

@pekkaklarck pekkaklarck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work but lot of things to enhance still. General problems:

  • Some style violations especially related to imports.
  • In some case types added to attributes that don't even exist.
  • Return types and also parameter types missing in many places in public APIs.
  • Some of the internal type hints look so complicated that they may not be useful.

Fixing everything in this PR and then re-reviewing is a pretty big task. I believe it would be best to start from a single module like robot.model.testsuite and type it thoroughly. Once we get that done, we set standard that can then be followed when typing other modules.

Comment thread .devcontainer/devcontainer.json Outdated

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure what this is, but it certainly doesn't belong to this PR.

@Serhiy1 Serhiy1 Apr 4, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was accidentally added, removing

Comment thread src/robot/model/body.py Outdated
from typing_extensions import Unpack
C = TypeVar("C", bound="BodyItem")
if TYPE_CHECKING:
from robot.model.control import (Break, Continue, Return, Try, If, While, For, Error)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Imports from standard modules should be before internal impots.
  2. We don't have typing_extensions as a dependency so imports from it needs to be surrounded with try/except ImportError. It only should be used when it has concrete benefits and Unpack doesn't actually seem to be used anywhere.
  3. TypeVar declarations should be after imports like other module level constants. They should only be used when actually needed.
  4. There should be an empty line before if TYPE_CHECKING.
  5. Unnecessary parens.

Comment thread src/robot/model/body.py
from robot.model.message import Message
from robot.model.keyword import Keyword

class BodyItem(ModelObject):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be two empty rows before a class declaration on module level.

Comment thread src/robot/model/body.py Outdated
MESSAGE = 'MESSAGE'
type = None
__slots__ = ['parent']
body: "Body" # type hint: no runtime impact

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, all body items don't themselves have body.

Comment thread src/robot/model/body.py Outdated
continue_class: Union[None, Type["Continue"]] = None
break_class: Union[None, Type["Break"]] = None
message_class: Union[None, Type["Message"]] = None
error_class: Union[None, Type["Error"]] = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty complicated. These classes are only used internally and I believe they don't need type hints.

Comment thread src/robot/model/testsuite.py Resolved
Comment thread src/robot/model/testsuite.py Resolved
Comment thread src/robot/model/testsuite.py Resolved
Comment thread src/robot/model/testsuite.py Resolved
Comment thread src/robot/model/visitor.py Resolved
@Serhiy1
Serhiy1 force-pushed the dev/type-hinting branch 3 times, most recently from 5687a62 to 7d5dcf3 Compare April 4, 2023 08:47
@Serhiy1

Serhiy1 commented Apr 4, 2023

Copy link
Copy Markdown
Contributor Author

Fixing everything in this PR and then re-reviewing is a pretty big task. I believe it would be best to start from a single module like robot.model.testsuite and type it thoroughly. Once we get that done, we set standard that can then be followed when typing other modules.

@pekkaklarck I think I have gone through all the requested changes inside robot.model.testsuite

Comment thread src/robot/model/testsuite.py Outdated
from typing import Any, TYPE_CHECKING, Dict, List, Union
if TYPE_CHECKING:
from robot.model.visitor import SuiteVisitor

@pekkaklarck pekkaklarck Apr 4, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Standard module imports should be before internal imports.
  • We discussed on #devel about using 'x | y' and 'list[x]' style instead of Union[x, y] and list[x]. That would avoid lot of from typing imports, although in this case we anyway needed to import TYPE_CHECKING.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I switch to stringifying all the type hints in the file?

Comment thread src/robot/model/testsuite.py Outdated
metadata: Union[Metadata, None] = None,
source: Union[Path, None] = None,
rpa: bool = False,
parent: Union["TestSuite", None] = None):

@pekkaklarck pekkaklarck Apr 4, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • metadata can also be dict and source can also be str.
  • Using stringly types like 'Metadata | dict | None' would probably be better than using Union[Metadata, dict, None]. That's also shorter.
  • Please use single quotes instead of double quotes.
  • We need to decide how to handle long signatures like this one. Splitting all parameters to own lines would be a simple solution, but it should be done consistently (i.e. also name and doc on own lines) and such formatting takes lot of vertical space. I'd try having multiple params per line, but we can make a final decision a bit later. Notice that we use 88 as the maximum line length.

Comment thread src/robot/model/testsuite.py Outdated

def to_dict(self):
data = {'name': self.name}
data: Dict[str, Any] = {'name': self.name}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this helps. The method should get -> dict, though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the type hint to suppress some reported type errors from VScode, with the first assignment, it assumes that keys and values for the dict are always strings raising errors all over the place.

Will remove though.

@pekkaklarck

pekkaklarck commented Apr 4, 2023 •

Copy link
Copy Markdown
Member

@pekkaklarck I think I have gone through all the requested changes inside robot.model.testsuite

I added few more comments. I mentioned two things to decide in them:

  1. Should we use stringified types like 'x | y' and 'list[int]' or use Union, List, etc? We discussed this on #devel and most people seemed to prefer the former. I also like that style more, especially with unions, and it avoids lot of from typing imports.
  2. How to format long signatures? Having each parameter on its own line would be an easy solution, but such style takes lot of vertical space. I believe we should try fitting multiple params per line.

How do you feel about the above? If you agree with me, please update testsuite to use such style. We can then see how it feels and decide should we use that style also with other files.

The next question is how to merge only testsuite now. I think I cannot, at least here on web, merge only one file, so a new PR would probably be easiest.

@Serhiy1
Serhiy1 force-pushed the dev/type-hinting branch from 7d5dcf3 to 5548088 Compare April 4, 2023 14:22
@Serhiy1

Serhiy1 commented Apr 4, 2023

Copy link
Copy Markdown
Contributor Author

Changes to TestSuite.py

  • Re-ordered imports
  • Switched to stringified type hints
  • Changed init signature to maximise horizontal character count before newlines

As for, just emerging TestSuite.py, I will roll back and locally stash my changes and use them in a follow-up PR

@Serhiy1
Serhiy1 force-pushed the dev/type-hinting branch 2 times, most recently from 3851763 to 0c36024 Compare April 4, 2023 14:41
@Serhiy1
Serhiy1 force-pushed the dev/type-hinting branch from 0c36024 to a53e1e9 Compare April 4, 2023 14:42
@pekkaklarck

Copy link
Copy Markdown
Member

Great! There's still one issue with two few lines before a class but I can fix that myself. I'll also experiment with typing otherwise.

@pekkaklarck
pekkaklarck merged commit 984c2e1 into robotframework:master Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants