Repository navigation
Description
Describe the Bug
Since #4115 (fixing #3932), a ForeignKey("app_label.Model") string reference resolves when the target class is defined or imported in the module that declares the field. When the target is not imported there, the reference is not resolved: the relation is typed Any, and the <fk>_id attribute that Django always adds is missing, so every access reports missing-attribute.
Importing the module is not enough; the class name itself has to be in scope. The "app_label.Model" form exists so that a model can reference another app without importing it, often to avoid an import cycle, so this is the case the string form is mostly used for.
Reproduction
# authors/models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Agent(models.Model):
name = models.CharField(max_length=100)# books/models.py
from django.db import models
from authors.models import Agent
class Publisher(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
# Class reference to a model in another app: resolved.
agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
# String reference to a model in this module: resolved.
publisher = models.ForeignKey("Publisher", on_delete=models.CASCADE)
# String reference to a model in another app whose class is not imported here:
# not resolved, even though `authors.models` is imported for `Agent`.
author = models.ForeignKey("authors.Author", on_delete=models.CASCADE)# repro.py
from typing import reveal_type
from books.models import Book
def show(book: Book) -> None:
reveal_type(book.agent) # Agent
reveal_type(book.agent_id) # int
reveal_type(book.publisher) # Publisher
reveal_type(book.publisher_id) # int
reveal_type(book.author) # Any, expected Author
reveal_type(book.author_id) # Unknown + missing-attribute, expected intERROR Object of class `Book` has no attribute `author_id` [missing-attribute]
If books/models.py adds from authors.models import Author, or the same import under if TYPE_CHECKING:, then book.author is Author and book.author_id is int.
Expected
book.authorisAuthorandbook.author_idisint. Django resolves"authors.Author"through the app registry, and fix Add support for Django reverse relationships for cross module #2071 #5130'sdjango-model-modulessetting gives pyrefly a list of model modules to look the label up in.- Where the reference still can't be resolved,
<fk>_idshould exist with an unknown type rather than be missing (suggested in Django: ForeignKey("app_label.Model") string refs are not resolved (no <fk>_id / relation typed Any) #3932). The attribute exists on every FK at runtime, somissing-attributeis always a false positive.
Notes
- Django ForeignKey ID attribute not generated when referencing un-imported model #3657 (closed as a duplicate of Django: ForeignKey("app_label.Model") string refs are not resolved (no <fk>_id / relation typed Any) #3932) describes the same un-imported case. In Django: ForeignKey("app_label.Model") string refs are not resolved (no <fk>_id / relation typed Any) #3932, a maintainer noted that resolving it needs global analysis.
- fix Add support for Django reverse relationships for cross module #2071 #5130 (open, for Add support for Django reverse relationships #2071) adds
django-model-modulesfor cross-module reverse relations. Its docs say: "String references resolve only to models defined or imported in the source module. Pyrefly does not use app labels to locate models." So it does not cover this case, but its module list could. - In our project, after switching every reference that can be imported to a class reference, 9 string references remain. Each is needed because the two apps' models modules import each other (for example
partnership.models->ForeignKey("organization.Company")whileorganization.modelsimportspartnership.models), which is the case the string form exists for. The only workaround is anif TYPE_CHECKING:import of each target.
Versions
pyrefly 1.5.0 (preset = "default"), Django 5.2, django-stubs 6.1.2, Python 3.12.
Activity