- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用工厂函数来生成一些类型注释 - 特别是针对 tuple
类型。我有一个可以正常工作的工厂版本(例如,它在 MyPy 中编译、运行和检查令人满意):
import typing as tx
HomogenousTypeVar = tx.TypeVar('HomogenousTypeVar')
TupleTypeReturnType = tx.Type[tx.Tuple[HomogenousTypeVar, ...]]
def TupleType(length: int,
tuptyp: tx.Type[HomogenousTypeVar] = str) -> TupleTypeReturnType:
""" Create a type annotation for a tuple of a given type and length """
assert length > 0
return tx.Tuple[tuple(tuptyp for idx in range(length))]
...其用法如下:
class Thing(object):
__slots__: TupleType(2) = ('yo', 'dogg')
other_fields: TupleType(4) = ('i', 'heard',
'you', 'like')
# etc, or what have you
...但是,当我尝试添加对 typing.ClassVar
注释的支持时,我没有成功,如下所示:
import typing as tx
HomogenousTypeVar = tx.TypeVar('HomogenousTypeVar')
TupleTypeReturnType = tx.Union[tx.Type[tx.Tuple[HomogenousTypeVar, ...]],
tx.Type[tx.ClassVar[tx.Tuple[HomogenousTypeVar, ...]]]]
def TupleType(length: int,
tuptyp: tx.Type[HomogenousTypeVar] = str,
clsvar: bool = False) -> TupleTypeReturnType:
""" Create a type annotation for a tuple of a given type and length,
specifying additionally whether or not it is a ClassVar """
assert length > 0
out = tx.Tuple[tuple(tuptyp for idx in range(length))]
return clsvar and tx.ClassVar[out] or out
…在此更改之后,代码甚至不会最初编译 - 它无法通过 typing
模块深处的 TypeError
进行编译:
TypeError: typing.ClassVar[typing.Tuple[~HomogenousTypeVar, ...]] is not valid as type argument
…随着错误的发生,我觉得这有点像电话进来的;我的意思是,难道打字
中的一切都应该以某种方式成为有效的类型参数吗?
在 the typing
source code related to ClassVar
,文档字符串中提到了对其使用的一些限制 - 但这不是其中之一。我是否缺少一些明显的东西?我尝试以这种方式使用此注释是否是堂吉诃德式的?我还可以尝试什么?
最佳答案
您确定您的原始代码片段确实使用 mypy 进行了类型检查吗?当我尝试使用 Mypy 0.620 或 github 上的最新版本运行它时,出现以下错误:
test.py:13: error: invalid type comment or annotation
test.py:13: note: Suggestion: use TupleType[...] instead of TupleType(...)
test.py:14: error: invalid type comment or annotation
test.py:14: note: Suggestion: use TupleType[...] instead of TupleType(...)
我也无法重现您在 ClassVar 代码中遇到的错误 - 当我尝试运行它时,出现以下错误:
test.py:4: error: Invalid type: ClassVar nested inside other type
test.py:6: error: Incompatible default for argument "tuptyp" (default has type "Type[str]", argument has type "Type[HomogenousTypeVar]")
test.py:12: error: Invalid type alias
test.py:13: warning: Returning Any from function declared to return "Union[Type[Tuple[HomogenousTypeVar?, ...]], Type[Tuple[HomogenousTypeVar?, ...]]]"
test.py:15: error: Name 'Thing' is not defined
test.py:16: error: Revealed type is 'Any'
您确定您确实在运行 mypy,而不是仅仅运行代码吗?例如。如果您只运行 python3 test.py,您基本上会跳过所有类型检查(除了打字模块中内置的一些最低限度的健全性检查之外)。
如果您想对代码进行类型检查,则需要 pip-install mypy 并运行 python3 -m mypy test.py
。
无论如何,所有这些错误消息都是预期行为 - mypy(以及任何其他符合 PEP 484 的类型检查器)只能静态分析您的代码,并且不会尝试运行或分析任何代码。工厂函数/您可以尝试编写的任何类型提示生成函数。
因此,不幸的是,如果您希望 PEP 484 兼容工具能够分析您的代码,您将无法将生成的类型提示与 ClassVars 一起使用 - 它们无法理解/解释您的原始集合类型提示,添加 ClassVars 当然不会有帮助。
如果你想生成类型提示,我能想到的唯一真正的选择是在 Python 之上发明某种迷你语言或宏系统,当运行时,将生成 Python 代码。然后,您将运行并检查生成的代码而不是宏化的 Python 语言。
但我真的不建议这样做——这是一个非常脆弱的黑客行为。
<小时/>更广泛地说,每当您开始遇到这些类型相关的限制时,我认为这表明您的代码太复杂了。我会考虑简化您的代码,或者(如果不可能的话)切换到 Haskell 或 Idris 等语言,这将使您使用更具表现力(尽管更复杂)的类型系统。
例如,在本例中,您试图概括元组类型 - 这使我推断您的代码库包含许多不同种类的不同数量和类型的元组。
这让我觉得有点可疑——我会考虑将其中一些元组转换为常规类或(如果您仍然需要类似元组的功能)namedtuple 。 Dataclasses (这是新的 as of Python 3.7 )在这里也很方便。
这些解决方案还有助于使您的代码更具可读性——您现在可以为每种不同类型的元组指定具体的名称和含义。
或者,如果您只有几种不同类型的元组,但到处都使用这些元组,则可以尝试使用 type aliases因此您不必一遍又一遍地重复输入相同的(长)类型。例如。而不是这样做:
def foo(x: Tuple[int, int, int, int]) -> None: ...
...你可以这样做:
IpAddress = Tuple[int, int, int, int]
def foo(x: IpAddress) -> None: ...
关于python - Python 3.7中使用工厂函数生成注释类型时出现 “typing.ClassVar”问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52262822/
假设a是张量,那么有什么区别: 类型(a) a.类型 a.type() 我找不到区分这些的文档。 最佳答案 type 是 python 内置方法。 它将返回对象的类型。喜欢 torch.Tensor.
什么是 Type 1 的居民的例子?两者都不是 Type也不是Type的居民?在 Idris REPL 中进行探索时,我无法想出任何东西。 更准确地说,我正在寻找一些 x除了 Type产生以下结果:
我找到了一些资源,但我不确定我是否理解。 我找到的一些资源是: http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb2ff3358411d1829f00
这两个函数原型(prototype)有什么区别? void apply1(double(f)(double)); void apply2(double(*f)(double)); 如果目标是将提供的函
http://play.golang.org/p/icQO_bAZNE 我正在练习使用堆进行排序,但是 prog.go:85: type bucket is not an expression
假设有一个泛型定义的方法信息对象,即一个方法信息对象,这样的方法Info.IsGenericMethodDefinition==TRUE:。也可以说它们也有一个泛型参数列表:。我可以使用以下命令获取该
在具有依赖类型的语言中,您可以使用 Type-in-Type 来简化语言并赋予它很多功能。这使得语言在逻辑上不一致,但如果您只对编程感兴趣而不对定理证明感兴趣,这可能不是问题。 在 Cayenne
根据 Nim 手册,变量类型是“静态类型”,而变量在内存中指向的实际值是“动态类型”。 它们怎么可能是不同的类型?我认为将错误的类型分配给变量将是一个错误。 最佳答案 import typetrait
假设您有以下结构和协议(protocol): struct Ticket { var items: [TicketItem] = [] } struct TicketItem { } prot
我正在处理一个 EF 问题,我发现它很难调试...以前,在我的系统中有一个表类型继承设置管理不同的用户类型 - 所有用户共有的一种根类型,以及大致基于使用该帐户的人员类型的几种不同的子类型。现在,我遇
这是我的 DBManager.swift import RealmSwift class DBManager { class func getAllDogs() -> [Dog] {
我正在尝试使用傅里叶校正图像中的曝光。这是我面临的错误 5 padded = np.log(padded + 1) #so we never have log of 0 6 g
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
请考虑以下设置: protocol MyProcotol { } class MyModel: MyProcotol { } enum Result { case success(value:
好吧,我将我的 python 项目编译成一个可执行文件,它在我的电脑上运行,但我将它发送给几个 friend 进行测试,他们都遇到了这个错误。我以前从未见过这样的错误。我使用 Nuitka 来编译代码
当我尝试训练我的模型时"ValueError: Type must be a sub-type of ndarray type"出现在 line x_norm=(np.power(x,2)).sum(
我尝试在另一个类中打断、计数然后加入对象。所以我构建协议(protocol): typealias DataBreaker = () -> [Double] typealias DataJoiner
我正在使用 VS 2015 更新 3、Angular 2.1.2、Typescript 2.0.6 有人可以澄清什么是 typings 与 npm @types 以及本月很难找到的任何其他文档吗? 或
我正在考虑从 VS2010 更改为 Mono,因此我通过 MoMA 运行我的程序集,看看我在转换过程中可能遇到多少困难。在生成的报告中,我发现我不断收到此错误: bool Type.op_Equali
主要问题 不太确定这是否可能,但由于我讨厌 Typescript 并且它使我的编码变得困难,我想我会问只是为了确定。 interface ISomeInterface { handler: ()
我是一名优秀的程序员,十分优秀!