Skip to content

unresolved-attribute errors for Django model fields #1018

@aaschenbrener

Description

@aaschenbrener

Summary

This is an expansion on the known issue: No implicit import of submodules (#133). I've imported Django models explicitly from their submodules. There is an issue with dynamic model attributes like objects and id that Ty doesn’t recognize.

I’m encountering persistent unresolved-attribute errors when running Ty static type checker on Django models, specifically on standard Django model attributes such as id, demo_name, and objects. These errors occur even though the Django model is properly defined, registered, explicitly imported and confirmed to have those attributes in the Django shell.

Steps to reproduce:

Define a Django model with fields such as id, demo_name in apps/sample/models/example.py.

Confirm the model loads and works correctly inside the Django shell:

from apps.sample.models.example import Demo
from django.db import models

issubclass(Demo, models.Model)  # Returns True
Demo._meta.get_fields()  # Returns expected model fields including 'id'
Demo.objects.all()  # Executes without error

Run Ty on a test file that uses this model, e.g. test_ty_model.py:

def test_model_fields(m: Demo):
    reveal_type(m.id)
    reveal_type(m.demo_name)
    reveal_type(m.save)

Ty reports errors like:

error[unresolved-attribute]: Type `Demo` has no attribute `id`
error[unresolved-attribute]: Type `Demo` has no attribute `objects`

I have tried running Ty with explicit environment variables to load Django settings, but the errors persist:

PYTHONPATH=. DJANGO_SETTINGS_MODULE=proj.settings.dev ty check test_ty_model.py

Expected behavior:
Ty should recognize standard Django model attributes (id, objects, model fields) and when the Django environment is correctly configured.

Actual behavior:
Ty reports unresolved-attribute errors for these standard model attributes, even though Django shell confirms their existence.

Environment:

Ty version: 0.0.1-alpha.18 (d697cc0 2025-08-14)
Python version: 3.13.6
Django version: 5.2.5
OS: macOS 15.6 24G84

Project structure:

project-root/
├── manage.py
├── test_ty_model.py             
├── apps/
│   ├── sample/
│   │   ├── models/
│   │   │   └── example.py
├── proj/
│   ├── settings/
│   │   └── base.py
│   │   └──  dev.py

Version

0.0.1-alpha.18 (d697cc0 2025-08-14)

Metadata

Metadata

Assignees

No one assigned

    Labels

    libraryDedicated support for popular third-party libraries

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions