微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

VS Code 提到问题失败:“NameError: name 'functionName' is not defined -> VS Code 中的问题控制台没有更新?Python,pytest

如何解决VS Code 提到问题失败:“NameError: name 'functionName' is not defined -> VS Code 中的问题控制台没有更新?Python,pytest

我尝试关注 youtube pytest 研讨会。 我使用 VS Code 来处理它。并学会正确使用 VS Code...

所以我有包含函数 total 和 join 的文件 ls24_module.py。 我在测试文件中导入的,据我所知它有效并且是正确的。甚至 pytest 也显示所有 6 个测试都通过了。

但是 VS Code 仍然显示 test_join_use_case 的错误
Fail: NameError: name 'join' is not defined. 并且错误在第 17 行。这是第一次正确。我在导入中添加了 join 并将所有内容都写下来。但错误并没有消失。所以我复制了这部分测试并将其包含到最后。因为测试的顺序并不重要。但这仍然表明问题消息不会自行更新。由于
def test_join_use_case() 现在定义在第 23 行。但我没有找到删除问题的方法,让 VS Code 再次检查文件。它只表明第17行仍然存在问题。

VSCode 中未更新的问题消息图片

1

使用的文件是:

ls24_module.py

from typing import List

# skeleton function 
def total(xs: List[float]) -> float:
    """Total return the sum of xs."""
    result: float = 0.0
    """For each x float in xs,add it to result"""
    for x in xs:
        result += x
    return result

def join(xs: List[int],delimiter: str) -> str:
    """Produce a string where subsequent items are separated by delimiters."""
    generated_string: str = ""
    for item in xs:
        if generated_string == "": # Don't put delimiter before first
            generated_string = str(item)
        else:
            generated_string += delimiter + str(item)
    return generated_string

ls24_module_test.py

"""An example of a test module in pytest."""

from ls24_module import total,join

def test_total_empty() -> None:
    """Total of empty list should be 0.0"""
    assert total([]) == 0.0

def test_total_single_item() -> None:
    """Total of a single item list should be the first item's value."""
    assert total([110]) == 110.0

def test_total_multiple_item() -> None:
    """Total of a multiple item list should be the sum of the items in the list."""
    assert total([1.0,2.0,3.0]) == 6.0

def test_join_edge_single_item() -> None:
    assert join([1],",") == "1"

def test_join_edge_empty_delimiter() -> None:
    assert join([1,2,3],"") == "123"
        
def test_join_use_case() -> None:
    assert join([1,") == "1,3"

有人能帮忙吗。

更新

解决:重新启动 VS Code 并重新加载 Windows 后,错误消失。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。