I found a pyi file which has the following def
def most_common(self, n: Optional[int] = ...) -> List[Tuple[_T, int]]: ...
How could this happen? List is not defined, and no implementation?
Just highlight some valuable suggestions here for followers:
List is imported from the typing module; it's not the same thing as list. The .pyi file doesn't need to import it because stub files are never executed; they just have to be syntactically valid Python
If you use from future import annotations, you won't have to import typing to use List et al. in function annotations in .py files, either, since function annotations will be treated as string literals. (Starting in Python 4, that will be the default behavior. See PEP 563 for details.)
You are looking at the
pyi
file which is used solely for annotations. It is never executed by the Python interpreter. You can learn more aboutpyi
files by reading PEP484.Using a debugger, put a breakpoint on the line where you call
most_common
and then step into the method.Python 3.7 implementation.
...\Lib\collections\__init__.py
:_heapq.nlargest
(in...\Lib\heapq.py
) implementation: