paulocheque / django-dynamic-fixture

A complete library to create dynamic model instances for testing purposes.
http://django-dynamic-fixture.readthedocs.io/
Other
390 stars 67 forks source link

Python 3.5: type hints for N() and G() outputs #96

Closed JanChec closed 4 years ago

JanChec commented 5 years ago

Hey,

I know that the support for new Python is not there yet, so I'm not making it a PR. Instead I put here how it can be achieved to have type hints for N() and G() functions (super useful for using the lib with an IDE that uses type hints).

In __init__.py, at the top:

import typing

(...)

INSTANCE_TYPE = typing.TypeVar('INSTANCE')

Then:

def new(model: typing.Type[INSTANCE_TYPE], shelve=False, n=1, persist_dependencies=True, **kwargs) -> INSTANCE_TYPE:
def get(model: typing.Type[INSTANCE_TYPE], shelve=False, n=1, **kwargs) -> INSTANCE_TYPE:
paulocheque commented 4 years ago

Thanks @Kern3l ! I did a hack to handle the Python 2/3 compatibility. https://github.com/paulocheque/django-dynamic-fixture/pull/102

It is ugly but it works well for a test library.