首页 > 解决方案 > mypy 键入导致意外回溯

问题描述

我正在尝试解决GitHub 问题中描述的问题(“具有函数字段的类错误地认为第一个参数是 self”)。

from dataclasses import dataclass
from typing import TypeVar, Generic, Any, Iterable, List

T = TypeVar("T")

# See https://github.com/python/mypy/issues/5485


@dataclass
class Box(Generic[T]):
    inner: T

    @property
    def unboxed(self) -> T:
        return self.inner

不过,我遇到了这样的回溯:

但是,在从另一个模块导入 (仅(!))上面的代码时,我遇到了这样的回溯:

(py) sugarline:~/src/oss/ormsnack write-compile-eval
Traceback (most recent call last):
[.....]
  File "/Users/jacob/src/oss/ormsnack/ormsnack/ng_desc.py", line 8, in <module>
    from kingston.kind import Box  # type: ignore
  File "kingston/kind.py", line 12, in <module>
AttributeError: attribute '__dict__' of 'type' objects is not writable

不过,谷歌搜索只会让我陷入似乎已经过时的旧错误(https://bugs.python.org/issue38099、https://github.com/man-group/arctic/issues/17)等。

有没有人能够找出解决方法?

标签: pythonmypy

解决方案


推荐阅读