在哪里可以找到 set.symmetric_difference 等内置类型方法的类型注释?

Where can I find the type annotations for methods of built-in types like set.symmetric_difference?

我知道 CPython 的内置函数是用 C 实现的。但是我对内置类型的方法的类型提示或注释感兴趣。

比如我想知道set.symmetric_difference_update()是怎么标注的。但是我找不到。

内置函数似乎也需要类似的类型提示才能使 mypy 等程序正常工作。

您可以找到 set.symmetric_difference_update() here 的注释。

class set(MutableSet[_T], Generic[_T]):
    # ...
    def symmetric_difference_update(self, s: Iterable[_T]) -> None: ...

以下是 typeshed 存储库的 readme.md 文件的一些摘录:

Typeshed contains external type annotations for the Python standard library and Python builtins, as well as third party packages as contributed by people external to those projects.

This data can e.g. be used for static analysis, type checking or type inference. [...]

If you're just using mypy (or pytype or PyCharm), as opposed to developing it, you don't need to interact with the typeshed repo at all: a copy of standard library part of typeshed is bundled with mypy.