Questions tagged [django-stubs]

Repository: https://github.com/typeddjango/django-stubs

16 questions
11
votes
0 answers

mypy django rest framework - Unsupported left operand type when multiple permission classes are used

I am integrating mypy on a existing codebase(using django, drf frameworks). Sample code in view.py: from rest_framework.permissions import IsAdminUser, IsAuthenticatedOrReadOnly @api_view() @permission_classes([IsAuthenticatedOrReadOnly |…
partha biswas
  • 204
  • 1
  • 7
6
votes
1 answer

Type Hint for Django model with annotated field

Let's say I have the following Django models: class Toolbox(models.Model): name = models.CharField(max_length=255) tools = models.ManyToManyField("Tool") class Tool(models.Model): class Size(models.TextChoices): SMALL = "S" …
Johnny Metz
  • 5,977
  • 18
  • 82
  • 146
4
votes
0 answers

How to get .env to pre-commit + mypy + django-stubs

I try to configurate start mypy + django-stubs cheking before commiting. I use pre-commit for it. When I try to commit, I have error django.core.exceptions.ImproperlyConfigured: Set the POSTGRES_DB environment variable. This variable is in .env…
4
votes
1 answer

Django stubs interrupt Pycharm autocomplete

I have a django app with several models. Here is autocomplete when I filter objects Then I install django-stubs for MyPy pip install django-stubs Now my autocomplete looks like this When I delete django-stubs autocomplete works well, like on…
Yevhen Bondar
  • 4,357
  • 1
  • 11
  • 31
3
votes
2 answers

Django: typehinting backward / related_name / ForeignKey relationships

Let's say we have the following models: class Site(models.Model): # This is djangos build-in Site Model pass class Organization(models.Model): site = models.OneToOneField(Site) And if I use this somewhere in some other…
flix
  • 1,821
  • 18
  • 23
3
votes
2 answers

How to provide a source directory in the [tool.django-stubs] directive of a pyproject.tom file?

I'm using a src directory as root source directory in my django projects. I'm trying to perform pre-commit actions on those django projects, with mypy and django-stubs. Is is a way to say in pyproject.toml than the source root is in src/ ? For now,…
frague
  • 189
  • 1
  • 10
3
votes
1 answer

request.user returning abstract user (django-stubs)

I am trying to type the following: user: Final[CustomUser] = self.request.user Where CustomUser is my custom user model defined in settings.py. My IDE complains that Expected type 'CustomUser', got 'AbstractBaseUser' instead. Looking at the…
Bula
  • 2,398
  • 5
  • 28
  • 54
2
votes
0 answers

mypy returning module 'X' has no attribute 'MyClass' with django rest framework

I have the following django rest framework setup app/models.py: from django.db import models class A(models.Model): x = models.CharField(max_length=8) app/serializer.py: from .models import A from rest_framework import serializers class…
Lingster
  • 977
  • 10
  • 21
1
vote
1 answer

How to typehint custom and overridden Managers in an Abstract Class using django-stubs?

we are trying to add typehints to our django (3.2) project. We are using django-stubs (1.12.0). We have an AbstractBaseModel with a custom BaseManager that we use for almost every other model. In some cases we also extend the custom…
flix
  • 1,821
  • 18
  • 23
1
vote
1 answer

Mypy complaining about Name "Optional" is not defined without the use of Optional

I've recently started using mypy, and have run into some weird problems that i cannot for the life of me seem to figure out. I'm using mypy 0.950, django-stubs 1.11.0, django 4.0.5 and python 3.10.2. Running mypy through the command line returns…
DanielK
  • 257
  • 2
  • 15
1
vote
1 answer

How to use correctly types for Django model fields with django stubs?

Consider the following code fragment related to Django models: class Machine(HasStatus): # type: ignore machines: "models.Manager[Machine]" = models.Manager() number = models.IntegerField(verbose_name="Číslo stroje", unique=True) #Type…
Pavel Hanpari
  • 4,029
  • 2
  • 20
  • 23
1
vote
0 answers

Django-stubs doesn't ignore import

I've tried to configurate pre-commit + mypy + django-stubs, so I have error when i try to commit. ModuleNotFoundError: No module named 'environ' My configs: .pre-commit-config.yaml - repo: https://github.com/pre-commit/mirrors-mypy rev: '' …
1
vote
1 answer

django, mypy : queryset, choices. error: Incompatible types

We have: In models.py class Language(models.IntegerChoices): en = 1 ru = 2 class Translation(models.Model): language_id = models.PositiveIntegerField(choices=Language.choices) mnemonic = models.CharField(max_length=255) …
Yury Dr
  • 21
  • 4
1
vote
2 answers

Mypy linting in VSCode Errors out with "Error constructing plugin instance of NewSemanalDRFPlugin"

Trying to run Mypy linter in VSCode, always getting the same error in the output... The root of the Django project and the workspace directory are different. ##########Linting Output - mypy########## Error constructing plugin instance of…
vi_me
  • 373
  • 4
  • 17
0
votes
0 answers

Why is mypy not inferring Django CharField choices as type with django-stubs?

I have a Django model with a field that is a models.CharField with choices: from django.db import models class MyChoice(models.TextChoices): FOO = "foo", _("foo") BAR = "bar", _("bar") BAZ = "baz", _("baz") class…
tinom9
  • 369
  • 2
  • 12
1
2