Skip to content

Commit

Permalink
Add stubs for unidiff (#11860)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored May 4, 2024
1 parent a3f7c2b commit 9c8c9c7
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
7 changes: 7 additions & 0 deletions stubs/unidiff/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Aliases for Python 3 compatibility, not part of the API
unidiff.patch.PY2
unidiff.patch.basestring
unidiff.patch.implements_to_string
unidiff.patch.make_str
unidiff.patch.open_file
unidiff.patch.unicode
2 changes: 2 additions & 0 deletions stubs/unidiff/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = "0.7.*"
upstream_repository = "https://github.com/matiasb/python-unidiff"
12 changes: 12 additions & 0 deletions stubs/unidiff/unidiff/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from unidiff.patch import (
DEFAULT_ENCODING as DEFAULT_ENCODING,
LINE_TYPE_ADDED as LINE_TYPE_ADDED,
LINE_TYPE_CONTEXT as LINE_TYPE_CONTEXT,
LINE_TYPE_REMOVED as LINE_TYPE_REMOVED,
Hunk as Hunk,
PatchedFile as PatchedFile,
PatchSet as PatchSet,
UnidiffParseError as UnidiffParseError,
)

VERSION: str
1 change: 1 addition & 0 deletions stubs/unidiff/unidiff/__version__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__: str
24 changes: 24 additions & 0 deletions stubs/unidiff/unidiff/constants.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from re import Pattern
from typing import Final

RE_SOURCE_FILENAME: Pattern[str]
RE_TARGET_FILENAME: Pattern[str]
RE_DIFF_GIT_HEADER: Pattern[str]
RE_DIFF_GIT_HEADER_URI_LIKE: Pattern[str]
RE_DIFF_GIT_HEADER_NO_PREFIX: Pattern[str]
RE_DIFF_GIT_DELETED_FILE: Pattern[str]
RE_DIFF_GIT_NEW_FILE: Pattern[str]
RE_HUNK_HEADER: Pattern[str]
RE_HUNK_BODY_LINE: Pattern[str]
RE_HUNK_EMPTY_BODY_LINE: Pattern[str]
RE_NO_NEWLINE_MARKER: Pattern[str]
RE_BINARY_DIFF: Pattern[str]

DEFAULT_ENCODING: Final = "UTF-8"
DEV_NULL: Final = "/dev/null"
LINE_TYPE_ADDED: Final = "+"
LINE_TYPE_REMOVED: Final = "-"
LINE_TYPE_CONTEXT: Final = " "
LINE_TYPE_EMPTY: Final = ""
LINE_TYPE_NO_NEWLINE: Final = "\\"
LINE_VALUE_NO_NEWLINE: Final = " No newline at end of file"
1 change: 1 addition & 0 deletions stubs/unidiff/unidiff/errors.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class UnidiffParseError(Exception): ...
115 changes: 115 additions & 0 deletions stubs/unidiff/unidiff/patch.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from _typeshed import StrPath
from collections.abc import Iterable
from typing import overload
from typing_extensions import Self

from unidiff.constants import (
DEFAULT_ENCODING as DEFAULT_ENCODING,
LINE_TYPE_ADDED as LINE_TYPE_ADDED,
LINE_TYPE_CONTEXT as LINE_TYPE_CONTEXT,
LINE_TYPE_REMOVED as LINE_TYPE_REMOVED,
)
from unidiff.errors import UnidiffParseError as UnidiffParseError

class Line:
source_line_no: int | None
target_line_no: int | None
diff_line_no: int | None
line_type: str
value: str
def __init__(
self,
value: str,
line_type: str,
source_line_no: int | None = None,
target_line_no: int | None = None,
diff_line_no: int | None = None,
) -> None: ...
def __eq__(self, other: Line) -> bool: ... # type: ignore[override]
@property
def is_added(self) -> bool: ...
@property
def is_removed(self) -> bool: ...
@property
def is_context(self) -> bool: ...

class PatchInfo(list[str]): ...

class Hunk(list[Line]):
source_start: int
source_length: int
target_start: int
target_length: int
section_header: str
def __init__(
self, src_start: int = 0, src_len: int | None = 0, tgt_start: int = 0, tgt_len: int | None = 0, section_header: str = ""
) -> None: ...
def append(self, line: Line) -> None: ...
@property
def added(self) -> int: ...
@property
def removed(self) -> int: ...
def is_valid(self) -> bool: ...
def source_lines(self) -> Iterable[Line]: ...
@property
def source(self) -> Iterable[str]: ...
def target_lines(self) -> Iterable[Line]: ...
@property
def target(self) -> Iterable[str]: ...

class PatchedFile(list[Hunk]):
patch_info: PatchInfo | None
source_file: str
source_timestamp: str | None
target_file: str
target_timestamp: str | None
is_binary_file: bool
def __init__(
self,
patch_info: PatchInfo | None = None,
source: str = "",
target: str = "",
source_timestamp: str | None = None,
target_timestamp: str | None = None,
is_binary_file: bool = False,
) -> None: ...
@property
def path(self) -> str: ...
@property
def added(self) -> int: ...
@property
def removed(self) -> int: ...
@property
def is_rename(self) -> bool: ...
@property
def is_added_file(self) -> bool: ...
@property
def is_removed_file(self) -> bool: ...
@property
def is_modified_file(self) -> bool: ...

class PatchSet(list[PatchedFile]):
@overload
def __init__(self, f: Iterable[str] | str, encoding: None = None, metadata_only: bool = False) -> None: ...
@overload
def __init__(self, f: Iterable[bytes] | bytes, encoding: str | None = None, metadata_only: bool = False) -> None: ...
@classmethod
def from_filename(
cls, filename: StrPath, encoding: str = "UTF-8", errors: str | None = None, newline: str | None = None
) -> Self: ...
@classmethod
@overload
def from_string(cls, data: str, encoding: None = None, errors: str | None = "strict") -> Self: ...
@classmethod
@overload
def from_string(cls, data: bytes, encoding: str | None = None, errors: str | None = "strict") -> Self: ...
@property
def added_files(self) -> list[PatchedFile]: ...
@property
def removed_files(self) -> list[PatchedFile]: ...
@property
def modified_files(self) -> list[PatchedFile]: ...
@property
def added(self) -> int: ...
@property
def removed(self) -> int: ...

0 comments on commit 9c8c9c7

Please sign in to comment.