Skip to content

Type annotations and metaclasses #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thauk-copperleaf opened this issue Mar 6, 2017 · 2 comments
Closed

Type annotations and metaclasses #399

thauk-copperleaf opened this issue Mar 6, 2017 · 2 comments

Comments

@thauk-copperleaf
Copy link

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.

@gvanrossum
Copy link
Member

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__.

@ilevkivskyi
Copy link
Member

This looks essentially like a duplicate of #293

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants