Skip to content

Commit

Permalink
fix: update typing
Browse files Browse the repository at this point in the history
  • Loading branch information
adhtruong committed May 8, 2024
1 parent 0032b5e commit 4813c6a
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ repos:
- id: prettier
exclude: ".all-contributorsrc"
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.9.0"
rev: "v1.10.0"
hooks:
- id: mypy
exclude: "test_decimal_constraints|examples/fields/test_example_2|examples/configuration|tools/"
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/decorators/test_example_1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from typing import cast

from polyfactory.decorators import post_generated
from polyfactory.factories import DataclassFactory
Expand All @@ -16,7 +15,7 @@ class DatetimeRangeFactory(DataclassFactory[DatetimeRange]):
@post_generated
@classmethod
def to_dt(cls, from_dt: datetime) -> datetime:
return from_dt + cast(timedelta, cls.__faker__.time_delta("+3d"))
return from_dt + cls.__faker__.time_delta("+3d")


def test_post_generated() -> None:
Expand Down
4 changes: 2 additions & 2 deletions polyfactory/factories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def _create_generic_fn() -> Callable:
# standard library objects
Path: lambda: Path(realpath(__file__)),
Decimal: cls.__faker__.pydecimal,
UUID: lambda: UUID(cls.__faker__.uuid4()),
UUID: lambda: cls.__faker__.uuid4(),
# datetime
datetime: cls.__faker__.date_time_between,
date: cls.__faker__.date_this_decade,
Expand Down Expand Up @@ -777,7 +777,7 @@ def get_field_value_coverage( # noqa: C901
"""
if cls.is_ignored_type(field_meta.annotation):
return [None]
return

for unwrapped_annotation in flatten_annotation(field_meta.annotation):
if unwrapped_annotation in (None, NoneType):
Expand Down
2 changes: 1 addition & 1 deletion polyfactory/factories/beanie_odm_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BeaniePersistenceHandler(Generic[T], AsyncPersistenceProtocol[T]):

async def save(self, data: T) -> T:
"""Persist a single instance in mongoDB."""
return await data.insert() # type: ignore[no-any-return]
return await data.insert() # pyright: ignore[reportGeneralTypeIssues]

async def save_many(self, data: list[T]) -> list[T]:
"""Persist multiple instances in mongoDB.
Expand Down
8 changes: 4 additions & 4 deletions polyfactory/factories/pydantic_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@
from pydantic_core import PydanticUndefined as UndefinedV2
from pydantic_core import to_json

from pydantic.v1 import ( # v1 compat imports
from pydantic.v1 import (
UUID1,
UUID3,
UUID4,
UUID5,
UUID5, # v1 compat imports
AmqpDsn,
AnyHttpUrl,
AnyUrl,
DirectoryPath,
FilePath,
FilePath, # v1 compat imports
HttpUrl,
KafkaDsn,
PostgresDsn,
Expand Down Expand Up @@ -484,7 +484,7 @@ def should_set_field_value(cls, field_meta: FieldMeta, **kwargs: Any) -> bool:

@classmethod
def get_provider_map(cls) -> dict[Any, Callable[[], Any]]:
mapping = {
mapping: dict[Any, Callable[[], Any]] = {
pydantic.ByteSize: cls.__faker__.pyint,
pydantic.PositiveInt: cls.__faker__.pyint,
pydantic.NegativeFloat: lambda: cls.__random__.uniform(-100, -1),
Expand Down
4 changes: 2 additions & 2 deletions polyfactory/value_generators/constrained_dates.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from datetime import date, datetime, timedelta, timezone, tzinfo
from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from faker import Faker
Expand Down Expand Up @@ -38,4 +38,4 @@ def handle_constrained_date(
elif lt:
end_date = lt - timedelta(days=1)

return cast("date", faker.date_between(start_date=start_date, end_date=end_date))
return faker.date_between(start_date=start_date, end_date=end_date)
4 changes: 2 additions & 2 deletions tests/test_random_configuration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from random import Random
from typing import List, Union, cast
from typing import List, Union

import pytest
from faker import Faker
Expand Down Expand Up @@ -65,7 +65,7 @@ class FooFactory(DataclassFactory[Foo]):

@classmethod
def foo(cls) -> int:
return cast(int, cls.__faker__.random_digit())
return cls.__faker__.random_digit()

assert FooFactory.build().foo == RANDINT_MAP[seed]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_recursive_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_recursive_pydantic_models(factory_use_construct: bool) -> None:
factory = ModelFactory.create_factory(PydanticNode)

result = factory.build(factory_use_construct)
assert result.child is _Sentinel, "Default is not used"
assert result.child is _Sentinel, "Default is not used" # type: ignore[comparison-overlap]
assert isinstance(result.union_child, int)
assert result.optional_child is None
assert result.list_child == []
Expand Down

0 comments on commit 4813c6a

Please sign in to comment.