Skip to content

Commit

Permalink
Merge pull request #102 from paulocheque/typing
Browse files Browse the repository at this point in the history
[feature] Added Python type hints for the main DDF methods
  • Loading branch information
paulocheque committed Jan 4, 2020
2 parents 8968fff + 7c052ae commit dc8a369
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
38 changes: 33 additions & 5 deletions django_dynamic_fixture/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Module that contains wrappers and shortcuts (aliases).
"""
import warnings
import six

from django_dynamic_fixture.ddf import DynamicFixture, Copier, DDFLibrary, \
set_pre_save_receiver, set_post_save_receiver
Expand Down Expand Up @@ -57,7 +58,8 @@ def fixture(**kwargs):


# Wrappers
def new(model, n=1, lesson=None, persist_dependencies=True, **kwargs):

def _new(model, n=1, lesson=None, persist_dependencies=True, **kwargs):
"""
Return one or many valid instances of Django Models with fields filled with auto generated or customized data.
All instances will NOT be persisted in the database, except its dependencies, in case @persist_dependencies is True.
Expand Down Expand Up @@ -87,7 +89,7 @@ def new(model, n=1, lesson=None, persist_dependencies=True, **kwargs):
return instances


def get(model, n=1, lesson=None, **kwargs):
def _get(model, n=1, lesson=None, **kwargs):
"""
Return one or many valid instances of Django Models with fields filled with auto generated or customized data.
All instances will be persisted in the database.
Expand Down Expand Up @@ -116,7 +118,7 @@ def get(model, n=1, lesson=None, **kwargs):
return instances


def teach(model, lesson=None, **kwargs):
def _teach(model, lesson=None, **kwargs):
'''
@model: The class of the Django model.
@lesson: Name of custom lesson to be created.
Expand All @@ -140,11 +142,37 @@ def teach(model, lesson=None, **kwargs):


# Shortcuts
N = new
G = get
N = new = _new
G = get = _get
T = teach = _teach
F = fixture
C = Copier
P = print_field_values
DDFLibrary = DDFLibrary
PRE_SAVE = set_pre_save_receiver
POST_SAVE = set_post_save_receiver

if six.PY3:
# Add type hints for Python >= 3.5
try:
import typing

INSTANCE_TYPE = typing.TypeVar('INSTANCE')

hack_to_avoid_py2_syntax_errors = '''
def new(model: typing.Type[INSTANCE_TYPE], n=1, lesson=None, persist_dependencies=True, **kwargs) -> INSTANCE_TYPE:
return _new(model, n=n, lesson=lesson, persist_dependencies=persist_dependencies, **kwargs)
def get(model: typing.Type[INSTANCE_TYPE], n=1, lesson=None, **kwargs) -> INSTANCE_TYPE:
return _get(model, n=n, lesson=lesson, **kwargs)
def teach(model: typing.Type[INSTANCE_TYPE], lesson=None, **kwargs):
return _teach(model, lesson=lesson, **kwargs)
N = new
G = get
T = teach
'''
exec(hack_to_avoid_py2_syntax_errors)
except (ImportError, SyntaxError) as e:
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
]

install_requires = [
'six',
'six'
]

setup(name='django-dynamic-fixture',
Expand Down

0 comments on commit dc8a369

Please sign in to comment.