简体   繁体   中英

How to type-hint a getter to only allow keys of dict?

I wonder how to make this getter more type-safe:

VALUES = {
   '1': 'One',
   '2': 'Two',
   '3': 'Three'
}


def get(key : str) -> str:
    return VALUES[key]

Instead of the type str I would love to have a keyof VALUES and type(VALUES[key]) types.

get('4') should throw a invalid type warning then as this key does not exist. Not sure if this is possible with Python as I properly live in a TypeScript wonderland... :-)

TypeScript would properly look like this:

get<K extends keyof VALUES>(key : K): typeof K
{
    return VALUES[key];
}

You cannot do this in general . However, you can accomplish what you want in this particular case using typing.Literal :

import typing
def get(key: typing.Literal['1','2','3']) -> str:
    return VALUES[key]

As was suggested in the comments, the enum module provides a nice solution here. By mixing in str with enum.Enum , we can create an Enum that is fully backwards-compatible with str (ie, can be used wherever a str type is expected.

from enum import Enum

# The mixin type has to come first if you're combining
# enum.Enum with other types 
class Values(str, Enum):
    N1 = 'One'
    N2 = 'Two'
    N3 = 'Three'

If we enter this definition into the interactive console, we'll see that this class has the following behaviour:

>>> Values['N1']
<Values.N1: 'One'>
>>> Values('One')
<Values.N1: 'One'>
>>> Values.N1
<Values.N1: 'One'>
>>> Values('One') is Values.N1 is Values['N1']
True
>>> Values.N1.name
'N1'
>>> Values.N1.value
'One'
>>> Values.N1 == 'One'
True
>>> Values.N1 is 'One'
False
>>> Values.N1.startswith('On')
True
>>> type(Values.N1)
<enum 'Values'>
>>> for key in Values:
...     print(key)
...
Values.N1
Values.N2
Values.N3
>>> list(Values)
[Values.N1, Values.N2, Values.N3]

As you can see, we have defined a new type which:

  • Provides both dynamic dictionary-like access to members, as well as more type-safe forms of access.
  • Is fully backwards-compatible with str — it can freely be compared with str objects, and str methods can be used on its members.
  • Can be used in type hints as an alternative to typing.Literal . If I have a function like this:
def do_something(some_string):
    if some_string not in ('One', 'Two', 'Three'):
        raise Exception('NO.')

then I can either annotate it like this:

from typing import Literal

def do_something(some_string: Literal['One', 'Two', 'Three']) -> None:
   ...

or like this (in which case you'll have to pass in a member of the Values enum rather than a vanilla string, or the type-checker will raise an error):

# Values enum as defined above 
def do_something(some_string: Values) -> None:
   ...

There's an even more detailed guide to the intricacies of python Enum s here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM