mirror of
https://github.com/davidhalter/django-stubs.git
synced 2026-10-07 20:54:38 +08:00
Handle GenericForeignKey class typeinfo lookup failure. (#597)
This addresses an obscure crash we're getting when defining a GenericForeignKey subclass on a model. Not sure how this slipped through type checking since `helpers.lookup_class_typeinfo -> Optional[TypeInfo]` while `.get_private_descriptor_type(type_info: TypeInfo)` so this should be a clear type violation.
This commit is contained in:
1 parent
3931432b34
commit
d4c1ed2ce0
2 files changed
+30
-2
No files matched your search
@@ -18,4 +18,27 @@
|
||||
class User(models.Model):
|
||||
pass
|
||||
class Tag(models.Model):
|
||||
content_object = fields.GenericForeignKey()
|
||||
content_object = fields.GenericForeignKey()
|
||||
- case: generic_foreign_key_subclass_could_point_to_any_model_and_is_always_optional
|
||||
main: |
|
||||
from myapp.models import Tag, User
|
||||
myuser = User()
|
||||
Tag(content_object=None)
|
||||
Tag(content_object=myuser)
|
||||
Tag.objects.create(content_object=None)
|
||||
Tag.objects.create(content_object=myuser)
|
||||
reveal_type(Tag().content_object) # N: Revealed type is 'Union[Any, None]'
|
||||
installed_apps:
|
||||
- myapp
|
||||
files:
|
||||
- path: myapp/__init__.py
|
||||
- path: myapp/models.py
|
||||
content: |
|
||||
from django.db import models
|
||||
from django.contrib.contenttypes import fields
|
||||
class User(models.Model):
|
||||
pass
|
||||
class Tag(models.Model):
|
||||
content_object = fields.GenericForeignKey()
|
||||
# Simulate a GenericForeignKey subclass without type infos.
|
||||
Tag.content_object.__class__ = type('MyGenericForeignKey', (fields.GenericForeignKey,), {})
|
||||
Reference in new issue
Block a user