Skip to content

apps.get_model() wrongfully returns latest model types #3584

Open

Description

@WhyNotHugo

What's wrong

For apps.get_model(), this plugin returns types declared as defined in models.py.

How is that should be

apps.get_model will return a runtime-generated model with the fields that the model had in the context where it is being used, but almost never the "latest" version of a model.

For example, suppose a system has 3 migrations:

  1. Create model Test with fields hello and goodbye.
  2. apps.get_model("myapp", "Test").
  3. Remove field goodbye from model Test.

In (2), this plugin will resolve a type which is missing field goodbye, but the actual class returned there does have that field, and that's the whole point of using apps.get_model.

Notes

I suspect that properly implementing this feature is wildly complex, since you basically need to run all the migration machinery in context when resolving.

I'd prefer the previous behaviour (not resolving these types) rather than resolving the wrong ones.

System information

  • OS: n/a
  • python version: 3.14.5
  • django version: 6.1.0
  • mypy version: 1.19.1
  • django-stubs version: 6.0.9
  • django-stubs-ext version: 6.0.9

Activity

  1. sobolevn commented on Aug 11, 2026

    @sobolevn
    Member

    How is apps annotated? from django.db.migrations.state import StateApps?
    See #3511 (comment)

  2. WhyNotHugo commented on Aug 11, 2026

    @WhyNotHugo
    ContributorAuthor
  3. sobolevn commented on Aug 11, 2026

    @sobolevn
    Member
  4. WhyNotHugo commented on Aug 11, 2026

    @WhyNotHugo
    ContributorAuthor

    I'm annotating using the class in Django's documentation.

    Using type annotations for an internal undocumented class is a very nasty hack.

  5. WhyNotHugo commented on Aug 11, 2026

    @WhyNotHugo
    ContributorAuthor

    Please re-open this to at least give visibility to the bug, even if it's not considered urgent.

  6. sobolevn commented on Aug 11, 2026

    @sobolevn
    Member

    Probably this needs to be updated in the Django docs :)

  7. UnknownPlatypus commented on Aug 11, 2026

    @UnknownPlatypus
    Contributor

    Given a few people already got tripped by that should we consider skipping in migration files like I suggested in #3511 (comment) ?

  8. sobolevn commented on Aug 11, 2026

    @sobolevn
    Member

    I also use StateApps in migration testing: https://github.com/wemake-services/django-test-migrations
    It would be very stange to have different types based on the code location :/
    I don't think that we ever should do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions