From 3bfff9035e9907358151e019bc5be0bb18eafcfc Mon Sep 17 00:00:00 2001 From: Flavio Curella <89607+fcurella@users.noreply.github.com> Date: Mon, 5 Jun 2023 10:04:25 -0500 Subject: [PATCH] refactor typing (#73) --- .github/workflows/python-package.yml | 2 +- django_fakery/blueprint.py | 91 ++++++-- django_fakery/faker_factory.py | 314 ++++++++++++++++++++------- django_fakery/field_mappings.py | 4 +- django_fakery/lazy.py | 3 +- django_fakery/shortcuts.py | 15 +- django_fakery/utils.py | 14 +- 7 files changed, 314 insertions(+), 129 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index fe6a08c..f894ad2 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.8", "3.9", "3.10", "3.11", 'pypy-3.8'] + python-version: ["3.8", "3.9", "3.10", "3.11", 'pypy-3.9'] django: - "Django>=3.2,<3.3" - "Django>=4.0,<4.1" diff --git a/django_fakery/blueprint.py b/django_fakery/blueprint.py index 8f84bb1..5de7ff2 100644 --- a/django_fakery/blueprint.py +++ b/django_fakery/blueprint.py @@ -7,8 +7,14 @@ class Blueprint(Generic[T]): - def __init__(self, model, fields=None, pre_save=None, post_save=None, seed=None): - # type: (T, Opt[FieldMap], Opt[SaveHooks], Opt[SaveHooks], Opt[Seed]) -> None + def __init__( + self, + model: T, + fields: Opt[FieldMap] = None, + pre_save: Opt[SaveHooks] = None, + post_save: Opt[SaveHooks] = None, + seed: Opt[Seed] = None, + ): from .faker_factory import factory self.factory = factory @@ -21,8 +27,7 @@ def __init__(self, model, fields=None, pre_save=None, post_save=None, seed=None) self.pk = -1 - def fields(self, **kwargs): - # type: (**Any) -> "Blueprint" + def fields(self, **kwargs) -> "Blueprint": return Blueprint( model=self._model, fields=dict(self._fields, **kwargs), @@ -32,9 +37,13 @@ def fields(self, **kwargs): ) def make_one( - self, fields=None, pre_save=None, post_save=None, seed=None, iteration=None - ): - # type: (Opt[FieldMap], Opt[SaveHooks], Opt[SaveHooks], Opt[Seed], Opt[int]) -> T + self, + fields: Opt[FieldMap] = None, + pre_save: Opt[SaveHooks] = None, + post_save: Opt[SaveHooks] = None, + seed: Opt[Seed] = None, + iteration: Opt[int] = None, + ) -> T: _fields = self._fields.copy() if fields: _fields.update(fields) @@ -52,13 +61,25 @@ def make_one( ) @overload - def make(self, fields, pre_save, post_save, seed, quantity): # pragma: no cover - # type: (Opt[FieldMap], Opt[SaveHooks], Opt[SaveHooks], Opt[Seed], None) -> T + def make( + self, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: None, + ) -> T: # pragma: no cover pass @overload - def make(self, fields, pre_save, post_save, seed, quantity): # pragma: no cover - # type: (Opt[FieldMap], Opt[SaveHooks], Opt[SaveHooks], Opt[Seed], int) -> List[T] + def make( + self, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: int, + ) -> List[T]: # pragma: no cover pass def make( @@ -81,13 +102,25 @@ def make( ) @overload - def build(self, fields, pre_save, seed, quantity, make_fks): # pragma: no cover - # type: (Opt[FieldMap], Opt[SaveHooks], Opt[Seed], None, bool) -> Built + def build( + self, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: None, + make_fks: bool, + ) -> Built: # pragma: no cover pass @overload - def build(self, fields, pre_save, seed, quantity, make_fks): # pragma: no cover - # type: (Opt[FieldMap], Opt[SaveHooks], Opt[Seed], int, bool) -> List[Built] + def build( + self, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: int, + make_fks: bool, + ) -> List[Built]: # pragma: no cover pass def build( @@ -107,13 +140,23 @@ def build( ) @overload - def m(self, pre_save, post_save, seed, quantity): # pragma: no cover - # type: (Opt[SaveHooks], Opt[SaveHooks], Opt[Seed], None) -> Callable[..., T] + def m( + self, + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: None, + ) -> Callable[..., T]: # pragma: no cover pass @overload - def m(self, pre_save, post_save, seed, quantity): # pragma: no cover - # type: (Opt[SaveHooks], Opt[SaveHooks], Opt[Seed], int) -> Callable[..., List[T]] + def m( + self, + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: int, + ) -> Callable[..., List[T]]: # pragma: no cover pass def m(self, pre_save=None, post_save=None, seed=None, quantity=None): @@ -131,13 +174,15 @@ def fn(**kwargs): return fn @overload - def b(self, pre_save, seed, quantity, make_fks): # pragma: no cover - # type: (Opt[SaveHooks], Opt[Seed], None, bool) -> Callable[..., Built] + def b( + self, pre_save: Opt[SaveHooks], seed: Opt[Seed], quantity: None, make_fks: bool + ) -> Callable[..., Built]: # pragma: no cover pass @overload - def b(self, pre_save, seed, quantity, make_fks): # pragma: no cover - # type: (Opt[SaveHooks], Opt[Seed], int, bool) -> Callable[..., List[Built]] + def b( + self, pre_save: Opt[SaveHooks], seed: Opt[Seed], quantity: int, make_fks: bool + ) -> Callable[..., List[Built]]: # pragma: no cover pass def b(self, pre_save=None, seed=None, quantity=None, make_fks=False): diff --git a/django_fakery/faker_factory.py b/django_fakery/faker_factory.py index 6b40fa1..8738690 100644 --- a/django_fakery/faker_factory.py +++ b/django_fakery/faker_factory.py @@ -31,20 +31,18 @@ class Empty(object): pass -fks_cache = {} # type: Dict[str, Any] +fks_cache: Dict[str, Any] = {} class Factory(Generic[T]): - def __init__(self, fake=None): - # type: (Opt[Factory]) -> None + def __init__(self, fake: Opt[FakerFactory] = None): self.fake = fake or FakerFactory.create(locale) self.field_types = field_mappings.mappings_types self.field_names = field_mappings.mappings_names - def _serialize_instance(self, instance): - # type: (models.Model) -> FieldMap + def _serialize_instance(self, instance: models.Model) -> FieldMap: model_fields = dict(get_model_fields(instance)) - attrs = {} # type: FieldMap + attrs: FieldMap = {} for k, v in model_to_dict(instance).items(): if k == instance._meta.pk.name: # type: ignore continue @@ -62,28 +60,36 @@ def _serialize_instance(self, instance): return attrs - def seed(self, seed, set_global=False): - # type: (Seed, bool) -> None + def seed(self, seed: Seed, set_global: bool = False) -> None: self.fake.seed(seed) if set_global: random.seed(seed) - def blueprint(self, model, *args, **kwargs): - # type: (Union[str, models.Model], *Any, **Any) -> Blueprint + def blueprint(self, model: Union[str, models.Model], *args, **kwargs) -> Blueprint: return Blueprint(get_model(model), *args, **kwargs) @overload def build_one( - self, model, fields, pre_save, seed, make_fks, iteration - ): # pragma: no cover - # type: (str, Opt[FieldMap] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ..., bool = ..., Opt[int] = ...) -> LazyBuilt + self, + model: str, + fields: Opt[FieldMap], + pre_save: Opt[LazySaveHooks], + seed: Opt[Seed], + make_fks: bool, + iteration: Opt[int], + ) -> LazyBuilt: # pragma: no cover pass @overload def build_one( - self, model, fields, pre_save, seed, make_fks, iteration - ): # pragma: no cover - # type: (T, Opt[FieldMap] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ..., bool = ..., Opt[int] = ...) -> Built + self, + model: T, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + seed: Opt[Seed], + make_fks: bool, + iteration: Opt[int], + ) -> Built: # pragma: no cover pass def build_one( @@ -213,30 +219,50 @@ def build_one( @overload def build( - self, model, fields, pre_save, seed, quantity, make_fks - ): # pragma: no cover - # type: (str, Opt[FieldMap] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ..., None = ..., bool = ...) -> LazyBuilt + self, + model: str, + fields: Opt[FieldMap], + pre_save: Opt[LazySaveHooks], + seed: Opt[Seed], + quantity: None, + make_fks: bool, + ) -> LazyBuilt: # pragma: no cover pass @overload def build( - self, model, fields, pre_save, seed, quantity, make_fks - ): # pragma: no cover - # type: (T, Opt[FieldMap] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ..., None = ..., bool = ...) -> Built + self, + model: T, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + seed: Opt[SaveHooks], + quantity: None, + make_fks: bool, + ) -> Built: # pragma: no cover pass @overload def build( - self, model, fields, pre_save, seed, quantity, make_fks - ): # pragma: no cover - # type: (str, Opt[FieldMap] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ..., bool = ...) -> List[LazyBuilt] + self, + model: str, + fields: Opt[FieldMap], + pre_save: Opt[LazySaveHooks], + seed: Opt[Seed], + quantity: int, + make_fks: bool, + ) -> List[LazyBuilt]: # pragma: no cover pass @overload def build( - self, model, fields, pre_save, seed, quantity, make_fks - ): # pragma: no cover - # type: (T, Opt[FieldMap] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ..., bool = ...) -> List[Built] + self, + model: T, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: Opt[int], + make_fks: bool, + ) -> List[Built]: # pragma: no cover pass def build( @@ -261,16 +287,26 @@ def build( @overload def make_one( - self, model, fields, pre_save, post_save, seed, iteration - ): # pragma: no cover - # type: (str, Opt[FieldMap] = ..., Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ...) -> models.Model + self, + model: str, + fields: Opt[FieldMap], + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + iteration: Opt[int], + ) -> models.Model: # pragma: no cover pass @overload def make_one( - self, model, fields, pre_save, post_save, seed, iteration - ): # pragma: no cover - # type: (T, Opt[FieldMap] = ..., Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ...) -> T + self, + model: T, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + iteration: Opt[int], + ) -> T: # pragma: no cover pass def make_one( @@ -306,16 +342,26 @@ def make_one( @overload def get_or_make( - self, model, lookup, fields, pre_save, post_save, seed - ): # pragma: no cover - # type: (str, Opt[Lookup] = ..., Opt[FieldMap] = ..., Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ...) -> Tuple[models.Model, bool] + self, + model: str, + lookup: Opt[Lookup], + fields: Opt[FieldMap], + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + ) -> Tuple[models.Model, bool]: # pragma: no cover pass @overload def get_or_make( - self, model, lookup, fields, pre_save, post_save, seed - ): # pragma: no cover - # type: (T, Opt[Lookup] = ..., Opt[FieldMap] = ..., Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ...) -> Tuple[T, bool] + self, + model: T, + lookup: Opt[Lookup], + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + ) -> Tuple[T, bool]: # pragma: no cover pass def get_or_make( @@ -345,13 +391,25 @@ def get_or_make( return instance, created @overload - def g_m(self, model, lookup, pre_save, post_save, seed): # pragma: no cover - # type: (str, Opt[Lookup] = ..., Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ...) -> Callable[..., models.Model] + def g_m( + self, + model: str, + lookup: Opt[Lookup], + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + ) -> Callable[..., models.Model]: # pragma: no cover pass @overload - def g_m(self, model, lookup, pre_save, post_save, seed): # pragma: no cover - # type: (T, Opt[Lookup] = ..., Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ...) -> Callable[..., T] + def g_m( + self, + model: T, + lookup: Opt[Lookup], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + ) -> Callable[..., T]: # pragma: no cover pass def g_m(self, model, lookup=None, pre_save=None, post_save=None, seed=None): @@ -371,16 +429,26 @@ def fn(**kwargs): @overload def update_or_make( - self, model, lookup, fields, pre_save, post_save, seed - ): # pragma: no cover - # type: (str, Opt[Lookup] = ..., Opt[FieldMap] = ..., Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ...) -> Tuple[models.Model, bool] + self, + model: str, + lookup: Opt[Lookup], + fields: Opt[FieldMap], + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + ) -> Tuple[models.Model, bool]: # pragma: no cover pass @overload def update_or_make( - self, model, lookup, fields, pre_save, post_save, seed - ): # pragma: no cover - # type: (T, Opt[Lookup] = ..., Opt[FieldMap] = ..., Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ...) -> Tuple[T, bool] + self, + model: T, + lookup: Opt[Lookup], + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + ) -> Tuple[T, bool]: # pragma: no cover pass def update_or_make( @@ -418,13 +486,25 @@ def update_or_make( return instance, created @overload - def u_m(self, model, lookup, pre_save, post_save, seed): # pragma: no cover - # type: (str, Opt[Lookup] = ..., Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ...) -> Callable[..., models.Model] + def u_m( + self, + model: str, + lookup: Opt[Lookup], + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + ) -> Callable[..., models.Model]: # pragma: no cover pass @overload - def u_m(self, model, lookup, pre_save, post_save, seed): # pragma: no cover - # type: (T, Opt[Lookup] = ..., Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ...) -> Callable[..., T] + def u_m( + self, + model: T, + lookup: Opt[Lookup], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + ) -> Callable[..., T]: # pragma: no cover pass def u_m(self, model, lookup=None, pre_save=None, post_save=None, seed=None): @@ -444,30 +524,50 @@ def fn(**kwargs): @overload def make( - self, model, fields, pre_save, post_save, seed, quantity - ): # pragma: no cover - # type: (str, Opt[FieldMap] = ..., Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ..., None = ...) -> models.Model + self, + model: str, + fields: Opt[FieldMap], + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + quantity: None, + ) -> models.Model: # pragma: no cover pass @overload def make( - self, model, fields, pre_save, post_save, seed, quantity - ): # pragma: no cover - # type: (T, Opt[FieldMap] = ..., Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ..., None = ...) -> T + self, + model: T, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: None, + ) -> T: # pragma: no cover pass @overload def make( - self, model, fields, pre_save, post_save, seed, quantity - ): # pragma: no cover - # type: (str, Opt[FieldMap] = ..., Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ...) -> List[models.Model] + self, + model: str, + fields: Opt[FieldMap], + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + quantity: Opt[int], + ) -> List[models.Model]: # pragma: no cover pass @overload def make( - self, model, fields, pre_save, post_save, seed, quantity - ): # pragma: no cover - # type: (T, Opt[FieldMap] = ..., Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ...) -> List[T] + self, + model: T, + fields: Opt[FieldMap], + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: Opt[int], + ) -> List[T]: # pragma: no cover pass def make( @@ -490,23 +590,47 @@ def make( ] @overload - def m(self, model, pre_save, post_save, seed, quantity): # pragma: no cover - # type: (str, Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ..., None = ...) -> Callable[..., models.Model] + def m( + self, + model: str, + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + quantity: None, + ) -> Callable[..., models.Model]: # pragma: no cover pass @overload - def m(self, model, pre_save, post_save, seed, quantity): # pragma: no cover - # type: (T, Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ..., None = ...) -> Callable[..., T] + def m( + self, + model: T, + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: None, + ) -> Callable[..., T]: # pragma: no cover pass @overload - def m(self, model, pre_save, post_save, seed, quantity): # pragma: no cover - # type: (str, Opt[LazySaveHooks] = ..., Opt[LazySaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ...) -> Callable[..., List[models.Model]] + def m( + self, + model: str, + pre_save: Opt[LazySaveHooks], + post_save: Opt[LazySaveHooks], + seed: Opt[Seed], + quantity: Opt[int], + ) -> Callable[..., List[models.Model]]: # pragma: no cover pass @overload - def m(self, model, pre_save, post_save, seed, quantity): # pragma: no cover - # type: (T, Opt[SaveHooks] = ..., Opt[SaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ...) -> Callable[..., List[T]] + def m( + self, + model: T, + pre_save: Opt[SaveHooks], + post_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: Opt[int], + ) -> Callable[..., List[T]]: # pragma: no cover pass def m(self, model, pre_save=None, post_save=None, seed=None, quantity=None): @@ -525,23 +649,47 @@ def fn(**kwargs): return fn @overload - def b(self, model, pre_save, seed, quantity, make_fks): # pragma: no cover - # type: (str, Opt[LazySaveHooks] = ..., Opt[Seed] = ..., None = ..., bool = ...) -> Callable[..., LazyBuilt] + def b( + self, + model: str, + pre_save: Opt[LazySaveHooks], + seed: Opt[Seed], + quantity: None, + make_fks: bool, + ) -> Callable[..., LazyBuilt]: # pragma: no cover pass @overload - def b(self, model, pre_save, seed, quantity, make_fks): # pragma: no cover - # type: (T, Opt[SaveHooks] = ..., Opt[Seed] = ..., None = ..., bool = ...) -> Callable[..., Built] + def b( + self, + model: T, + pre_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: None, + make_fks: bool, + ) -> Callable[..., Built]: # pragma: no cover pass @overload - def b(self, model, pre_save, seed, quantity, make_fks): # pragma: no cover - # type: (str, Opt[LazySaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ..., bool = ...) -> Callable[..., List[LazyBuilt]] + def b( + self, + model: str, + pre_save: Opt[LazySaveHooks], + seed: Opt[Seed], + quantity: Opt[int], + make_fks: bool, + ) -> Callable[..., List[LazyBuilt]]: # pragma: no cover pass @overload - def b(self, model, pre_save, seed, quantity, make_fks): # pragma: no cover - # type: (T, Opt[SaveHooks] = ..., Opt[Seed] = ..., Opt[int] = ..., bool = ...) -> Callable[..., List[Built]] + def b( + self, + model: T, + pre_save: Opt[SaveHooks], + seed: Opt[Seed], + quantity: Opt[int], + make_fks: bool, + ) -> Callable[..., List[Built]]: # pragma: no cover pass def b(self, model, pre_save=None, seed=None, quantity=None, make_fks=False): @@ -560,4 +708,4 @@ def fn(**kwargs): return fn -factory = Factory() # type: Factory +factory: Factory = Factory() diff --git a/django_fakery/field_mappings.py b/django_fakery/field_mappings.py index 6de52c3..c6bab11 100644 --- a/django_fakery/field_mappings.py +++ b/django_fakery/field_mappings.py @@ -19,10 +19,10 @@ def add(self, key, value): self.move_to_end(key, last=False) -STRING_FIELDS = ( +STRING_FIELDS: Tuple[Any, ...] = ( models.CharField, models.TextField, -) # type: Tuple[Any, ...] +) if HAS_PSYCOPG2: from django.contrib.postgres import fields as pg_fields diff --git a/django_fakery/lazy.py b/django_fakery/lazy.py index a14df36..00c8139 100644 --- a/django_fakery/lazy.py +++ b/django_fakery/lazy.py @@ -2,8 +2,7 @@ class Lazy(object): - def __init__(self, name, *args, **kwargs): - # type (str, *Any, **Any) -> None + def __init__(self, name: str, *args, **kwargs): self.name = name self.args = args self.kwargs = kwargs diff --git a/django_fakery/shortcuts.py b/django_fakery/shortcuts.py index e0fb1fb..e7bd054 100644 --- a/django_fakery/shortcuts.py +++ b/django_fakery/shortcuts.py @@ -9,13 +9,11 @@ ParsableDate = Union[str, int, datetime, timedelta] -def get_timezone(): - # type: () -> Optional[tzinfo] +def get_timezone() -> Optional[tzinfo]: return timezone.get_current_timezone() if settings.USE_TZ else None -def future_datetime(end="+30d"): - # type: (ParsableDate) -> Callable[[int, Faker], datetime] +def future_datetime(end: ParsableDate = "+30d") -> Callable[[int, Faker], datetime]: """ Returns a ``datetime`` object in the future (that is, 1 second from now) up to the specified ``end``. ``end`` can be a string, another datetime, or a @@ -33,8 +31,7 @@ def future_datetime(end="+30d"): return lambda n, f: f.future_datetime(end_date=end, tzinfo=get_timezone()) -def future_date(end="+30d"): - # type: (ParsableDate) -> Callable[[int, Faker], date] +def future_date(end: ParsableDate = "+30d") -> Callable[[int, Faker], date]: """ Returns a ``date`` object in the future (that is, 1 day from now) up to the specified ``end``. ``end`` can be a string, another date, or a @@ -52,8 +49,7 @@ def future_date(end="+30d"): return lambda n, f: f.future_date(end_date=end, tzinfo=get_timezone()) -def past_datetime(start="-30d"): - # type: (ParsableDate) -> Callable[[int, Faker], datetime] +def past_datetime(start: ParsableDate = "-30d") -> Callable[[int, Faker], datetime]: """ Returns a ``datetime`` object in the past between 1 second ago and the specified ``start``. ``start`` can be a string, another datetime, or a @@ -71,8 +67,7 @@ def past_datetime(start="-30d"): return lambda n, f: f.past_datetime(start_date=start, tzinfo=get_timezone()) -def past_date(start="-30d"): - # type: (ParsableDate) -> Callable[[int, Faker], date] +def past_date(start: ParsableDate = "-30d") -> Callable[[int, Faker], date]: """ Returns a ``date`` object in the past between 1 day ago and the specified ``start``. ``start`` can be a string, another date, or a diff --git a/django_fakery/utils.py b/django_fakery/utils.py index 3ee9c51..5d128d9 100644 --- a/django_fakery/utils.py +++ b/django_fakery/utils.py @@ -1,4 +1,4 @@ -from typing import overload +from typing import Any, List, Type, overload from django.apps import apps from django.db import models @@ -8,7 +8,7 @@ from .types import T -def language_to_locale(language): +def language_to_locale(language: str) -> str: """ Converts django's `LANGUAGE_CODE` settings to a proper locale code. """ @@ -19,14 +19,12 @@ def language_to_locale(language): @overload -def get_model(model): - # type: (str) -> models.Model +def get_model(model: str) -> models.Model: pass @overload -def get_model(model): - # type: (T) -> T +def get_model(model: T) -> T: pass @@ -36,13 +34,13 @@ def get_model(model): return model -def get_model_fields(model): +def get_model_fields(model: Type[models.Model]) -> List[models.fields.Field]: fields = list(model._meta._forward_fields_map.items()) for m2m in model._meta.many_to_many: fields.append((m2m.name, m2m)) return fields -def set_related(instance, attr, value): +def set_related(instance: models.Model, attr: str, value: Any): field = getattr(instance, attr) field.set(value)