-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pylance v2024.10.1 doesn't work with Django models #6562
Comments
Thanks for reaching out. To help us investigate, could you provide the complete code sample that reproduces the issue? If your project is in a public repo, sharing the link would also be helpful. Could you also provide more logs as outlined in our troubleshooting guide here? The logs you shared don't seem to have Without the full project, it's hard to diagnose the exact problem. However, if you're seeing new diagnostics, it might be related to the |
This is a known issue. These fields are added dynamically by django and there's currently no way in the Python type system to tell a static type checker that they will exist. General improvements for django are tracked by #3701 |
@StellaHuang95 Please see my attached sample script and the log. server_log.txt Pylance was failed to check field id from django model and some fields from django rest framework such data, query_params. Pylance version v2024.8.1 can check these fields. from django.db import models
from rest_framework.response import Response
from rest_framework import viewsets, serializers, permissions, decorators
class Author(models.Model):
"""Author model"""
name = models.CharField(max_length=100, help_text="Author name")
description = models.TextField(max_length=200, help_text="Author description")
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
"""To string"""
return self.name
def get_books(self):
"""Get books"""
return Book.objects.filter(author_id=self.id)
def update_signature(self, signature=None):
"""Update signature"""
if signature is None:
raise ValueError("Signature is required")
for obj in self.get_books():
obj.signature = signature
obj.save()
class Signature(models.Model):
"""Signature model"""
name = models.CharField(max_length=100, help_text="Signature name")
data = models.TextField(max_length=200, help_text="Signature description")
author = models.ForeignKey(Author, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
"""To string"""
return self.name
class Book(models.Model):
"""Test model"""
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
description = models.TextField(max_length=100)
signature = models.TextField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
"""To string"""
return self.title
def get_author_name(self):
"""Get author name"""
return self.author.name
def set_author(self, author_obj:'Author'):
"""Set author"""
if author_obj is None:
raise ValueError("Author is required")
if author_obj.get_books().count() > 5:
raise ValueError("Author has too many books")
self.author = author_obj
self.save()
def set_signutare(self):
"""Set signature"""
signature_obj = Signature.objects.filter(author_id=self.author.id).first()
if signature_obj is None:
raise ValueError("Signature not found")
self.signature = signature_obj
self.save()
class AuthorSerializer(serializers.ModelSerializer):
"""Author serializer"""
class Meta:
model = Author
fields = '__all__'
class AuthorViewSet(viewsets.ModelViewSet):
"""Author view set"""
queryset = Author.objects.all()
serializer_class = AuthorSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
@decorators.action(
detail=False, methods=['get'],
url_path='find-author', url_name='find-author')
def find_author(self, *args, **kwargs):
"""Get author name"""
name = self.request.query_params.get('name', '')
return Response({ "data": name })
@decorators.action(
detail=True, methods=['post'],
url_path='create-signature', url_name='create-signature')
def create_signature(self, *args, **kwargs):
"""Get author name"""
print(self.request.data)
return Response({ "data": "created" })
This is the error from Pylance |
@debonte In version v2024.8.1. I didn't face this issue. Please see my screenshot for version 2024.8.1 |
The same issue rears its head elsewhere when using dynamic model fetching, in this example with As a result, any type inferences for children and children's children of that object (in this instance, In As far as I can tell, this particular invocation of the issue arises in In In |
NoReturn generally means Pylance/Pyright thinks the function is throwing an exception. Looking at that code, maybe that's a bug in Pyright? It must think the assert always fires? |
@luanft, from the logs, I see What's causing confusion is probably that the default value for I think we could improve clarity by adding more detail to the warning message when |
@StellaHuang95
|
@luanft Right, so two things are happening here:
I hope this clears up the confusion. To get the same behavior, you can add |
@StellaHuang95 Thank you, define typeCheckingMode = off in pyright config worked for me. |
The Pylance version v2024.8.1 works well on django models. After I updated the Pylance to latest version v2024.10.1. Pylance was failed to get the id, foreign key properties. Can you help to check it?
Environment data
Code Snippet
Pyright config
Issue
Logs
The text was updated successfully, but these errors were encountered: