嵌套字典的提示类型 [英] Type hint for nested dict

查看:126
本文介绍了嵌套字典的提示类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python脚本中解析的数据结构是一个json文件,在 json.load(file_handle)之后,该文件的类型为 < class'dict'> 。到现在为止还挺好。现在,对于使用它作为输入参数的函数,我想要解析的json的类型提示。我在打字文档中读到了 dict s作为参数,我应该使用 Mapping [key_type,value_type]

 从键入import映射

def foo(json_data:Mapping [str,str])->无:
...

我解析的json具有 str 型键和 str 型值,但通常情况下,其结构是高度递归的。因此,值更有可能是带有 str 键的 dict 甚至是这样的 dict s作为值。它是非常嵌套的,直到最深层次的最后一个dict最终都具有 str 键和 str 值。 / p>

那么如何更精确地表示此数据结构?我正在按照此问题,可能是:

  Union [Mapping [str,str],Mapping [str,Mapping]] 

但是它似乎只代表一个递归级别。

解决方案

简单的字典


可以简单地使用 Mapping [K,V] 作为另一个 Mapping 的值。假设您的json看起来像这样:

  {
person_1:{ money :1000},
person_2:{ money:1000}
}

在这种情况下,您可以使用如下所示的类型提示:

  Mapping [str,Mapping [str, int]] 


更严厉的格言


,但假设您有更复杂的东西,像这样:

  {
person_1:{
money: 1000,
职位:编码器,
}
}

那么您如何键入提示呢?您可以使用 Union (来自输入)来执行以下操作:

  Mapping [str,Mapping [str, Union [str,int]]] 

,但是现在我们会遇到像mypy这样的工具,认为 our_dict [ person_1] [ job] 具有类型 Union [str,int]


嗯,这并没有错,毕竟这就是我们所说的。可能有帮助的是 TypedDict(链接到pep对其进行解释) [typing_extensions]( https://pypi.org/project/typing-extensions/

  from来自typeing_extensions import TypedDict 

class Person(TypedDict):
钱:int
工作:str

#类型的提示,您将在函数中使用:
Mapping [str,Person]

注意:在python 3.8 TypedDict 输入


The kind of data structure I parse in my Python script is a json file which, after json.load(file_handle), is of type <class 'dict'>. So far so good. Now for a function using it as an input argument, I want a type hint for the parsed json. I read in the typing documentation, that for dicts as arguments, I should use Mapping[key_type, value_type]:

from typing import Mapping

def foo(json_data: Mapping[str, str]) -> None:
    ...

The json I parse has str-type keys and str-type values, but more often than not, its structure is highly recursive. Hence a value is more likely to be a dict with str keys and even such dicts as values. It is very nested, until, at the deepest level, the last dict finally has str keys and str values.

So how do I represent this data structure more precisely? I was thinking something, along the lines of this question, that it might be:

Union[Mapping[str, str], Mapping[str, Mapping]]

But it does seem to represent only one level of recursion. Is there a better way to type-hint this?

解决方案

simple dicts

you can simply use Mapping[K, V] as the value for another Mapping. let's say your json looks like this:

{
   "person_1": {"money": 1000},
   "person_2": {"money": 1000}
}

in this case you can use a type hint that looks like this:

Mapping[str, Mapping[str, int]]

harder dicts

but let's say you have something more complex, like this:

{
    "person_1": {
        "money": 1000,
        "job": "coder",
    }
}

how could you type hint this then? you can use Union (from typing) to do something like this:

Mapping[str, Mapping[str, Union[str, int]]]

but now we would run into a problem with tools like mypy thinking that our_dict["person_1"]["job"] has the type Union[str, int]

well it is not wrong, that is what we told it after all. something that could help here is TypedDict (link to pep explaining it) from [typing_extensions](https://pypi.org/project/typing-extensions/.

from typing_extensions import TypedDict

class Person(TypedDict):
    money: int
    job: str

# type hint you would use in your function:
Mapping[str, Person]

NOTE: in python 3.8 TypedDict is part of typing

这篇关于嵌套字典的提示类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆