Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding stubs for ruamel.yaml #12584

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyrightconfig.stricter.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"stubs/reportlab",
"stubs/requests",
"stubs/requests-oauthlib",
"stubs/ruamel.yaml",
"stubs/seaborn",
"stubs/setuptools/setuptools",
"stubs/shapely",
Expand Down
6 changes: 6 additions & 0 deletions stubs/ruamel.yaml/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version = "0.18.*"
upstream_repository = "https://sourceforge.net/p/ruamel-yaml/code/"

[tool.stubtest]
skip = true # mypy parse errors in upstream package
stubtest_requirements = ["configobj"]
55 changes: 55 additions & 0 deletions stubs/ruamel.yaml/_ruamel_yaml.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from _typeshed import SupportsRead, SupportsWrite
from collections.abc import Mapping, Sequence

from ruamel.yaml.events import Event
from ruamel.yaml.nodes import Node
from ruamel.yaml.tokens import Token

def get_version_string() -> str: ...
def get_version() -> tuple[int, int, int]: ...

class Mark:
name: str | None
index: int
line: int
column: int
buffer: None
pointer: None
def __init__(self, name: str | None, index: int, line: int, column: int, buffer: None, pointer: None) -> None: ...
def get_snippet(self) -> None: ...

class CParser:
def __init__(self, stream: str | bytes | SupportsRead[str | bytes]) -> None: ...
def dispose(self) -> None: ...
def get_token(self) -> Token | None: ...
def peek_token(self) -> Token | None: ...
def check_token(self, *choices: type[Token]) -> bool: ...
def get_event(self) -> Event | None: ...
def peek_event(self) -> Event | None: ...
def check_event(self, *choices: type[Token]) -> bool: ...
def check_node(self) -> bool: ...
def get_node(self) -> Node | None: ...
def get_single_node(self) -> Node | None: ...
def raw_parse(self) -> int: ...
def raw_scan(self) -> int: ...

class CEmitter:
def __init__(
self,
stream: SupportsWrite[str | bytes],
canonical: bool | None = ...,
indent: int | None = ...,
width: int | None = ...,
allow_unicode: bool | None = ...,
line_break: str | None = ...,
encoding: str | None = ...,
explicit_start: bool | None = ...,
explicit_end: bool | None = ...,
version: Sequence[int] | None = ...,
tags: Mapping[str, str] | None = ...,
) -> None: ...
def dispose(self) -> None: ...
def emit(self, event_object: Event) -> None: ...
def open(self) -> None: ...
def close(self) -> None: ...
def serialize(self, node: Node) -> None: ...
16 changes: 16 additions & 0 deletions stubs/ruamel.yaml/ruamel/yaml/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .comments import CommentedMap as CommentedMap, CommentedSeq as CommentedSeq
from .constructor import *
from .cyaml import *
from .dumper import *
from .error import YAMLError as YAMLError
from .events import *
from .loader import *
from .main import *
from .nodes import *
from .representer import *
from .resolver import *
from .tokens import *

version_info: tuple[int, int, int]
__version__: str
__with_libyaml__: bool
Comment on lines +14 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The should most likely be final:

Suggested change
version_info: tuple[int, int, int]
__version__: str
__with_libyaml__: bool
version_info: Final[tuple[int, int, int]]
__version__: Final[str]
__with_libyaml__: Final[bool]

(Needs import from typing.)

9 changes: 9 additions & 0 deletions stubs/ruamel.yaml/ruamel/yaml/anchor.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Final

anchor_attrib: Final = "_yaml_anchor"

class Anchor:
attrib: Final = anchor_attrib
value: str | None
always_dump: bool
def __init__(self) -> None: ...
184 changes: 184 additions & 0 deletions stubs/ruamel.yaml/ruamel/yaml/comments.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import sys
from _typeshed import SupportsWrite, sentinel
from collections.abc import Hashable, Iterable, Iterator, Mapping, MutableSet, Set as AbstractSet, Sized
from typing import Any, Final, Generic, NoReturn, TypeVar
from typing_extensions import Self, TypeAlias

from .anchor import Anchor
from .compat import MutableSliceableSequence, ordereddict
from .nodes import _ScalarNodeStyle
from .tag import Tag
from .tokens import CommentToken, _CommentGroup

__all__ = [
"CommentedSeq",
"CommentedKeySeq",
"CommentedMap",
"CommentedOrderedMap",
"CommentedSet",
"comment_attrib",
"merge_attrib",
"TaggedScalar",
"C_POST",
"C_PRE",
"C_SPLIT_ON_FIRST_BLANK",
"C_BLANK_LINE_PRESERVE_SPACE",
]

_Index: TypeAlias = int
_Key: TypeAlias = Hashable

_T = TypeVar("_T")
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
_KT_co = TypeVar("_KT_co", covariant=True)
_VT_co = TypeVar("_VT_co", covariant=True)
_Commented = TypeVar("_Commented", bound=CommentedBase)

C_POST: Final = 0b00
C_PRE: Final = 0b01
C_SPLIT_ON_FIRST_BLANK: Final = 0b10
C_BLANK_LINE_PRESERVE_SPACE: Final = 0b100

comment_attrib: Final = "_yaml_comment"
format_attrib: Final = "_yaml_format"
line_col_attrib: Final = "_yaml_line_col"
merge_attrib: Final = "_yaml_merge"

class Comment:
attrib: Final = comment_attrib
comment: _CommentGroup | None
def __init__(self, *, old: bool = True) -> None: ...
@property
def items(self) -> dict[_Index | _Key, _CommentGroup]: ...
@property
def end(self): ... # RTSC
@end.setter
def end(self, value) -> None: ... # RTSC
@property
def pre(self): ... # RTSC
@pre.setter
def pre(self, value) -> None: ... # RTSC
def get(self, item: _Index | _Key, pos: int): ... # RTSC
def set(self, item: _Index | _Key, pos: int, value: list[int]): ... # RTSC
def __contains__(self, x: str, /) -> bool: ...

class Format:
attrib: Final = format_attrib
def __init__(self) -> None: ...
def set_flow_style(self) -> None: ...
def set_block_style(self) -> None: ...
def flow_style(self, default: bool | None = None) -> bool | None: ...

class LineCol:
attrib: Final = line_col_attrib
line: int | None
col: int | None
data: dict[_Key, list[int]] | None
def __init__(self) -> None: ...
def add_kv_line_col(self, key: _Key, data: list[int]) -> None: ...
def key(self, k: str, /) -> tuple[int, int] | None: ...
def value(self, k: str, /) -> tuple[int, int] | None: ...
def item(self, idx: int, /) -> tuple[int, int] | None: ...
def add_idx_line_col(self, key: _Index, data: list[int]) -> None: ...

class CommentedBase:
@property
def ca(self) -> Comment: ...
def yaml_end_comment_extend(self, comment: list[CommentToken] | None, *, clear: bool = False) -> None: ...
def yaml_key_comment_extend(self, key: _Index | _Key, comment: _CommentGroup, *, clear: bool = False) -> None: ...
def yaml_value_comment_extend(self, key: _Key, comment: _CommentGroup, *, clear: bool = False) -> None: ...
def yaml_set_start_comment(self, comment: str, *, indent: int = 0) -> None: ...
def yaml_set_comment_before_after_key(
self, key: _Key, before: str | None = None, indent: int = 0, after: str | None = None, after_indent: int | None = None
) -> None: ...
@property
def fa(self) -> Format: ...
def yaml_add_eol_comment(self, comment: str, key: _Index | _Key = sentinel, column: int | None = None) -> None: ...
@property
def lc(self) -> LineCol: ...
@property
def anchor(self) -> Anchor: ...
def yaml_anchor(self) -> Anchor | None: ...
def yaml_set_anchor(self, value: str, *, always_dump: bool = False) -> None: ...
@property
def tag(self) -> Tag: ...
def yaml_set_ctag(self, value: Tag, /) -> None: ...
def copy_attributes(self, t: _Commented, memo: dict[int, Any] | None = None) -> _Commented: ...

class CommentedSeq(MutableSliceableSequence[_T], list[_T], CommentedBase): # type: ignore[misc]
def __getsingleitem__(self, idx: int) -> _T: ...
def __setsingleitem__(self, idx: int, value: _T) -> None: ...
def __delsingleitem__(self, idx: int) -> None: ...
def __deepcopy__(self, memo: dict[int, Any]) -> Self: ...

class CommentedKeySeq(tuple[_T, ...], CommentedBase): ...

class CommentedMapView(Sized):
def __init__(self, mapping: CommentedMap[Any, Any]) -> None: ...
def __len__(self) -> int: ...

class CommentedMapKeysView(CommentedMapView, AbstractSet[_KT_co]):
def __init__(self, mapping: CommentedMap[_KT_co, Any]) -> None: ...
def __contains__(self, key: object) -> bool: ...
def __iter__(self) -> Iterator[_KT_co]: ...

class CommentedMapItemsView(CommentedMapView, AbstractSet[tuple[_KT_co, _VT_co]]):
def __init__(self, mapping: CommentedMap[_KT_co, _VT_co]) -> None: ...
def __contains__(self, item: object) -> bool: ...
def __iter__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ...

class CommentedMapValuesView(CommentedMapView, Generic[_VT_co]):
def __init__(self, mapping: CommentedMap[Any, _VT_co]) -> None: ...
def __contains__(self, value: object) -> bool: ...
def __iter__(self) -> Iterator[_VT_co]: ...

class CommentedMap(ordereddict[_KT, _VT], CommentedBase):
def insert(self, pos: int, key: _KT, value: _VT, comment: str | None = None) -> None: ...
def mlget(self, key: list[_Index | _Key], default: Any = None, list_ok: bool = False) -> Any: ...
def non_merged_items(self) -> Iterator[tuple[_KT, _VT]]: ...
def keys(self) -> CommentedMapKeysView[_KT]: ... # type: ignore[override]
def values(self) -> CommentedMapValuesView[_VT]: ... # type: ignore[override]
def items(self) -> CommentedMapItemsView[_KT, _VT]: ... # type: ignore[override]
@property
def merge(self) -> list[tuple[int, Self]]: ...
def add_referent(self, cm: Self) -> None: ...
def add_yaml_merge(self, value: list[tuple[int, Self]]) -> None: ...
def update_key_value(self, key: _KT) -> None: ...
def __deepcopy__(self, memo: dict[int, Any]) -> Self: ...

class CommentedKeyMap(CommentedBase, Mapping[_KT, _VT]):
def __init__(self, *args, **kwargs) -> None: ...
def __delitem__(self, *args, **kwargs) -> NoReturn: ...
def __setitem__(self, *args, **kwargs) -> NoReturn: ...
def clear(self, *args, **kwargs) -> NoReturn: ...
def pop(self, *args, **kwargs) -> NoReturn: ...
def popitem(self, *args, **kwargs) -> NoReturn: ...
def setdefault(self, *args, **kwargs) -> NoReturn: ...
def update(self, *args, **kwargs) -> NoReturn: ...
def __getitem__(self, index: _KT) -> _VT: ...
def __iter__(self) -> Iterator[_KT]: ...
def __len__(self) -> int: ...
def __hash__(self) -> int: ...
@classmethod
def fromkeys(keys, v=None) -> NoReturn: ... # broken

class CommentedOrderedMap(CommentedMap[_KT, _VT]): ...

class CommentedSet(MutableSet[_T], CommentedBase):
odict: ordereddict[_T, None]
def __init__(self, values: Iterable[_T] | None = None) -> None: ...
def add(self, value: _T) -> None: ...
def discard(self, value: _T) -> None: ...
def __contains__(self, x: object) -> bool: ...
def __iter__(self) -> Iterator[_T]: ...
def __len__(self) -> int: ...

class TaggedScalar(CommentedBase):
value: str | None
style: _ScalarNodeStyle | None
def __init__(self, value: str | None = None, style: _ScalarNodeStyle | None = None, tag: Tag | str | None = None) -> None: ...
def count(self, s: str, start: int | None = None, end: int | None = None) -> int: ...
def __getitem__(self, pos: int) -> str: ...

def dump_comments(d: object, name: str = "", sep: str = ".", out: SupportsWrite[str] = sys.stdout) -> None: ...
65 changes: 65 additions & 0 deletions stubs/ruamel.yaml/ruamel/yaml/compat.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from _typeshed import SupportsRead, SupportsWrite
from abc import ABCMeta, abstractmethod
from collections import OrderedDict
from collections.abc import Iterable, MutableSequence
from typing import IO, Any, Final, TypeVar, overload
from typing_extensions import Self, TypeAlias

from .docinfo import Version

_T = TypeVar("_T")
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")

_ReadStream: TypeAlias = str | bytes | SupportsRead[str] | SupportsRead[bytes]
_WriteStream: TypeAlias = SupportsWrite[str] | SupportsWrite[bytes]

StreamType: TypeAlias = _WriteStream
StreamTextType: TypeAlias = _ReadStream
VersionType: TypeAlias = str | tuple[int, int] | list[int] | Version | None

class ordereddict(OrderedDict[_KT, _VT]):
def insert(self, pos: int, key: _KT, value: _VT) -> None: ...

builtins_module: Final = "builtins"

def with_metaclass(meta, *bases): ...

DBG_TOKEN: Final = 1
DBG_EVENT: Final = 2
DBG_NODE: Final = 4

def dbg(val: int | None = None) -> int: ...

class Nprint:
def __init__(self, file_name: str | None = None) -> None: ...
def __call__(self, *args, **kw) -> None: ...
def set_max_print(self, i: int) -> None: ...
def fp(self, mode: str = "a") -> IO[Any]: ...

nprint: Nprint
nprintf: Nprint

def check_namespace_char(ch: str) -> bool: ...
def check_anchorname_char(ch: str) -> bool: ...
def version_tnf(t1: tuple[int, ...], t2: tuple[int, ...] | None = None) -> bool | None: ...

class MutableSliceableSequence(MutableSequence[_T], metaclass=ABCMeta):
@overload
def __getitem__(self, index: int) -> _T: ...
@overload
def __getitem__(self, index: slice) -> Self: ...
@overload
def __setitem__(self, index: int, value: _T) -> None: ...
@overload
def __setitem__(self, index: slice, value: Iterable[_T]) -> None: ...
@overload
def __delitem__(self, index: int) -> None: ...
@overload
def __delitem__(self, index: slice) -> None: ...
@abstractmethod
def __getsingleitem__(self, index: int) -> _T: ...
@abstractmethod
def __setsingleitem__(self, index: int, value: _T) -> None: ...
@abstractmethod
def __delsingleitem__(self, index: int) -> None: ...
31 changes: 31 additions & 0 deletions stubs/ruamel.yaml/ruamel/yaml/composer.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from .error import MarkedYAMLError
from .events import MappingEndEvent, SequenceEndEvent
from .loader import _Loader
from .main import YAML
from .nodes import CollectionNode, MappingNode, Node, ScalarNode, SequenceNode
from .parser import Parser
from .resolver import BaseResolver

__all__ = ["Composer", "ComposerError"]

class ComposerError(MarkedYAMLError): ...

class Composer:
loader: YAML | _Loader | None
anchors: dict[str, Node]
warn_double_anchors: bool
def __init__(self, loader: YAML | _Loader | None = None) -> None: ...
@property
def parser(self) -> Parser: ...
@property
def resolver(self) -> BaseResolver: ...
def check_node(self) -> bool: ...
def get_node(self) -> Node: ...
def get_single_node(self) -> Node: ...
def compose_document(self) -> Node: ...
def return_alias(self, a: Node) -> Node: ...
def compose_node(self, parent: CollectionNode | None, index: int | Node | None) -> Node: ...
def compose_scalar_node(self, anchor: str | None) -> ScalarNode: ...
def compose_sequence_node(self, anchor: str | None) -> SequenceNode: ...
def compose_mapping_node(self, anchor: str | None) -> MappingNode: ...
def check_end_doc_comment(self, end_event: SequenceEndEvent | MappingEndEvent, node: SequenceNode | MappingNode) -> None: ...
7 changes: 7 additions & 0 deletions stubs/ruamel.yaml/ruamel/yaml/configobjwalker.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from collections.abc import Iterator
from typing_extensions import deprecated

from configobj import ConfigObj # type: ignore[import-not-found] # pyright: ignore[reportMissingImports]

@deprecated("configobj_walker has moved to ruamel.yaml.util")
def configobj_walker(cfg: ConfigObj, /) -> Iterator[str]: ...
Loading
Loading