如果满足特定条件,我想退出工作:
jobs:
foo:
steps:
...
- name: Early exit
run: exit_with_success # I want to know what command I should write here
if: true
- run: foo
- run: ...
...
Run Code Online (Sandbox Code Playgroud)
怎么做?
(相关但不重复:如何注释可以作为属性实现的属性?)
我想创建一个Protocol,其中的字段可以通过简单类型和属性来实现。例如:
class P(Protocol):
v: int
@dataclass
class Foo(P):
v: int
class Bar(P):
@property
def v(self) -> int: # ERROR
return
Run Code Online (Sandbox Code Playgroud)
但上面的代码没有进行类型检查。我应该如何修复它?
注意:我想解决这个问题而不重写Foo和Bar,因为Foo和Bar不是我实现的。
根据这个问题,下面的代码不是解决方案,因为只读成员property和简单成员具有细微不同的语义。
class P(Protocol):
@property
def v(self) -> int: # declare as property
...
Run Code Online (Sandbox Code Playgroud)
Protocol由于差异,皮赖特否认了这一点。
a是一个Vec<i32>可以在一个表达式中可变且不可变地引用的 a :
fn main() {
let mut a = vec![0, 1];
a[0] += a[1]; // OK
}
Run Code Online (Sandbox Code Playgroud)
我认为这个编译是因为i32implements Copy,所以我创建了另一种类型,Copy它像第一个例子一样实现和编译它,但它失败了:
use std::ops::AddAssign;
#[derive(Clone, Copy, PartialEq, Debug, Default)]
struct MyNum(i32);
impl AddAssign for MyNum {
fn add_assign(&mut self, rhs: MyNum) {
*self = MyNum(self.0 + rhs.0)
}
}
fn main() {
let mut b = vec![MyNum(0), MyNum(1)];
b[0] += b[1];
}
Run Code Online (Sandbox Code Playgroud)
error[E0502]: cannot borrow `b` as immutable because it is also borrowed …Run Code Online (Sandbox Code Playgroud) 我创建了一个包,包含Pipfile,我想用 docker 进行测试。
我想用 pip 安装用 Pipfile 编写的包,而不创建 virutalenv。
# (do something to create some-file)
RUN pip install (some-file)
Run Code Online (Sandbox Code Playgroud)
怎么做?
我想与 mypy 一起使用property setter。属性 getter 和 setter 的类型不同:
from typing import List, Iterable
class Foo:
@property
def x(self) -> List[int]:
...
@x.setter
def x(self, new_x: Iterable[int]):
...
foo = Foo()
foo.x = (1, 2, 3) # error: Incompatible types in assignment (expression has type "Tuple[int, int, int]", variable has type "List[int]")
Run Code Online (Sandbox Code Playgroud)
我该如何处理这个错误?
我用 Poetry 创建了一个 python 项目“foo”。这是以下内容pyproject.toml:
[tool.poetry]
name = "bar"
version = "0.1.0"
description = ""
[tool.poetry.dependencies]
python = ">=3.5"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
Run Code Online (Sandbox Code Playgroud)
该包兼容Python3.5。我想要黑色格式化程序,它与Python3.5不兼容。我觉得使用Python>=3.6开发是没有问题的,但是无法安装black formatter:
[tool.poetry]
name = "bar"
version = "0.1.0"
description = ""
[tool.poetry.dependencies]
python = ">=3.5"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
Run Code Online (Sandbox Code Playgroud)
所以我直接安装了黑色pip:
$ poetry add black --dev
[SolverProblemError]
The current project's Python requirement (>=3.5) is not compatible with some of the required packages Python …Run Code Online (Sandbox Code Playgroud) 启动我的 powershell 需要大约 3 秒,所以我想减少它。如何知道哪个进程损害了powershell的启动性能?我想使用像vim profiling这样的工具。
不顺利的是在 nox 会话中安装 dev 依赖项。
我有noxfile.py如下所示:
import nox
from nox.sessions import Session
from pathlib import Path
__dir__ = Path(__file__).parent.absolute()
@nox.session(python=PYTHON)
def test(session: Session):
session.install(str(__dir__)) # I want to use dev dependency here
session.run("pytest")
Run Code Online (Sandbox Code Playgroud)
如何在 nox 会话中安装开发依赖项?
我创建了一个数据类Foo,它接受任何可以转换为的类型int:
import dataclasses
@dataclasses.dataclass
class Foo:
a: int
def __post_init__(self):
# Here `self.a` is converted to int, so this class accepts any type that can be converted to int
self.a = int(self.a)
# mypy error: Argument 1 to "Foo" has incompatible type "str"; expected "int",
foo = Foo("1")
print(foo)
print(foo.a + 2)
Run Code Online (Sandbox Code Playgroud)
输出:
Foo(a=1)
3
Run Code Online (Sandbox Code Playgroud)
但是,mypy 报告以下错误:
error: Argument 1 to "Foo" has incompatible type "str"; expected "int"
Run Code Online (Sandbox Code Playgroud)
Foo.a如果我修复to的类型Union[str, int],mypy 会报告另一个错误:
error: Unsupported …Run Code Online (Sandbox Code Playgroud)