puggan icon

TypedDictKeys.py

puggan | PRO | 06/29/20 12:09:51 PM UTC | 0 ⭐ | 1994 👁️ | Never ⏰ | []
Python |

400 B

|

None

|

0 👍

/

0 👎

from typing import Literal, TypedDict
 
 
class Td(TypedDict):
    bar: str
    foo: str
 
 
manual_literal = Literal['bar', 'foo']
 
def hello_td_str(key: str, td: Td):
    return 'Hello ' + td[key]
 
def hello_td_manual(key: manual_literal, td: Td):
    return 'Hello ' + td[key]
 
 
my_td = {'bar': 'Bar', 'foo': 'Foo'}
 
print(hello_td_manual('bar', my_td))
print(hello_td_str('foo', my_td))

Comments