How can I make the autocomplete know about pseudofields in a class?

Answered

For example, my model Object has a pseudo-field called photos of type List[ObjectPhoto]. The field is generated by the ORM library I use, so there is no manually written @property or something in the code. So, PyCharm never understands that the expression object.photos[0] is of type ObjectPhoto and does not show any useful completion.

How can I document this pseudo field for PyCharm?

0
2 comments

Hi Max,

 

It can be achieved by using type hints. You could either create a separate .pyi stub with an auxiliary definition of this class that contains all such generated fields or just put them directly in the original class itself. I mean something like the following:

class Object(metaclass=Meta):
photos = ... # type: List[ObjectPhoto]
# photos: List[ObjectPhoto] # Python 3.6 syntax, PEP 526

# the remaining part of the class
1
Avatar
Permanently deleted user

Thank you. I didn't know about the stub files at all.

0

Please sign in to leave a comment.