生成 visual studio 代码之类的 .pyi 文件

generate visual studio code's like .pyi file

visual studio 代码中的

.pyi 文件(python 扩展名)在我的例子中 _csv.pyi 文件与 mypy stubgen[= 生成的 .pyi 文件相比有更多关于类型提示的详细信息13=]

例如,在下面的代码中您可以看到两个 .pyi 文件

visual studio代码_csv.pyi:

from typing import Any, Iterable, Iterator, Protocol, Union
from typing_extensions import Literal

__version__: str

QUOTE_ALL: Literal[1]
QUOTE_MINIMAL: Literal[0]
QUOTE_NONE: Literal[3]
QUOTE_NONNUMERIC: Literal[2]

class Error(Exception): ...

class Dialect:
    delimiter: str
    quotechar: str | None
    escapechar: str | None
    doublequote: bool
    skipinitialspace: bool
    lineterminator: str
    quoting: int
    strict: int
    def __init__(self) -> None: ...

_DialectLike = Union[str, Dialect, type[Dialect]]

class _reader(Iterator[list[str]]):
    dialect: Dialect
    line_num: int
    def __next__(self) -> list[str]: ...

class _writer:
    dialect: Dialect
    def writerow(self, row: Iterable[Any]) -> Any: ...
    def writerows(self, rows: Iterable[Iterable[Any]]) -> None: ...

class _Writer(Protocol):
    def write(self, __s: str) -> object: ...

def writer(csvfile: _Writer, dialect: _DialectLike = ..., **fmtparams: Any) -> _writer: ...
def reader(csvfile: Iterable[str], dialect: _DialectLike = ..., **fmtparams: Any) -> _reader: ...
def register_dialect(name: str, dialect: Any = ..., **fmtparams: Any) -> None: ...
def unregister_dialect(name: str) -> None: ...
def get_dialect(name: str) -> Dialect: ...
def list_dialects() -> list[str]: ...
def field_size_limit(new_limit: int = ...) -> int: ...

mypy stubgen _csv.pyi:

from typing import Any, ClassVar

QUOTE_ALL: int
QUOTE_MINIMAL: int
QUOTE_NONE: int
QUOTE_NONNUMERIC: int
_dialects: dict

class Dialect:
    delimiter: ClassVar[getset_descriptor] = ...
    doublequote: ClassVar[member_descriptor] = ...
    escapechar: ClassVar[getset_descriptor] = ...
    lineterminator: ClassVar[getset_descriptor] = ...
    quotechar: ClassVar[getset_descriptor] = ...
    quoting: ClassVar[getset_descriptor] = ...
    skipinitialspace: ClassVar[member_descriptor] = ...
    strict: ClassVar[member_descriptor] = ...
    @classmethod
    def __init__(cls, *args, **kwargs) -> None: ...

class Error(Exception): ...

def field_size_limit(*args, **kwargs) -> Any: ...
def get_dialect(name) -> Any: ...
def list_dialects() -> Any: ...
def reader(*args, **kwargs) -> Any: ...
def register_dialect(*args, **kwargs) -> Any: ...
def unregister_dialect(name) -> Any: ...
def writer(*args, **kwargs) -> Any: ...

如您所见,visual studio 与 mypy stubgen 相比,代码具有更准确的类型提示...

为什么会这样?哪个工具 visual studio 代码(准确地说是 python 扩展名)用于生成这些 .pyi 文件?

_cvs.pyi 是随 mypy 安装的 typeshed 的一部分。你可以在这里看到它:https://github.com/python/typeshed/blob/master/stdlib/_csv.pyi