85

I'm using Python 3 typing feature for better autocomplete.

Many times I have functions that return key/value (dictionary) with specific keys. super simple example:

def get_info(name):
    name_first_letter = name[0]
    return {'my_name': name, 'first_letter': name_first_letter}

I want to add type hinting to this function to tell others who use this function what to expect.

I can do something like:

class NameInfo(object):
    def __init__(self, name, first_letter):
        self.name = name
        self.first_letter = first_letter

and then change the function signature to:

def get_info(name) -> NameInfo:

But it requires too much code for each dictionary.

What is the best practice in that case?

7

2 Answers 2

106

As pointed out by Blckknght, you and Stanislav Ivanov in the comments, you can use NamedTuple:

from typing import NamedTuple


class NameInfo(NamedTuple):
    name: str
    first_letter: str


def get_info(name: str) -> NameInfo:
    return NameInfo(name=name, first_letter=name[0])

Starting from Python 3.8 you can use TypedDict which is more similar to what you want:

from typing import TypedDict


class NameInfo(TypedDict):
    name: str
    first_letter: str


def get_info(name: str) -> NameInfo:
    return {'name': name, 'first_letter': name[0]}
4
  • In this way you get some class-specific limitations, for example you can not pack your class using msgpack. Jul 4, 2019 at 11:18
  • how to define, that NameInfo has key with service symbols, for example last#name: str? Jan 23, 2020 at 10:25
  • @IvanBryzzhin You might try using alternative syntax.
    – Georgy
    Jan 31, 2020 at 14:39
  • This only works for str keys though
    – Jerinaw
    Aug 26, 2022 at 16:35
5

TypedDict can also be created using a typename and a dictionary where the keys are the keys and the values are the type of the values, respectively, of the dict to be returned by get_info(). This allows you to create keys that uses characters not in A-Za-z0-9_.

from typing import TypedDict

NameInfo = TypedDict('NameInfo', {'name': str, 'first-letter': str})

def get_info(name: str) -> NameInfo:
    return {'name': name, 'first-letter': name[0]}

x = get_info('cottontail')
print(x['first-letter'])   # c

Then on PyCharm, the key hint is there (also works on jupyter notebook too by pressing tab).

pycharm

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.