Skip to content
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

Create regression test for related managers 2022 #967

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/typecheck/fields/test_related.yml
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,46 @@
pass


- case: test_related_managers_for_subclass_with_multiple_inheritance
main: |
from myapp import models
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models

class TimestampedModel(models.Model):
id = models.UUIDField(primary_key=True, editable=False)
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
updated_at = models.DateTimeField(auto_now=True, db_index=True)

class Meta:
abstract = True

class ReprMixin:
def __str__(self) -> str:
return self.__repr__()

def __repr__(self) -> str:
return 'yolo'

class User(ReprMixin, TimestampedModel):
name = models.TextField()

class Booking(ReprMixin, TimestampedModel):
renter = models.ForeignKey(User, on_delete=models.PROTECT)
owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='bookingowner_set')

class Meta(TimestampedModel.Meta):
ordering = ['-created_at']

def process_user(user: User):
reveal_type(user.bookingowner_set) # N: Revealed type is 'django.db.models.manager.RelatedManager[myapp.models.Booking]'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reveal_type(user.bookingowner_set) # N: Revealed type is 'django.db.models.manager.RelatedManager[myapp.models.Booking]'
reveal_type(user.bookingowner_set) # N: Revealed type is "django.db.models.manager.RelatedManager[myapp.models.Booking]"

reveal_type(user.booking_set) # N: Revealed type is 'django.db.models.manager.RelatedManager[myapp.models.Booking]'
Copy link
Collaborator

@intgr intgr May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider using typing_extensions.assert_type() instead of reveal_type(), which is less verbose.

But would need to ensure that testing dependencies are typing-extensions>=4.2.0 and mypy>=0.950 then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it won't work with our pytest-mypy-plugin lib 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reveal_type(user.booking_set) # N: Revealed type is 'django.db.models.manager.RelatedManager[myapp.models.Booking]'
reveal_type(user.booking_set) # N: Revealed type is "django.db.models.manager.RelatedManager[myapp.models.Booking]"


- case: foreign_key_relationship_for_models_with_custom_manager
main: |
from myapp.models import Transaction
Expand Down