Skip to content

Commit

Permalink
Remove circular testslide imports
Browse files Browse the repository at this point in the history
Summary:
While testing the import cost for some binaries I've found out that testslide is providing an implicit circular import.
An example is `testslide.mock_constructor` importing `testslide`.

This is not necessarily an issue, but I would love to improve it.

In the general case the `testslide` import was needed:
* to make `testslide._importer` available, I'm fixing it via a local import
* to have `testslide.__file__`, even in this case a local import would be sufficient

Differential Revision: D51025476

fbshipit-source-id: b3292393137c01a6a64b29d620dadb001a989260
  • Loading branch information
macisamuele authored and facebook-github-bot committed Nov 10, 2023
1 parent 4fda1fa commit 2a18d5e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions testslide/mock_callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
)
from unittest.mock import Mock

import testslide
from testslide.lib import _validate_return_type, _wrap_signature_and_type_validation
from testslide.strict_mock import StrictMock

Expand Down Expand Up @@ -833,7 +832,8 @@ def __init__(
self._allow_coro = False
self._accept_partial_call = False
if isinstance(target, str):
self._target = testslide._importer(target)
from testslide import _importer
self._target = _importer(target)
else:
self._target = target

Expand Down
4 changes: 2 additions & 2 deletions testslide/mock_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import inspect
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union

import testslide
from testslide.mock_callable import _CallableMock, _MockCallableDSL

from .lib import (
Expand Down Expand Up @@ -331,7 +330,8 @@ def mock_constructor(
raise ValueError("Second argument must be a string with the name of the class.")
_bail_if_private(class_name, allow_private)
if isinstance(target, str):
target = testslide._importer(target)
from testslide import _importer
target = _importer(target)
target_class_id = (id(target), class_name)

if target_class_id in _mocked_target_classes:
Expand Down
8 changes: 4 additions & 4 deletions testslide/patch_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

from typing import Any, Callable, Dict, Tuple

import testslide
from testslide.strict_mock import UndefinedAttribute
from testslide.strict_mock import StrictMock, UndefinedAttribute

from .lib import _bail_if_private, _validate_argument_type
from .patch import _patch
Expand Down Expand Up @@ -55,11 +54,12 @@ def patch_attribute(
_bail_if_private(attribute, allow_private)

if isinstance(target, str):
target = testslide._importer(target)
from testslide import _importer
target = _importer(target)

key = (id(target), attribute)

if isinstance(target, testslide.StrictMock):
if isinstance(target, StrictMock):
if not type_validation:
target.__dict__["_attributes_to_skip_type_validation"].append(attribute)
template_class = target._template
Expand Down
7 changes: 4 additions & 3 deletions testslide/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
Whitespace,
)

import testslide

from . import AggregatedExceptions, Context, Example, Skip, _ExampleRunner

##
Expand Down Expand Up @@ -281,7 +279,10 @@ def print_cyan_dim_underline(self, *values: Any, **kwargs: Any) -> None:


class FailurePrinterMixin(ColorFormatterMixin):
TESTSLIDE_PATH: str = os.path.abspath(os.path.dirname(testslide.__file__))
@property
def TESTSLIDE_PATH(self) -> str:
from testslide import __file__
return os.path.abspath(os.path.dirname(__file__))

def _get_test_module_index(self, tb: traceback.StackSummary) -> Optional[int]:
test_module_paths = [
Expand Down

0 comments on commit 2a18d5e

Please sign in to comment.