Skip to content
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

Stop using more-itertools #7587

Merged
merged 1 commit into from
Jul 31, 2020
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
1 change: 1 addition & 0 deletions changelog/7587.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The dependency on the ``more-itertools`` package has been removed.
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ packages =
install_requires =
attrs>=17.4.0
iniconfig
more-itertools>=4.0.0
packaging
pluggy>=0.12,<1.0
py>=1.8.2
Expand Down
7 changes: 3 additions & 4 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import functools
import inspect
import itertools
import sys
import warnings
from collections import defaultdict
Expand Down Expand Up @@ -1489,10 +1488,10 @@ def getfixtureinfo(
else:
argnames = ()

usefixtures = itertools.chain.from_iterable(
mark.args for mark in node.iter_markers(name="usefixtures")
usefixtures = tuple(
arg for mark in node.iter_markers(name="usefixtures") for arg in mark.args
)
initialnames = tuple(usefixtures) + argnames
initialnames = usefixtures + argnames
fm = node.session._fixturemanager
initialnames, names_closure, arg2fixturedefs = fm.getfixtureclosure(
initialnames, node, ignore_args=self._get_direct_parametrize_args(node)
Expand Down
22 changes: 10 additions & 12 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import inspect
import math
import pprint
from collections.abc import Iterable
from collections.abc import Mapping
from collections.abc import Sized
from decimal import Decimal
from itertools import filterfalse
from numbers import Number
from types import TracebackType
from typing import Any
Expand All @@ -18,8 +16,6 @@
from typing import TypeVar
from typing import Union

from more_itertools.more import always_iterable

import _pytest._code
from _pytest.compat import overload
from _pytest.compat import STRING_TYPES
Expand All @@ -30,9 +26,6 @@
from typing import Type


BASE_TYPE = (type, STRING_TYPES)


def _non_numeric_type_error(value, at: Optional[str]) -> TypeError:
at_str = " at {}".format(at) if at else ""
return TypeError(
Expand Down Expand Up @@ -680,11 +673,16 @@ def raises( # noqa: F811
documentation for :ref:`the try statement <python:try>`.
"""
__tracebackhide__ = True
for exc in filterfalse(
inspect.isclass, always_iterable(expected_exception, BASE_TYPE)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks.

):
msg = "exceptions must be derived from BaseException, not %s"
raise TypeError(msg % type(exc))

if isinstance(expected_exception, type):
excepted_exceptions = (expected_exception,) # type: Tuple[Type[_E], ...]
else:
excepted_exceptions = expected_exception
for exc in excepted_exceptions:
if not isinstance(exc, type) or not issubclass(exc, BaseException):
msg = "expected exception must be a BaseException type, not {}"
not_a = exc.__name__ if isinstance(exc, type) else type(exc).__name__
raise TypeError(msg.format(not_a))

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

Expand Down
12 changes: 7 additions & 5 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import attr
import pluggy
import py
from more_itertools import collapse

import pytest
from _pytest import nodes
Expand Down Expand Up @@ -715,11 +714,14 @@ def pytest_sessionstart(self, session: "Session") -> None:
self._write_report_lines_from_hooks(lines)

def _write_report_lines_from_hooks(
self, lines: List[Union[str, List[str]]]
self, lines: Sequence[Union[str, Sequence[str]]]
) -> None:
lines.reverse()
for line in collapse(lines):
self.write_line(line)
for line_or_lines in reversed(lines):
if isinstance(line_or_lines, str):
self.write_line(line_or_lines)
else:
for line in line_or_lines:
self.write_line(line)

def pytest_report_header(self, config: Config) -> List[str]:
line = "rootdir: %s" % config.rootdir
Expand Down
19 changes: 19 additions & 0 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,22 @@ def test_raises_context_manager_with_kwargs(self):
with pytest.raises(Exception, foo="bar"): # type: ignore[call-overload]
pass
assert "Unexpected keyword arguments" in str(excinfo.value)

def test_expected_exception_is_not_a_baseexception(self) -> None:
with pytest.raises(TypeError) as excinfo:
with pytest.raises("hello"): # type: ignore[call-overload]
pass # pragma: no cover
assert "must be a BaseException type, not str" in str(excinfo.value)

class NotAnException:
pass

with pytest.raises(TypeError) as excinfo:
with pytest.raises(NotAnException): # type: ignore[type-var]
pass # pragma: no cover
assert "must be a BaseException type, not NotAnException" in str(excinfo.value)

with pytest.raises(TypeError) as excinfo:
with pytest.raises(("hello", NotAnException)): # type: ignore[arg-type]
pass # pragma: no cover
assert "must be a BaseException type, not str" in str(excinfo.value)