如何使用 mypy 键入提示自定义 类
How to typehint custom classes with mypy
给定以下几个模块:
# mod_1.py
from dataclasses import dataclass
@dataclass
class C:
x : int
# mod_2.py
from mod_1 import C
c = C(x = 3)
# mod_3.py
from mod_2 import c
def f(something : <type c>) -> None:
print(c.x)
我想知道应该用什么代替 <type c>
如果不导入类型本身,则可以使用字符串。
def f(something: 'C') -> None:
print(c.x)
您还可以导入类型:
from mod_2 import c, C
def f(something: C) -> None:
print(c.x)
给定以下几个模块:
# mod_1.py
from dataclasses import dataclass
@dataclass
class C:
x : int
# mod_2.py
from mod_1 import C
c = C(x = 3)
# mod_3.py
from mod_2 import c
def f(something : <type c>) -> None:
print(c.x)
我想知道应该用什么代替 <type c>
如果不导入类型本身,则可以使用字符串。
def f(something: 'C') -> None:
print(c.x)
您还可以导入类型:
from mod_2 import c, C
def f(something: C) -> None:
print(c.x)