重现此错误的最小示例如下所示
from enum import Enum
from typing import List
class Stuff(Enum):
A = 'something'
B = 'something_else'
C = 'even_more'
@classmethod
@property
def important_stuff(cls) -> List[str]:
return [cls.A.value, cls.C.value]
pri
我正在尝试使用,但我发现与mypy相比,它会抛出额外的错误。
例如:
$ cat t.py
class Cat:
other = None
$ mypy t.py
Success: no issues found in 1 source file
$ dmypy run -- t.py
t.py:3: error: Need type annotation for 'other'
Found 1 error in 1 file (checked 1 source file)
有没有办法让dmypy与mypy达成一致?
其他信息:
$ python --vers
我试图用一些自定义方法扩展pythons的list,因为我正在创建一个继承自UserList的类。
我不知道什么是正确的方式,我想让mypy很好地发挥与UserList。
我向咨询,并在mypy中搜索UserList,但什么也找不到。
使用:
类型0.641
python 3.7.0
这是我想要取得的成果的一个很小的例子
from collections import UserList
from typing import List, Optional, Union
class A:
...
class B:
...
class C:
...
随着SQLAlchemy的复杂性,它们往往会导致循环导入,所以我想出了一种方法来“重新打开”一个类似于Ruby的类:
def reopen(cls):
"""
Moves the contents of the decorated class into an existing class and returns that.
Usage::
from .other_module import ExistingClass
@reopen(ExistingClass)
class ExistingC
from typing import Callable, TypeVar
T = TypeVar('T')
IndicatorFunction = Callable[[T], bool]
# mypy accepts this annotation.
s1: Callable[[T], bool] = lambda x: False
# mypy rejects this annotation, but it's just an alias of the first one.
s2: IndicatorFunction[T] = lambda x: False
我在我的python项目中使用进行类型检查。我还使用PyYAML来读写项目配置文件。不幸的是,在使用时,这会在尝试导入本机库的try/except子句中生成虚假错误:
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
在我的系统上,CLoader和CDumper不存在,这会导致错误error: Module 'yaml' has no a
我使用的是Python3.5,我想使用来启用静态类型检查。但是当我尝试定义二叉树时,我遇到了错误:
class BinaryTreeNode(object):
def __init__(self, value, left: BinaryTreeNode=None, right:BinaryTreeNode=None):
self.value = value
self.left = left
self.right = right
错误消息:
NameError: name 'BinaryTreeNode' is not de
以下是我到目前为止所拥有的:
def remove_duplicates(values):
output = []
seen = set()
for value in values:
# If value has not been encountered yet,
# ... add it to both list and set.
if value not in seen:
output.append(value)
seen.add(value)
return ou
我正在编写一个程序,它读取由字符串组成的文本文件,每个字符串在一行上。基本上我是这样做的:
...
char* name;
char* buffer = malloc(sizeof(char) * SIZE); //size is a defined constant in the header
while(fgets(buffer, SIZE, pf)){ //pf is the opened stream
name = malloc(sizeof(char) * SIZE);
strcpy(name, strtok(buffer, "\n"));
m
稍微有点背景,我基本上需要定义一个int包装器类型,比如MyInt (以及其他一些类),以及另一个可以接受MyInt对象以及其他类型对象的通用Interval类型。由于Interval所接受的类型不属于一个整洁的层次结构,我认为这将是实验Protocol的一个完美的用例,在我的例子中,它需要几个方法和几个@classmethod,所有的方法都返回一个“自我类型”,即MyInt.my_method返回一个MyInt。这是一个MCVE:
from dataclasses import dataclass
from typing import Union, ClassVar, TypeVar, Ge