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

Make plugin handle explicitly declared reverse descriptors for FKs #2230

Conversation

flaeppe
Copy link
Member

@flaeppe flaeppe commented Jun 22, 2024

If a reverse descriptor was explicitly declared on a model, the plugin skipped to create a more specific subclass for its related manager.

Refs: #2227

If a reverse descriptor was explicitly declared on a model, the plugin
skipped to create a more specific subclass for its related manager.
from django.db.models.fields.related_descriptors import ReverseManyToOneDescriptor

class Other(models.Model):
explicit_descriptor: ClassVar[ReverseManyToOneDescriptor["MyModel"]]
Copy link
Member

Choose a reason for hiding this comment

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

When is this useful? Isn't it better to just write = ReverseManyToOneDescriptor(...)?

Copy link
Member Author

Choose a reason for hiding this comment

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

When you don't use mypy and also don't want to override anything that Django does during runtime.

Copy link
Member Author

Choose a reason for hiding this comment

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

Or rather: when you don't use our mypy plugin

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we then need a assert_type test for this instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose we could, but we'd have to make it assert to a different type depending on if we're running with mypy or pyright. Since the plugin creates a more specific type. It didn't feel very useful to be honest. But I can add it if you find that it could be useful

The test here was merely for the plugin, it failed initially so it was some form of regression. I wanted to ensure that the more specific type was set even though an explicit descriptor was declared. I discovered the plugin had an early exit that stopped the descriptor refinement

Copy link
Member Author

@flaeppe flaeppe Jun 22, 2024

Choose a reason for hiding this comment

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

Actually, thinking twice about it. I don't think that mypy would output anything different, since it currently requires the runtime to output a more specialised type

Copy link
Member Author

Choose a reason for hiding this comment

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

FWIW I added an assert_type test

@flaeppe
Copy link
Member Author

flaeppe commented Jun 28, 2024

What do you think? Does this look fine?

@sobolevn sobolevn merged commit 8703696 into typeddjango:master Jun 28, 2024
36 checks passed
@flaeppe flaeppe deleted the fix/explicit-reverse-many-to-one-descriptor branch June 28, 2024 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants