如何告诉 python 传递给函数的意外变量类型在上下文中是正确的
How to tell python that the unexpected variable type passing to a function is correct in the context
我是 Python 3 的新手,正在使用 pytest 进行测试。有时,我想测试一下,当我向函数提供它不应处理的类型时,它是否会引发 TypeError 或 AssertionError。考虑这个简单的例子:
import pytest
def concatenate_string(first_string: str, second_string: str) -> str:
return first_string + second_string
def test_concatenate_string_raises_type_error():
with pytest.raises(TypeError) as pytest_wrapped_e:
concatenate_string('abc', 1)
assert e.type == TypeError
我的问题是,当我向函数 concatenate_string 提供整数 1 时,PyCharm 警告我有关分配的意外类型。我知道这是一个正确的行为,但是有没有办法告诉程序(例如通过一些其他注释):“我知道我在做什么,我真的很想在这里拥有这种类型,不要警告它."?
我知道我可以让编辑忽略它,但我觉得有点不对劲。
concatenate_string('abc', 1) # type: ignore
这应该禁止对该行进行类型检查
我是 Python 3 的新手,正在使用 pytest 进行测试。有时,我想测试一下,当我向函数提供它不应处理的类型时,它是否会引发 TypeError 或 AssertionError。考虑这个简单的例子:
import pytest
def concatenate_string(first_string: str, second_string: str) -> str:
return first_string + second_string
def test_concatenate_string_raises_type_error():
with pytest.raises(TypeError) as pytest_wrapped_e:
concatenate_string('abc', 1)
assert e.type == TypeError
我的问题是,当我向函数 concatenate_string 提供整数 1 时,PyCharm 警告我有关分配的意外类型。我知道这是一个正确的行为,但是有没有办法告诉程序(例如通过一些其他注释):“我知道我在做什么,我真的很想在这里拥有这种类型,不要警告它."?
我知道我可以让编辑忽略它,但我觉得有点不对劲。
concatenate_string('abc', 1) # type: ignore
这应该禁止对该行进行类型检查