Skip to content

Commit a1ba8df

Browse files
authored
Merge pull request pytest-dev#7587 from bluetech/rm-more-itertools
Stop using more-itertools
2 parents 645cbc9 + 96a48f0 commit a1ba8df

File tree

6 files changed

+40
-22
lines changed

6 files changed

+40
-22
lines changed

changelog/7587.trivial.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The dependency on the ``more-itertools`` package has been removed.

setup.cfg

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ packages =
4242
install_requires =
4343
attrs>=17.4.0
4444
iniconfig
45-
more-itertools>=4.0.0
4645
packaging
4746
pluggy>=0.12,<1.0
4847
py>=1.8.2

src/_pytest/fixtures.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import functools
22
import inspect
3-
import itertools
43
import sys
54
import warnings
65
from collections import defaultdict
@@ -1489,10 +1488,10 @@ def getfixtureinfo(
14891488
else:
14901489
argnames = ()
14911490

1492-
usefixtures = itertools.chain.from_iterable(
1493-
mark.args for mark in node.iter_markers(name="usefixtures")
1491+
usefixtures = tuple(
1492+
arg for mark in node.iter_markers(name="usefixtures") for arg in mark.args
14941493
)
1495-
initialnames = tuple(usefixtures) + argnames
1494+
initialnames = usefixtures + argnames
14961495
fm = node.session._fixturemanager
14971496
initialnames, names_closure, arg2fixturedefs = fm.getfixtureclosure(
14981497
initialnames, node, ignore_args=self._get_direct_parametrize_args(node)

src/_pytest/python_api.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import inspect
21
import math
32
import pprint
43
from collections.abc import Iterable
54
from collections.abc import Mapping
65
from collections.abc import Sized
76
from decimal import Decimal
8-
from itertools import filterfalse
97
from numbers import Number
108
from types import TracebackType
119
from typing import Any
@@ -18,8 +16,6 @@
1816
from typing import TypeVar
1917
from typing import Union
2018

21-
from more_itertools.more import always_iterable
22-
2319
import _pytest._code
2420
from _pytest.compat import overload
2521
from _pytest.compat import STRING_TYPES
@@ -30,9 +26,6 @@
3026
from typing import Type
3127

3228

33-
BASE_TYPE = (type, STRING_TYPES)
34-
35-
3629
def _non_numeric_type_error(value, at: Optional[str]) -> TypeError:
3730
at_str = " at {}".format(at) if at else ""
3831
return TypeError(
@@ -680,11 +673,16 @@ def raises( # noqa: F811
680673
documentation for :ref:`the try statement <python:try>`.
681674
"""
682675
__tracebackhide__ = True
683-
for exc in filterfalse(
684-
inspect.isclass, always_iterable(expected_exception, BASE_TYPE)
685-
):
686-
msg = "exceptions must be derived from BaseException, not %s"
687-
raise TypeError(msg % type(exc))
676+
677+
if isinstance(expected_exception, type):
678+
excepted_exceptions = (expected_exception,) # type: Tuple[Type[_E], ...]
679+
else:
680+
excepted_exceptions = expected_exception
681+
for exc in excepted_exceptions:
682+
if not isinstance(exc, type) or not issubclass(exc, BaseException):
683+
msg = "expected exception must be a BaseException type, not {}"
684+
not_a = exc.__name__ if isinstance(exc, type) else type(exc).__name__
685+
raise TypeError(msg.format(not_a))
688686

689687
message = "DID NOT RAISE {}".format(expected_exception)
690688

src/_pytest/terminal.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import attr
2626
import pluggy
2727
import py
28-
from more_itertools import collapse
2928

3029
import pytest
3130
from _pytest import nodes
@@ -715,11 +714,14 @@ def pytest_sessionstart(self, session: "Session") -> None:
715714
self._write_report_lines_from_hooks(lines)
716715

717716
def _write_report_lines_from_hooks(
718-
self, lines: List[Union[str, List[str]]]
717+
self, lines: Sequence[Union[str, Sequence[str]]]
719718
) -> None:
720-
lines.reverse()
721-
for line in collapse(lines):
722-
self.write_line(line)
719+
for line_or_lines in reversed(lines):
720+
if isinstance(line_or_lines, str):
721+
self.write_line(line_or_lines)
722+
else:
723+
for line in line_or_lines:
724+
self.write_line(line)
723725

724726
def pytest_report_header(self, config: Config) -> List[str]:
725727
line = "rootdir: %s" % config.rootdir

testing/python/raises.py

+19
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,22 @@ def test_raises_context_manager_with_kwargs(self):
283283
with pytest.raises(Exception, foo="bar"): # type: ignore[call-overload]
284284
pass
285285
assert "Unexpected keyword arguments" in str(excinfo.value)
286+
287+
def test_expected_exception_is_not_a_baseexception(self) -> None:
288+
with pytest.raises(TypeError) as excinfo:
289+
with pytest.raises("hello"): # type: ignore[call-overload]
290+
pass # pragma: no cover
291+
assert "must be a BaseException type, not str" in str(excinfo.value)
292+
293+
class NotAnException:
294+
pass
295+
296+
with pytest.raises(TypeError) as excinfo:
297+
with pytest.raises(NotAnException): # type: ignore[type-var]
298+
pass # pragma: no cover
299+
assert "must be a BaseException type, not NotAnException" in str(excinfo.value)
300+
301+
with pytest.raises(TypeError) as excinfo:
302+
with pytest.raises(("hello", NotAnException)): # type: ignore[arg-type]
303+
pass # pragma: no cover
304+
assert "must be a BaseException type, not str" in str(excinfo.value)

0 commit comments

Comments
 (0)