Add stubs for pygit2 - #11374
Conversation
This comment has been minimized.
This comment has been minimized.
9e7fd82 to
7f3e7da
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
351dd7e to
37f496a
Compare
This comment has been minimized.
This comment has been minimized.
76491de to
87d8532
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
JelleZijlstra
left a comment
There was a problem hiding this comment.
Thank you. I looked through the code up to config.pyi, lightly comparing the stubs against the implementation. Mostly it looks good, but I see quite a few incorrect uses of AnyStr_co.
|
|
||
| from . import enums | ||
| from ._build import __version__ as __version__ | ||
| from ._pygit2 import ( |
There was a problem hiding this comment.
The runtime just does from ._pygit2 import *, why not do that here?
There was a problem hiding this comment.
Originally I kept the star imports, but then mypy started to complain about wrong types of settings (it seems unable to distinguish the module settings and the global variable settings for example), and stubtest also wasn't happy with all those re-exported names either, so I had to spell out the names to basically help the checkers properly resolve the names.
| get_credentials as get_credentials, | ||
| ) | ||
| from .config import Config as Config | ||
| from .credentials import ( |
There was a problem hiding this comment.
This one is also import *ed.
| from .errors import Passthrough as Passthrough | ||
| from .filter import Filter as Filter | ||
| from .index import Index as Index, IndexEntry as IndexEntry | ||
| from .legacyenums import ( |
| LIBGIT2_VER: tuple[int, int, int] | ||
|
|
||
| def init_repository( | ||
| path: str | bytes | PathLike[AnyStr_co] | None, |
There was a problem hiding this comment.
A TypeVar doesn't make sense here since there is only one occurrence in the function. In any case, covariance is meaningless in a function.
You can instead use the StrOrBytesPath from _typeshed.
There was a problem hiding this comment.
Thanks for the suggestion; I only recently returned to Python development so I'm still getting familiar with the new typing scheme. I'll push an updated version later.
|
|
||
| __version__: str | ||
|
|
||
| def get_libgit2_paths() -> tuple[Path, dict[str, Any]]: ... |
There was a problem hiding this comment.
| def get_libgit2_paths() -> tuple[Path, dict[str, Any]]: ... | |
| def get_libgit2_paths() -> tuple[Path, dict[str, list[str]]]: ... |
| from .utils import _IntoStrArray | ||
|
|
||
| class Payload: | ||
| def __init__(self, **kw: dict[str, object]) -> None: ... |
There was a problem hiding this comment.
| def __init__(self, **kw: dict[str, object]) -> None: ... | |
| def __init__(self, **kw: object) -> None: ... |
Note that the type for **kwargs is the type of individual kwargs, not of the whole kwargs dict.
| directory: PathLike[AnyStr_co] | bytes | str | None = None, | ||
| paths: _IntoStrArray[AnyStr_co] = None, |
There was a problem hiding this comment.
It doesn't look like this function really needs to be generic. These types imply that directory and paths must be of the same type, which doesn't seem to be the case.
| callbacks: StashApplyCallbacks | None = None, | ||
| reinstate_index: bool = False, | ||
| strategy: CheckoutStrategy | None = None, | ||
| directory: PathLike[AnyStr_co] | bytes | str | None = None, |
|
|
||
| from _cffi_backend import _CDataBase | ||
|
|
||
| def str_to_bytes(value: object, name: str) -> bytes: ... |
There was a problem hiding this comment.
| def str_to_bytes(value: object, name: str) -> bytes: ... | |
| def str_to_bytes(value: str, name: object) -> bytes: ... |
It throws a TypeError if it's not a str
There was a problem hiding this comment.
That must be a thinko. Fixed in the next push.
| def __del__(self) -> None: ... | ||
| def __contains__(self, key: str) -> bool: ... | ||
| def __getitem__(self, key: str) -> str: ... | ||
| def __setitem__(self, key: str, value: bool | int | _CDataBase | PathLike[AnyStr_co] | bytes | str | None) -> None: ... |
There was a problem hiding this comment.
Again, don't think AnyStr_co makes sense here.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
|
I've just removed all |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
It seems mypy cannot handle the re-import of |
This is the same as python/typeshed#11374, and allows proper type-checking before it is accepted upstream.
The upstream library is very tricky to type (likely requires nontrivial refactoring), and only contains partial type information, but stubs are a lot easier because only the public signatures are involved this way, so I plan to first make the library usable in typed projects by making stubs available here, then gradually work my way upstream. The stubs are auto-generated then completed with fully manual inspection of every Python source file. The `_pygit2.pyi` comes from upstream and is mostly untouched except for required style changes, the signature of `options()`, and `FilterSource` which is missing from upstream.
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
|
It seems simply ignoring the type check error on the |
This is the same as python/typeshed#11374, and allows proper type-checking before it is accepted upstream.
The upstream library is very tricky to type (likely requires nontrivial refactoring), and only contains partial type information, but stubs are a lot easier because only the public signatures are involved this way, so I plan to first make the library usable in typed projects by making stubs available here, then gradually work my way upstream.
The stubs are auto-generated then completed with fully manual inspection of every Python source file. The
_pygit2.pyicomes from upstream and is mostly untouched except for required style changes, the signature ofoptions(), andFilterSourcewhich is missing from upstream.