[mypy.plugins.django-stubs] django_settings_module = 'hyre.settings'
Has anyone written a stub for model_utils Choices?
Considering this one incoming https://docs.djangoproject.com/en/dev/releases/3.0/#enumerations-for-model-field-choices, wouldn't it be redundant?
virtualenvbut I started a dummy project with
pipenvand your instructions worked (even without specifying the
mypyPathexplicitly). I'm not sure if
virtualenvis to blame at this point, or something else with my main project.
class BaseAbstractModel(models.Model): created_datetime = models.DateTimeField(_('Created at'), auto_now_add=True) updated_datetime = models.DateTimeField(_('Last update at'), auto_now=True) objects = models.Manager() class Meta: abstract = True
in my inherited model I have
class LoanProduct(BaseAbstractModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # noqa name = models.TextField() slug = models.SlugField(unique=True)
src/loan/models.py:22: error: Need type annotation for 'id' src/loan/models.py:24: error: Need type annotation for 'name' src/loan/models.py:25: error: Need type annotation for 'slug'
class LoanProduct(BaseAbstractModel, models.Model):
objects isn't quite typed correctly. In the simplest case
# models.py from django.db import models class MyModel(models.Model): field = models.CharField(max_length=100) # views.py from . import models def my_view() -> None: instance = models.MyModel.objects.all() instance = models.MyModel.objects.first() # correct, no error, revealed type is Union[MyModel, None] instance = models.MyModel.objects.method_that_does_not_exist() # no error, should error out ,revealed type is Any instance = models.MyModel.ojects.method_that_does_not_exist() # correctly errors out saying that ojects (misspelled) does not exist on Type[MyModel].
This applies to mypy 0.720 with stubs 1.1, and mypy 0.730 with stubs 1.2
_ + 2which equals to
lambda x: x + 2
core/folder that has non-Django specific library code, and a
web/folder where Django stuff is defined. So I can't just run the stuff from within
web/cause then it can't find the stuff that I have in
I' ve written a number of tests where I need to pass an invalid Object.ID which I solved by incrementing the id of the last valid instance like so:
invalid_academy_id: int = Academy.objects.last().id + 100
However, this is causing mypy-django to trip up and I'm a bit at a loss on how to solve this. Mypy considers the
Academy.object.last() to be
Optional and as such raises the following errors:
api/tests/test_utils.py:106: error: Unsupported operand types for + ("AutoField[Union[Combinable, int, str], int]" and "int") api/tests/test_utils.py:106: note: Left operand is of type "Union[AutoField[Union[Combinable, int, str], int], Any]"
However, even removing the
:int typing fails to remove the error. I could ofcourse ignore the error - but that seems like an inadequate solution. Has anyone run into these kind of errors (fetching parameters from objects which are technically optional but which you know to exist) and - if so - how did you solve them?
you don't actually know that there is any object returned by
last() -- if there are no rows in the table :)
You could do:
obj = Academy.objects.last() assert obj is not None my_id = obj.id # Now mypy knows that obj is not None
Then you will also get a run-time assertion if the object is
Alternatively, you could just tell mypy that you know better about the type:
obj = cast(Academy, Academy.objects.last()) but it's kind of unsafe