Repository navigation
Add some type hinting to the classes inside models. - #4678
Conversation
c11a342 to
cba9584
Compare
b056429 to
17896a5
Compare
993d541 to
4702ed0
Compare
pekkaklarck
left a comment
There was a problem hiding this comment.
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.
|
|
||
| // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. | ||
| // "remoteUser": "root" | ||
| } |
There was a problem hiding this comment.
I'm not exactly sure what this is, but it certainly doesn't belong to this PR.
There was a problem hiding this comment.
This was accidentally added, removing
| 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) |
There was a problem hiding this comment.
- Imports from standard modules should be before internal impots.
- We don't have
typing_extensionsas a dependency so imports from it needs to be surrounded withtry/except ImportError. It only should be used when it has concrete benefits andUnpackdoesn't actually seem to be used anywhere. TypeVardeclarations should be after imports like other module level constants. They should only be used when actually needed.- There should be an empty line before
if TYPE_CHECKING. - Unnecessary parens.
| from robot.model.message import Message | ||
| from robot.model.keyword import Keyword | ||
|
|
||
| class BodyItem(ModelObject): |
There was a problem hiding this comment.
There should be two empty rows before a class declaration on module level.
| MESSAGE = 'MESSAGE' | ||
| type = None | ||
| __slots__ = ['parent'] | ||
| body: "Body" # type hint: no runtime impact |
There was a problem hiding this comment.
This is wrong, all body items don't themselves have body.
| 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 |
There was a problem hiding this comment.
This looks pretty complicated. These classes are only used internally and I believe they don't need type hints.
5687a62 to
7d5dcf3
Compare
@pekkaklarck I think I have gone through all the requested changes inside |
| from typing import Any, TYPE_CHECKING, Dict, List, Union | ||
| if TYPE_CHECKING: | ||
| from robot.model.visitor import SuiteVisitor | ||
|
|
There was a problem hiding this comment.
- Standard module imports should be before internal imports.
- We discussed on #devel about using
'x | y'and'list[x]'style instead ofUnion[x, y]andlist[x]. That would avoid lot offrom typingimports, although in this case we anyway needed to importTYPE_CHECKING.
There was a problem hiding this comment.
Should I switch to stringifying all the type hints in the file?
| metadata: Union[Metadata, None] = None, | ||
| source: Union[Path, None] = None, | ||
| rpa: bool = False, | ||
| parent: Union["TestSuite", None] = None): |
There was a problem hiding this comment.
metadatacan also bedictandsourcecan also bestr.- Using stringly types like
'Metadata | dict | None'would probably be better than usingUnion[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
nameanddocon 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.
|
|
||
| def to_dict(self): | ||
| data = {'name': self.name} | ||
| data: Dict[str, Any] = {'name': self.name} |
There was a problem hiding this comment.
I don't think this helps. The method should get -> dict, though.
There was a problem hiding this comment.
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.
I added few more comments. I mentioned two things to decide in them:
How do you feel about the above? If you agree with me, please update The next question is how to merge only |
7d5dcf3 to
5548088
Compare
|
Changes to
As for, just emerging TestSuite.py, I will roll back and locally stash my changes and use them in a follow-up PR |
3851763 to
0c36024
Compare
0c36024 to
a53e1e9
Compare
|
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. |
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.