33from __future__ import annotations
44
55from collections import defaultdict
6- from typing import TYPE_CHECKING
6+ from typing import Any
77
88import pytest
9+ from _pytest .config import PytestPluginManager
10+ from _pytest .fixtures import FixtureRequest , SubRequest
11+ from _pytest .nodes import Item
12+ from _pytest .python import Metafunc
13+ from factory .base import Factory
914
1015from .compat import getfixturedefs
11-
12- if TYPE_CHECKING :
13- from typing import Any
14-
15- from _pytest .config import PytestPluginManager
16- from _pytest .fixtures import FixtureRequest , SubRequest
17- from _pytest .nodes import Item
18- from _pytest .python import Metafunc
19- from factory import Factory
20-
21- from .fixture import DeferredFunction
16+ from .fixture import DeferredFunction
2217
2318
2419class CycleDetected (Exception ):
@@ -30,12 +25,12 @@ class Request:
3025
3126 def __init__ (self ) -> None :
3227 """Create pytest_factoryboy request."""
33- self .deferred : list [list [DeferredFunction ]] = []
28+ self .deferred : list [list [DeferredFunction [ object ] ]] = []
3429 self .results : dict [str , dict [str , Any ]] = defaultdict (dict )
35- self .model_factories : dict [str , type [Factory ]] = {}
36- self .in_progress : set [DeferredFunction ] = set ()
30+ self .model_factories : dict [str , type [Factory [ object ] ]] = {}
31+ self .in_progress : set [DeferredFunction [ object ] ] = set ()
3732
38- def defer (self , functions : list [DeferredFunction ]) -> None :
33+ def defer (self , functions : list [DeferredFunction [ object ] ]) -> None :
3934 """Defer post-generation declaration execution until the end of the test setup.
4035
4136 :param functions: Functions to be deferred.
@@ -51,6 +46,8 @@ def get_deps(self, request: SubRequest, fixture: str, deps: set[str] | None = No
5146 if fixture == "request" :
5247 return deps
5348
49+ assert request ._pyfuncitem .parent is not None , "Request must have a parent item."
50+
5451 fixturedefs = getfixturedefs (request ._fixturemanager , fixture , request ._pyfuncitem .parent )
5552 for fixturedef in fixturedefs or []:
5653 for argname in fixturedef .argnames :
@@ -67,7 +64,9 @@ def get_current_deps(self, request: FixtureRequest | SubRequest) -> set[str]:
6764 request = request ._parent_request
6865 return deps
6966
70- def execute (self , request : SubRequest , function : DeferredFunction , deferred : list [DeferredFunction ]) -> None :
67+ def execute (
68+ self , request : SubRequest , function : DeferredFunction [object ], deferred : list [DeferredFunction [object ]]
69+ ) -> None :
7170 """Execute deferred function and store the result."""
7271 if function in self .in_progress :
7372 raise CycleDetected ()
@@ -114,9 +113,7 @@ def factoryboy_request() -> Request:
114113 return Request ()
115114
116115
117- # type ignored because pluggy v1.0.0 has no type annotations:
118- # https://github.com/pytest-dev/pluggy/issues/191
119- @pytest .hookimpl (tryfirst = True ) # type: ignore[misc]
116+ @pytest .hookimpl (tryfirst = True )
120117def pytest_runtest_call (item : Item ) -> None :
121118 """Before the test item is called."""
122119 # TODO: We should instead do an `if isinstance(item, Function)`.
0 commit comments