Skip to content
Merged
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
72 changes: 72 additions & 0 deletions stubs/behave/behave/model.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from _typeshed import Incomplete
from collections import OrderedDict
from collections.abc import Iterable, Iterator, Mapping, Sequence
from typing import ClassVar, TypeAlias, TypeVar, overload
from typing_extensions import Never, Self

from behave.model_core import Replayable

_T = TypeVar("_T")

# A table row, either already parsed or as a plain list of cell values.
_RowData: TypeAlias = Row | list[str]

class Table(Replayable):
type: ClassVar[str]
headings: list[str]
rows: list[Row]
line: int
modified: bool
def __init__(self, headings: list[str], rows: Iterable[_RowData] | None = None, line: int | None = None) -> None: ...
def clear(self, headings: list[str] | None = None, keep_headings: bool = True) -> None: ...
def add_row(self, row: _RowData, line: int | None = None) -> None: ...
def add_column(self, column_name: str, values: Iterable[str] | None = None, default_value: str = "") -> int: ...
def remove_column(self, column_name_or_index: str | int) -> None: ...
def remove_columns(self, column_names: Iterable[str]) -> None: ...
def has_column(self, column_name: str) -> bool: ...
def get_column_index(self, column_name: str) -> int: ...
def require_column(self, column_name: str) -> int: ...
def require_columns(self, column_names: Iterable[str]) -> None: ...
def ensure_column_exists(self, column_name: str) -> int: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
def __iter__(self) -> Iterator[Row]: ...

@overload
def __getitem__(self, index: int) -> Row: ...
@overload
def __getitem__(self, index: slice) -> list[Row]: ...

# Always raises: the comparison is asserted and then `NotImplementedError` is raised.
def assert_equals(self, data: object) -> Never: ...
@classmethod
def from_dicts(cls, headings: list[str], table_data: Sequence[dict[str, str]], line: int | None = None) -> Self: ...
@classmethod
def from_data(
cls, headings: list[str], table_data: Sequence[dict[str, str] | _RowData] | None = None, line: int = 0
) -> Table: ...
Comment on lines +42 to +47

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Mapping too narrow (there is an explicit isinstance check):

Suggested change
@classmethod
def from_dicts(cls, headings: list[str], table_data: Sequence[Mapping[str, str]], line: int | None = None) -> Self: ...
@classmethod
def from_data(
cls, headings: list[str], table_data: Sequence[Mapping[str, str] | _RowData] | None = None, line: int = 0
) -> Table: ...
@classmethod
def from_dicts(cls, headings: list[str], table_data: Sequence[dict[str, str]], line: int | None = None) -> Self: ...
@classmethod
def from_data(
cls, headings: list[str], table_data: Sequence[dict[str, str] | _RowData] | None = None, line: int = 0
) -> Table: ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in a2f1e41 — both now take Sequence[dict[str, str]] / Sequence[dict[str, str] | _RowData].

from_dicts raises TypeError("%r (expected: List[dict]") when table_data[0] is not a dict, and from_data takes its else-branch and passes the data straight to Table(headings, rows=table_data), so a non-dict Mapping is rejected by one and silently mishandled by the other.

Left Row.from_dict on Mapping: it only does row_data.get(name), no isinstance check.


class Row:
headings: list[str]
cells: list[str]
line: int | None
comments: Incomplete | None
def __init__(self, headings: list[str], cells: list[str], line: int | None = None, comments=None) -> None: ...
def __getitem__(self, name: str | int) -> str: ...
def __contains__(self, item: str) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[str]: ...
def items(self) -> zip[tuple[str, str]]: ...

@overload
def get(self, key: str | int, default: None = None) -> str | None: ...
@overload
def get(self, key: str | int, default: _T) -> str | _T: ...

def as_dict(self) -> OrderedDict[str, str]: ...
@classmethod
def from_dict(cls, headings: list[str], row_data: Mapping[str, str], *, line: int | None = None, comments=None) -> Self: ...

def __getattr__(name: str) -> Incomplete: ...
8 changes: 8 additions & 0 deletions stubs/behave/behave/model_core.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from _typeshed import Incomplete
from typing import ClassVar

class Replayable:
type: ClassVar[str | None]
def replay(self, formatter) -> None: ...

def __getattr__(name: str) -> Incomplete: ...
6 changes: 4 additions & 2 deletions stubs/behave/behave/runner.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ from collections.abc import Callable
from contextlib import AbstractContextManager
from typing import ClassVar, ParamSpec

from behave.model import Row, Table

_P = ParamSpec("_P")

class Context:
Expand All @@ -14,10 +16,10 @@ class Context:
tags: set[str]
aborted: bool
failed: bool
table: Incomplete | None
table: Table | None
text: str | None
config: Incomplete
active_outline: Incomplete
active_outline: Row | None
fail_on_cleanup_errors: bool

def __init__(self, runner) -> None: ...
Expand Down
Loading