Search code examples
pythonpython-typingmypytypeshed

Where can I find the type annotations for methods of built-in types?


I know that built-ins for CPython are implemented in C. But I'm interested in the type-hints, or annotations, for the methods of the built-in types.

For example, I'd like to know how set.symmetric_difference_update()is annotated. But I can't find it.

It also seems like the built-in functions would need similar type-hints for programs like mypy to work.


Solution

  • You can find annotation for set.symmetric_difference_update() here.

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

    Below are some excerpts from the readme.md file of the typeshed repository:

    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.