Skip to content

How to typecheck methods and attributes generated by metaprogramming #1310

Closed
@akaariai

Description

@akaariai

I'm wondering how to add type information for a Django project. For example, consider a typical Django model:

class Foo(models.Model):
    field1 = models.TextField(choices=(('a', 'Choice A'), ('b', 'Choice B')))
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children')

The metaprogramming in Django will generate the following attributes and methods:

  1. Foo.objects, a QuerySet for Foo objects. For example Foo.objects.get(pk=1) will return a Foo object, and Foo.objects.filter(pk__gte=1) is an iterable of Foo objects.
  2. There will be an autogenerated method get_field1_display() for Foo. It returns the human readable value for foo field1's value (that is, it returns an str).
  3. foo.parent should be a Foo instance, foo.children should be a QuerySet of Foo instances.

The request itself for this issue is to document a way to add type information for meta-programming generated attributes and methods. Optimally this would be done as part of the meta-programming itself.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions