Skip to content

mark/structures: slightly optimize some functions #9152

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

Merged
merged 1 commit into from
Oct 2, 2021
Merged
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
15 changes: 7 additions & 8 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,30 +360,29 @@ def __call__(self, *args: object, **kwargs: object):
return self.with_args(*args, **kwargs)


def get_unpacked_marks(obj) -> List[Mark]:
def get_unpacked_marks(obj: object) -> Iterable[Mark]:
"""Obtain the unpacked marks that are stored on an object."""
mark_list = getattr(obj, "pytestmark", [])
if not isinstance(mark_list, list):
mark_list = [mark_list]
return normalize_mark_list(mark_list)


def normalize_mark_list(mark_list: Iterable[Union[Mark, MarkDecorator]]) -> List[Mark]:
def normalize_mark_list(
mark_list: Iterable[Union[Mark, MarkDecorator]]
) -> Iterable[Mark]:
"""
Normalize an iterable of Mark or MarkDecorator objects into a list of marks
by retrieving the `mark` attribute on MarkDecorator instances.

:param mark_list: marks to normalize
:returns: A new list of the extracted Mark objects
"""

def parse_mark(mark: Union[Mark, MarkDecorator]) -> Mark:
for mark in mark_list:
mark_obj = getattr(mark, "mark", mark)
if not isinstance(mark_obj, Mark):
raise TypeError(f"got {repr(mark_obj)} instead of Mark")
return mark_obj

return [parse_mark(x) for x in mark_list]
yield mark_obj


def store_mark(obj, mark: Mark) -> None:
Expand All @@ -394,7 +393,7 @@ def store_mark(obj, mark: Mark) -> None:
assert isinstance(mark, Mark), mark
# Always reassign name to avoid updating pytestmark in a reference that
# was only borrowed.
obj.pytestmark = get_unpacked_marks(obj) + [mark]
obj.pytestmark = [*get_unpacked_marks(obj), mark]


# Typing for builtin pytest marks. This is cheating; it gives builtin marks
Expand Down