我试图在我的python代码上执行一些输入操作,但我得到了以下错误:“索引赋值的目标不受支持”
在一个简化的示例中,它相当于以下代码:
from pathlib import Path
from typing import (Literal, Mapping,
Optional, Union)
STRAND = Literal["+", "-"]
PATH = Union[str, Path]
fastq_files: Mapping[STRAND, Optional[PATH]] = { # simultaneous annotation and assignment
"+": None,
"-": None}
reads_dir = Path("/tmp")
fastq_files["+"] = reads_dir.joinpath( # mypy error
"plus.fastq.gz")
fastq_files["-"] = reads_dir.joinpath( # mypy error
"minus.fastq.gz")错误发生在将字典值中的None替换为Path时。
既然PATH是Union[str, Path],为什么应该是Optional[PATH]类型的值不能替换为Path类型的值呢?我原以为Path是与Union[str, Path]兼容的,而Optional[Union[str, Path]]又是与have兼容的。
为什么当我在赋值之前注释字典而不是在赋值时注释它时,错误就消失了(见下文)?
from pathlib import Path
from typing import (Literal, Mapping,
Optional, Union)
STRAND = Literal["+", "-"]
PATH = Union[str, Path]
fastq_files: Mapping[STRAND, Optional[PATH]] # annotation before assignment
fastq_files = {
"+": None,
"-": None}
reads_dir = Path("/tmp")
fastq_files["+"] = reads_dir.joinpath( # no mypy error
"plus.fastq.gz")
fastq_files["-"] = reads_dir.joinpath( # no mypy error
"minus.fastq.gz")上面显示了None可以被类型为Optional[Union[str, Path]]的"slot“中的Path替换。
这是否意味着,当我在赋值的同时进行注释时,实际的类型将被“简化”为与赋值兼容的最严格的类型?(结果是“槽”得到了一个更具限制性的类型)
发布于 2020-02-29 03:02:56
问题是映射应该是一个只读协议--如果检查type hints for Mapping,就会发现它实际上没有定义__setitem__方法。
如果你想改变你的映射,你需要使用Dict或者MutableMapping。
按照另一个答案的建议切换到使用TypedDict也是可行的,因为TypedDicts被假定为Dict的一个子类型,因此本质上是可变的。
发布于 2020-02-29 01:30:01
我认为TypedDict解决了这个(奇怪的)问题:
from pathlib import Path
from typing import (Literal, Mapping,
Optional, Union, TypedDict)
STRAND = Literal["+", "-"]
PATH = Union[str, Path]
FASTQ = TypedDict("FASTQ", {"+": Optional[PATH], "-": Optional[PATH]})
fastq_files: FASTQ = {
"+": None,
"-": None}
reads_dir = Path("/tmp")
fastq_files["+"] = reads_dir.joinpath("plus.fastq.gz")
fastq_files["-"] = reads_dir.joinpath("minus.fastq.gz")https://stackoverflow.com/questions/60456140
复制相似问题