Skip to content

Commit

Permalink
refactor typing (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella authored Jun 5, 2023
1 parent 0084492 commit 3bfff90
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
91 changes: 68 additions & 23 deletions django_fakery/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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):
Expand Down
Loading

0 comments on commit 3bfff90

Please sign in to comment.