Typing: Type annotations and metaclasses

Created on 6 Mar 2017  路  2Comments  路  Source: python/typing

I've read through PEPs 483, 484, and 526, but I don't see any discussion of how type hints should work when the type of a class member differs from the type of an instance member, like when metaclasses are used to create instances.

e.g.:

>>> from django.db import models
>>> class MyModel(models.Model):
...     name = models.CharField()
...     class Meta:
...         app_label = "myapp"
...
>>> type(MyModel.name)
<class 'django.db.models.query_utils.DeferredAttribute'>
>>> m = MyModel()
>>> type(m.name)
<class 'str'>

In this case, I would like to be able to specify an instance type of str for MyModel.name.

Most helpful comment

The best approximation we currently have are descriptors. If the CharField class defines a __get__ method then the type of m.name will be determined by the return type of CharField.__get__.

All 2 comments

The best approximation we currently have are descriptors. If the CharField class defines a __get__ method then the type of m.name will be determined by the return type of CharField.__get__.

This looks essentially like a duplicate of https://github.com/python/typing/issues/293

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sid-kap picture sid-kap  路  7Comments

shoyer picture shoyer  路  8Comments

LiraNuna picture LiraNuna  路  7Comments

Levitanus picture Levitanus  路  5Comments

zsluedem picture zsluedem  路  3Comments