Skip to content

Commit

Permalink
Typing tuning
Browse files Browse the repository at this point in the history
- Guard all imports in `robot.model.visitor` with `if TYPE_CHECKING`
  to avoid cyclic imports with modules needing to import `SuiteVisitor`.

- Modules anyway needing `if TYPE_CHECKING`, and only using `SuiteVisitor`
  for typing, still import it conditionally. We may consider using
  `if TYPE_CHECKING` with all imports needed only typing purposes.

- `dict[str, Any]` typing wtih `to_dict`. Also with `data` used internally
  to avoid errors with VScode.

- Use `from .mod import X` insted of `from robot.mod import X` style in
  imports consisently. `visitor` is an exception because importing
  everything from `robot.model` in one go is just too convenient.
  • Loading branch information
pekkaklarck committed Apr 6, 2023
1 parent 2a57c47 commit 30ebdb6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 70 deletions.
12 changes: 6 additions & 6 deletions src/robot/model/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Sequence, TYPE_CHECKING, Type
from typing import Any, Sequence, Type, TYPE_CHECKING
import warnings

from .body import Body, BodyItem
from .itemlist import ItemList

if TYPE_CHECKING:
from robot.model.testcase import TestCase
from robot.model.testsuite import TestSuite
from robot.model.visitor import SuiteVisitor
from .testcase import TestCase
from .testsuite import TestSuite
from .visitor import SuiteVisitor


@Body.register
Expand Down Expand Up @@ -64,8 +64,8 @@ def __str__(self) -> str:
parts = list(self.assign) + [self.name] + list(self.args)
return ' '.join(str(p) for p in parts)

def to_dict(self) -> dict:
data: 'dict[str,list|str]' = {'name': self.name}
def to_dict(self) -> 'dict[str, Any]':
data: 'dict[str, Any]' = {'name': self.name}
if self.args:
data['args'] = list(self.args)
if self.assign:
Expand Down
11 changes: 5 additions & 6 deletions src/robot/model/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

from pathlib import Path
from typing import Iterable, Mapping, Sequence, TYPE_CHECKING, Type
from typing import Any, Iterable, Mapping, Sequence, Type, TYPE_CHECKING

from robot.utils import setter

Expand All @@ -26,8 +26,8 @@
from .tags import Tags

if TYPE_CHECKING:
from robot.model.testsuite import TestSuite
from robot.model.visitor import SuiteVisitor
from .testsuite import TestSuite
from .visitor import SuiteVisitor


class TestCase(ModelObject):
Expand Down Expand Up @@ -179,9 +179,8 @@ def visit(self, visitor: 'SuiteVisitor'):
def __str__(self) -> str:
return self.name

def to_dict(self) -> dict:
data = {}
data['name'] = self.name
def to_dict(self) -> 'dict[str, Any]':
data: 'dict[str, Any]' = {'name': self.name}
if self.doc:
data['doc'] = self.doc
if self.tags:
Expand Down
13 changes: 5 additions & 8 deletions src/robot/model/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from collections.abc import Mapping
from pathlib import Path
from typing import Iterator, Sequence, Type, TYPE_CHECKING
from typing import Any, Iterator, Sequence, Type

from robot.utils import setter

Expand All @@ -28,9 +28,7 @@
from .modelobject import ModelObject
from .tagsetter import TagSetter
from .testcase import TestCase, TestCases

if TYPE_CHECKING:
from robot.model.visitor import SuiteVisitor
from .visitor import SuiteVisitor


class TestSuite(ModelObject):
Expand Down Expand Up @@ -303,16 +301,15 @@ def remove_empty_suites(self, preserve_direct_children: bool = False):
"""Removes all child suites not containing any tests, recursively."""
self.visit(EmptySuiteRemover(preserve_direct_children))

def visit(self, visitor: 'SuiteVisitor'):
def visit(self, visitor: SuiteVisitor):
""":mod:`Visitor interface <robot.model.visitor>` entry-point."""
visitor.visit_suite(self)

def __str__(self) -> str:
return self.name

def to_dict(self) -> dict:
data = {}
data['name'] = self.name
def to_dict(self) -> 'dict[str, Any]':
data: 'dict[str, Any]' = {'name': self.name}
if self.doc:
data['doc'] = self.doc
if self.metadata:
Expand Down

0 comments on commit 30ebdb6

Please sign in to comment.