Skip to content

Commit

Permalink
Merge pull request pytest-dev#9152 from bluetech/mark-optimize
Browse files Browse the repository at this point in the history
mark/structures: slightly optimize some functions
  • Loading branch information
bluetech committed Oct 2, 2021
2 parents 2cfce8d + 637e8ef commit 0fbfd1c
Showing 1 changed file with 7 additions and 8 deletions.
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

0 comments on commit 0fbfd1c

Please sign in to comment.