Skip to content

Commit

Permalink
chore: scopey fix
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 6, 2024
1 parent 6d9670c commit 0e4a4f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
19 changes: 9 additions & 10 deletions src/ape/pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import Iterable, Iterator, Mapping
from dataclasses import dataclass, field
from fnmatch import fnmatch
from functools import cached_property
from functools import cached_property, singledispatchmethod
from typing import TYPE_CHECKING, ClassVar, Optional

import pytest
Expand Down Expand Up @@ -313,21 +313,15 @@ def parametrized(self) -> dict[str, list]:
def _arg2fixturedefs(self) -> Mapping:
return self._item.session._fixturemanager._arg2fixturedefs

@singledispatchmethod
def __setitem__(self, key, value):
# NOTE: Not using singledispatchmethod because it requires
# types at runtime.
if isinstance(key, Scope):
self.__setitem_scope(key, value)
elif isinstance(key, str):
self.__setitem_str(key, value)
elif isinstance(key, int):
self.__setitem_int(key, value)

raise NotImplementedError(type(key))

@__setitem__.register
def __setitem_int(self, key: int, value: list[str]):
super().__setitem__(Scope(key), value)

@__setitem__.register
def __setitem_str(self, key: str, value: list[str]):
for scope in Scope:
if f"{scope}" == key:
Expand All @@ -336,9 +330,11 @@ def __setitem_str(self, key: str, value: list[str]):

raise KeyError(key)

@__setitem__.register
def __setitem_scope(self, key: Scope, value: list[str]):
super().__setitem__(key, value)

@singledispatchmethod
def __getitem__(self, key):
# NOTE: Not using singledispatchmethod because it requires
# types at runtime.
Expand All @@ -351,16 +347,19 @@ def __getitem__(self, key):

raise NotImplementedError(type(key))

@__getitem__.register
def __getitem_int(self, key: int) -> list[str]:
return super().__getitem__(Scope(key))

@__getitem__.register
def __getitem_str(self, key: str) -> list[str]:
for scope in Scope:
if f"{scope}" == key:
return super().__getitem__(scope)

raise KeyError(key)

@__getitem__.register
def __getitem_scope(self, key: Scope) -> list[str]:
return super().__getitem__(key)

Expand Down
5 changes: 1 addition & 4 deletions src/ape/pytest/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ape.exceptions import ConfigError, ProviderNotConnectedError
from ape.logging import LogLevel
from ape.pytest.utils import Scope
from ape.utils.basemodel import ManagerAccessMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -172,8 +173,6 @@ def pytest_runtest_setup(self, item):
self._setup_isolation(item)

def _setup_isolation(self, item):
from ape.pytest.utils import Scope

fixtures = self.fixture_manager.get_fixtures(item)
for scope in (Scope.SESSION, Scope.PACKAGE, Scope.MODULE, Scope.CLASS):
if not (
Expand Down Expand Up @@ -274,8 +273,6 @@ def pytest_fixture_post_finalizer(self, fixturedef, request):
self.fixture_manager.add_fixture_info(fixture_name, teardown_block=block_number)

def _track_fixture_blocks(self, fixture_name: str) -> bool:
from ape.pytest.utils import Scope

if not self.fixture_manager.is_custom(fixture_name):
return False

Expand Down

0 comments on commit 0e4a4f0

Please sign in to comment.