Skip to content

Refine Site model fields and reorder SiteManager - #3134

Open
ahmedasar00 wants to merge 1 commit into
typeddjango:masterfrom
ahmedasar00:fix/site-name-stub
Open

ahmedasar00 wants to merge 1 commit into
typeddjango:masterfrom
ahmedasar00:fix/site-name-stub

Conversation

@ahmedasar00

@ahmedasar00 ahmedasar00 commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

Changes

  • Removed outdated Any type hints and replaced them with concrete types:
    • django.contrib.sites.models.Site.domain → str
    • django.contrib.sites.models.Site.name → str
    • django.contrib.sites.models.Site.id → int
  • Updated SiteManager methods typing for better accuracy.

Notes

  • Moved SiteManager definition after the Site class to avoid potential forward reference issues.

Comment thread django-stubs/contrib/sites/models.pyi Outdated

domain = models.CharField(max_length=100)
name = models.CharField(max_length=50)
domain: Any

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, why do we change a model field to be Any? What is the original error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, why do we change a model field to be Any? What is the original error?

I used Any while debugging a stubtest error, but I agree the concrete types are better.
Should I revert them to str / int ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I dont think we should change anything here, I'm pretty sure the current state is correctly infered by the mypy plugin

@ahmedasar00 ahmedasar00 Mar 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In my opinion, I find it useful because it prevents forward references and using str and int helps ensure greater accuracy, although I could be wrong.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still not convinced it's a good idea, I'm almost sure we loose some strict type checking with mypy when setting attribute for ex.
I don't think this is the way to go, we need to research existing issues and pr on the subject, weight the various options we have and how well they work with typechecker with no plugin (which should be goal, but not at the expense of the mypy setup)

@ahmedasar00 ahmedasar00 Mar 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback.
I reopened this PR because I want to find the right solution rather than just close it without understanding the correct approach.
Could you point me to any existing issues or PRs that discussed how the mypy plugin handles Site model field types? That would help me understand the right direction before making further changes.
If you think there's no clear path forward right now, I'm also happy to close this and revisit it later with a better-informed approach.

@ahmedasar00 ahmedasar00 closed this Mar 2, 2026
@ahmedasar00 ahmedasar00 reopened this Mar 2, 2026
@ahmedasar00 ahmedasar00 changed the title Fix type hints for Site model fields Refine Site model fields and reorder SiteManager Mar 2, 2026
Detailed changes:

- Map Site fields (id, domain, name) to concrete types to fix DeferredAttribute errors.

- Type SITE_CACHE as dict[int | str, Site] for better precision.

- Add missing __str__ method to Site stub.

- Remove resolved Site entries from stubtest allowlist.

This branch has not been deployed

No deployments
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.

3 participants