2

In PyCharm Code completion > "Basic Completion" > "Invoke Basic Completion" > "Dictionaries" I see that, if you hard-code a dictionary to some values, you can use code completion when writing code about that dictionary.

But obviously, in many cases, you will work with a dict and you have some idea in advance what the structure of that dict will be, and you don't want to hard-code the dict into your code. For example, maybe you're parsing some YAML or JSON that has an expected structure.

It would be really nice if you could "hint" the structure in Python, so you could easily and rapidly code all the places you use that dictionary. Is that possible?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Edward Ned Harvey
  • 6,525
  • 5
  • 36
  • 45
  • 2
    Are you using type annotations? Docstrings? There has to be *something* for PyCharm to base it on. – jonrsharpe May 18 '19 at 11:26
  • @jonrsharpe, that's my question. I'm not aware of anything I could add to code, that would provide hints that the IDE could use to complete the keys of a dict when used. Is it possible? Type annotations don't provide info about dict keys (afaik). What about docstrings? I'll have to go search for docstrings, now that you mentioned it, cuz I didn't know what docstrings are until now. – Edward Ned Harvey May 18 '19 at 11:33
  • Type annotations can absolutely provide info about dict key (and value) types. The typing module has a generic for that: https://docs.python.org/3/library/typing.html#typing.Mapping. Have you read PyCharm's page about type hinting? https://www.jetbrains.com/help/pycharm/type-hinting-in-product.html – jonrsharpe May 18 '19 at 11:35
  • @jonrsharpe See the link in the question. I'm not asking about hinting the types of the keys and values. I'm asking about hinting the names of the keys. Like, you have a dict, and you know in advance that at runtime it will probably have a "config" key and a "query" key and a "result" key. You want to code-complete my_dict["config"] – Edward Ned Harvey May 18 '19 at 11:40
  • Oh, I see; then not until https://www.python.org/dev/peps/pep-0589/ AFAIK. – jonrsharpe May 18 '19 at 11:51
  • Yup, that's totally what I'm looking for... Thanks... If you want to paste that reply into an Answer, I would accept it as an answer. – Edward Ned Harvey May 18 '19 at 11:59
  • Possible duplicate of [Python 3 dictionary with known keys typing](https://stackoverflow.com/questions/44225788/python-3-dictionary-with-known-keys-typing) – Georgy Oct 10 '19 at 15:18

2 Answers2

4

As far as I'm aware, there's no agreed way to type-hint the specific key names and associated values of a Python dictionary until TypedDict is introduced in Python 3.8.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
4

There is no "official" support for typed (hinted) dictionaries in Python yet. However, the mypy_extensions package does expose TypedDict which supports the following syntax for defining typed dictionaries which can be validated by MyPy.

DictT = TypedDict(
    'DictT ',
    {
         'field': str, 
         'int_field': int,
    }
)

https://www.python.org/dev/peps/pep-0589/#alternative-syntax

You could then integrate MyPy into PyCharm but according to their bug tracker there is no direct support for type hinting TypedDicts https://youtrack.jetbrains.com/issue/PY-36008 .

henrycjc
  • 663
  • 7
  • 20