pythontype-hintingmypyvscode-pythonpyi

generate visual studio code's like .pyi file


.pyi file in visual studio code (python extension) in my case _csv.pyi file has more details on type hints compared to the .pyi file generated by mypy stubgen

for example, in the code below you can see both .pyi file

visual studio code _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: ...

as you can see visual studio code has a more accurate type hint compared to mypy stubgen...

why is this the case? which tool visual studio code (python extension precisely) used to generate these .pyi files?


Solution

  • _cvs.pyi is part of typeshed installed with mypy. You can see it here: https://github.com/python/typeshed/blob/master/stdlib/_csv.pyi