some small quality of life changes (#8454)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
This commit is contained in:
parent
90b70a7efe
commit
1d848314d0
|
@ -200,17 +200,12 @@ def deselect_by_keyword(items: "List[Item]", config: Config) -> None:
|
||||||
selectuntil = True
|
selectuntil = True
|
||||||
keywordexpr = keywordexpr[:-1]
|
keywordexpr = keywordexpr[:-1]
|
||||||
|
|
||||||
try:
|
expr = _parse_expression(keywordexpr, "Wrong expression passed to '-k'")
|
||||||
expression = Expression.compile(keywordexpr)
|
|
||||||
except ParseError as e:
|
|
||||||
raise UsageError(
|
|
||||||
f"Wrong expression passed to '-k': {keywordexpr}: {e}"
|
|
||||||
) from None
|
|
||||||
|
|
||||||
remaining = []
|
remaining = []
|
||||||
deselected = []
|
deselected = []
|
||||||
for colitem in items:
|
for colitem in items:
|
||||||
if keywordexpr and not expression.evaluate(KeywordMatcher.from_item(colitem)):
|
if keywordexpr and not expr.evaluate(KeywordMatcher.from_item(colitem)):
|
||||||
deselected.append(colitem)
|
deselected.append(colitem)
|
||||||
else:
|
else:
|
||||||
if selectuntil:
|
if selectuntil:
|
||||||
|
@ -245,24 +240,26 @@ def deselect_by_mark(items: "List[Item]", config: Config) -> None:
|
||||||
if not matchexpr:
|
if not matchexpr:
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
expr = _parse_expression(matchexpr, "Wrong expression passed to '-m'")
|
||||||
expression = Expression.compile(matchexpr)
|
remaining: List[Item] = []
|
||||||
except ParseError as e:
|
deselected: List[Item] = []
|
||||||
raise UsageError(f"Wrong expression passed to '-m': {matchexpr}: {e}") from None
|
|
||||||
|
|
||||||
remaining = []
|
|
||||||
deselected = []
|
|
||||||
for item in items:
|
for item in items:
|
||||||
if expression.evaluate(MarkMatcher.from_item(item)):
|
if expr.evaluate(MarkMatcher.from_item(item)):
|
||||||
remaining.append(item)
|
remaining.append(item)
|
||||||
else:
|
else:
|
||||||
deselected.append(item)
|
deselected.append(item)
|
||||||
|
|
||||||
if deselected:
|
if deselected:
|
||||||
config.hook.pytest_deselected(items=deselected)
|
config.hook.pytest_deselected(items=deselected)
|
||||||
items[:] = remaining
|
items[:] = remaining
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_expression(expr: str, exc_message: str) -> Expression:
|
||||||
|
try:
|
||||||
|
return Expression.compile(expr)
|
||||||
|
except ParseError as e:
|
||||||
|
raise UsageError(f"{exc_message}: {expr}: {e}") from None
|
||||||
|
|
||||||
|
|
||||||
def pytest_collection_modifyitems(items: "List[Item]", config: Config) -> None:
|
def pytest_collection_modifyitems(items: "List[Item]", config: Config) -> None:
|
||||||
deselect_by_keyword(items, config)
|
deselect_by_keyword(items, config)
|
||||||
deselect_by_mark(items, config)
|
deselect_by_mark(items, config)
|
||||||
|
|
|
@ -40,10 +40,7 @@ EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
|
||||||
|
|
||||||
|
|
||||||
def istestfunc(func) -> bool:
|
def istestfunc(func) -> bool:
|
||||||
return (
|
return callable(func) and getattr(func, "__name__", "<lambda>") != "<lambda>"
|
||||||
hasattr(func, "__call__")
|
|
||||||
and getattr(func, "__name__", "<lambda>") != "<lambda>"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_empty_parameterset_mark(
|
def get_empty_parameterset_mark(
|
||||||
|
@ -332,9 +329,6 @@ class MarkDecorator:
|
||||||
""":meta private:"""
|
""":meta private:"""
|
||||||
return self.name # for backward-compat (2.4.1 had this attr)
|
return self.name # for backward-compat (2.4.1 had this attr)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
|
||||||
return f"<MarkDecorator {self.mark!r}>"
|
|
||||||
|
|
||||||
def with_args(self, *args: object, **kwargs: object) -> "MarkDecorator":
|
def with_args(self, *args: object, **kwargs: object) -> "MarkDecorator":
|
||||||
"""Return a MarkDecorator with extra arguments added.
|
"""Return a MarkDecorator with extra arguments added.
|
||||||
|
|
||||||
|
@ -375,18 +369,21 @@ def get_unpacked_marks(obj) -> List[Mark]:
|
||||||
|
|
||||||
|
|
||||||
def normalize_mark_list(mark_list: Iterable[Union[Mark, MarkDecorator]]) -> List[Mark]:
|
def normalize_mark_list(mark_list: Iterable[Union[Mark, MarkDecorator]]) -> List[Mark]:
|
||||||
"""Normalize marker decorating helpers to mark objects.
|
|
||||||
|
|
||||||
:type List[Union[Mark, Markdecorator]] mark_list:
|
|
||||||
:rtype: List[Mark]
|
|
||||||
"""
|
"""
|
||||||
extracted = [
|
Normalize an iterable of Mark or MarkDecorator objects into a list of marks
|
||||||
getattr(mark, "mark", mark) for mark in mark_list
|
by retrieving the `mark` attribute on MarkDecorator instances.
|
||||||
] # unpack MarkDecorator
|
|
||||||
for mark in extracted:
|
:param mark_list: marks to normalize
|
||||||
if not isinstance(mark, Mark):
|
:returns: A new list of the extracted Mark objects
|
||||||
raise TypeError(f"got {mark!r} instead of Mark")
|
"""
|
||||||
return [x for x in extracted if isinstance(x, Mark)]
|
|
||||||
|
def parse_mark(mark: Union[Mark, MarkDecorator]) -> Mark:
|
||||||
|
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]
|
||||||
|
|
||||||
|
|
||||||
def store_mark(obj, mark: Mark) -> None:
|
def store_mark(obj, mark: Mark) -> None:
|
||||||
|
|
Loading…
Reference in New Issue