Skip to content

Commit 516dd6d

Browse files
authored
chore: improve test imports (#661)
1 parent b98ae5d commit 516dd6d

10 files changed

+24
-34
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
- id: mixed-line-ending
1919
- id: trailing-whitespace
2020
- repo: https://github.com/charliermarsh/ruff-pre-commit
21-
rev: "v0.9.7"
21+
rev: "v0.11.0"
2222
hooks:
2323
- id: ruff
2424
args: ["--fix"]
@@ -28,7 +28,7 @@ repos:
2828
hooks:
2929
- id: codespell
3030
- repo: https://github.com/pycontribs/mirrors-prettier
31-
rev: "v3.5.2"
31+
rev: "v3.5.3"
3232
hooks:
3333
- id: prettier
3434
exclude: ".all-contributorsrc"

polyfactory/factories/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _get_build_context(cls, build_context: BuildContext | None) -> BuildContext:
247247
return copy.deepcopy(build_context)
248248

249249
@classmethod
250-
def _infer_model_type(cls: type[F]) -> type[T] | None:
250+
def _infer_model_type(cls) -> type[T] | None:
251251
"""Return model type inferred from class declaration.
252252
class Foo(ModelFactory[MyModel]): # <<< MyModel
253253
...

polyfactory/factories/pydantic_factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def from_field_info(
203203
metadata.append(m)
204204

205205
constraints = cast(
206-
PydanticConstraints,
206+
"PydanticConstraints",
207207
cls.parse_constraints(metadata=metadata) if metadata else {},
208208
)
209209

@@ -438,7 +438,7 @@ def get_constrained_field_value(
438438
field_build_parameters: Any | None = None,
439439
build_context: BuildContext | None = None,
440440
) -> Any:
441-
constraints = cast(PydanticConstraints, field_meta.constraints)
441+
constraints = cast("PydanticConstraints", field_meta.constraints)
442442
if constraints.pop("json", None):
443443
value = cls.get_field_value(
444444
field_meta, field_build_parameters=field_build_parameters, build_context=build_context

polyfactory/pytest_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __call__(self, factory: type[T], depth: int = 1) -> type[T]:
7878

7979
def _factory_fixture() -> type[T]:
8080
"""The wrapped factory"""
81-
return cast(Type[T], factory)
81+
return cast("Type[T]", factory)
8282

8383
caller_globals = inspect.stack()[depth][0].f_globals
8484
caller_globals[fixture_name] = fixture_register(_factory_fixture)

polyfactory/utils/model_coverage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def next_value(self) -> T:
5454
if value.is_done():
5555
# Only move onto the next instance if the sub-container is done
5656
self._pos += 1
57-
return cast(T, result)
57+
return cast("T", result)
5858

5959
self._pos += 1
6060
return value

polyfactory/value_generators/complex_types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from typing_extensions import is_typeddict
66

77
from polyfactory.constants import INSTANTIABLE_TYPE_MAPPING, PY_38
8-
from polyfactory.field_meta import FieldMeta
98
from polyfactory.utils.model_coverage import CoverageContainer
109

1110
if TYPE_CHECKING:
1211
from polyfactory.factories.base import BaseFactory, BuildContext
12+
from polyfactory.field_meta import FieldMeta
1313

1414

1515
def handle_collection_type(
@@ -42,7 +42,7 @@ def handle_collection_type(
4242

4343
if issubclass(container_type, MutableMapping) or is_typeddict(container_type):
4444
for key_field_meta, value_field_meta in cast(
45-
Iterable[Tuple[FieldMeta, FieldMeta]],
45+
"Iterable[Tuple[FieldMeta, FieldMeta]]",
4646
zip(field_meta.children[::2], field_meta.children[1::2]),
4747
):
4848
key = factory.get_field_value(
@@ -112,7 +112,7 @@ def handle_collection_type_coverage( # noqa: C901, PLR0911
112112

113113
if issubclass(container_type, MutableMapping) or is_typeddict(container_type):
114114
for key_field_meta, value_field_meta in cast(
115-
Iterable[Tuple[FieldMeta, FieldMeta]],
115+
"Iterable[Tuple[FieldMeta, FieldMeta]]",
116116
zip(field_meta.children[::2], field_meta.children[1::2]),
117117
):
118118
key = CoverageContainer(factory.get_field_value_coverage(key_field_meta, build_context=build_context))

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ markers = [
162162
# Marks tests that use `attrs` library
163163
"attrs",
164164
]
165+
asyncio_default_fixture_loop_scope = "function"
165166

166167
[tool.coverage.report]
167168
exclude_lines = [

tests/sqlalchemy_factory/test_sqlalchemy_factory_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ class SqlaModel(Base):
9292
mssql_json: orm.Mapped[Dict] = orm.mapped_column(type_=MSSQL_JSON)
9393

9494
multible_pg_json_type: orm.Mapped[Dict] = orm.mapped_column(
95-
type_=MutableDict.as_mutable(JSON(astext_type=Text())) # type: ignore[no-untyped-call]
95+
type_=MutableDict.as_mutable(JSON(astext_type=Text()))
9696
)
9797
multible_pg_jsonb_type: orm.Mapped[Dict] = orm.mapped_column(
98-
type_=MutableDict.as_mutable(JSONB(astext_type=Text())) # type: ignore[no-untyped-call]
98+
type_=MutableDict.as_mutable(JSONB(astext_type=Text()))
9999
)
100100
multible_common_json_type: orm.Mapped[Dict] = orm.mapped_column(type_=MutableDict.as_mutable(types.JSON()))
101101
multible_mysql_json: orm.Mapped[Dict] = orm.mapped_column(type_=MutableDict.as_mutable(MYSQL_JSON()))

tests/test_beanie_factory.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,11 @@
33

44
import pymongo
55
import pytest
6+
from beanie import Document, Link, init_beanie
7+
from beanie.odm.fields import Indexed, PydanticObjectId
8+
from mongomock_motor import AsyncMongoMockClient
69

7-
try:
8-
from beanie import Document, Link, init_beanie
9-
from beanie.odm.fields import Indexed, PydanticObjectId
10-
from mongomock_motor import AsyncMongoMockClient
11-
12-
from polyfactory.factories.beanie_odm_factory import BeanieDocumentFactory
13-
except ImportError:
14-
pytest.importorskip("beanie")
15-
16-
BeanieDocumentFactory = None # type: ignore
17-
Document = None # type: ignore
18-
Link = None # type: ignore
19-
init_beanie = None # type: ignore
20-
Indexed = None # type: ignore
21-
PydanticObjectId = None # type: ignore
10+
from polyfactory.factories.beanie_odm_factory import BeanieDocumentFactory
2211

2312

2413
@pytest.fixture()

tests/test_odmantic_factory.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import pytest
77
from bson import ObjectId
88

9-
from polyfactory.decorators import post_generated
9+
import pydantic
10+
11+
if not pydantic.VERSION.startswith("1"):
12+
pytest.skip("only for pydantic v1", allow_module_level=True)
1013

11-
try:
12-
from odmantic import AIOEngine, EmbeddedModel, Model
14+
from odmantic import AIOEngine, EmbeddedModel, Model
1315

14-
from polyfactory.factories.odmantic_odm_factory import OdmanticModelFactory
15-
except ImportError:
16-
AIOEngine, EmbeddedModel, Model, OdmanticModelFactory = None, None, None, None # type: ignore
17-
pytest.importorskip("odmantic")
16+
from polyfactory.decorators import post_generated
17+
from polyfactory.factories.odmantic_odm_factory import OdmanticModelFactory
1818

1919

2020
class OtherEmbeddedDocument(EmbeddedModel): # type: ignore

0 commit comments

Comments
 (0)