diff --git a/_pytest/_code/_py2traceback.py b/_pytest/_code/_py2traceback.py index be73280a7..2dd100c33 100644 --- a/_pytest/_code/_py2traceback.py +++ b/_pytest/_code/_py2traceback.py @@ -4,6 +4,7 @@ # from __future__ import absolute_import, division, print_function import types +from six import text_type def format_exception_only(etype, value): @@ -79,7 +80,7 @@ def _format_final_exc_line(etype, value): def _some_str(value): try: - return unicode(value) + return text_type(value) except Exception: try: return str(value) diff --git a/_pytest/_code/code.py b/_pytest/_code/code.py index 41f232dc8..cb788c17f 100644 --- a/_pytest/_code/code.py +++ b/_pytest/_code/code.py @@ -8,7 +8,7 @@ import attr import re from weakref import ref from _pytest.compat import _PY2, _PY3, PY35, safe_str - +from six import text_type import py builtin_repr = repr @@ -479,7 +479,7 @@ class ExceptionInfo(object): def __unicode__(self): entry = self.traceback[-1] loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly()) - return unicode(loc) + return text_type(loc) def match(self, regexp): """ diff --git a/_pytest/compat.py b/_pytest/compat.py index 9a45a9eb3..e4072ecdb 100644 --- a/_pytest/compat.py +++ b/_pytest/compat.py @@ -13,6 +13,8 @@ import py import _pytest from _pytest.outcomes import TEST_OUTCOME +from six import text_type +import six try: import enum @@ -163,7 +165,7 @@ def get_default_arg_names(function): if _PY3: STRING_TYPES = bytes, str - UNICODE_TYPES = str, + UNICODE_TYPES = six.text_type if PY35: @@ -206,8 +208,8 @@ if _PY3: else: - STRING_TYPES = bytes, str, unicode - UNICODE_TYPES = unicode, + STRING_TYPES = six.string_types + UNICODE_TYPES = six.text_type def ascii_escaped(val): """In py2 bytes and str are the same type, so return if it's a bytes @@ -303,8 +305,8 @@ else: try: return str(v) except UnicodeError: - if not isinstance(v, unicode): - v = unicode(v) + if not isinstance(v, text_type): + v = text_type(v) errors = "replace" return v.encode("utf-8", errors) diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index c2b6ebc32..da84ab429 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -30,6 +30,8 @@ from _pytest.compat import ( ) from _pytest.outcomes import fail, TEST_OUTCOME +FIXTURE_MSG = 'fixtures cannot have "pytest_funcarg__" prefix and be decorated with @pytest.fixture:\n{}' + @attr.s(frozen=True) class PseudoFixtureDef(object): @@ -1178,8 +1180,7 @@ class FixtureManager(object): else: if marker.name: name = marker.name - msg = 'fixtures cannot have "pytest_funcarg__" prefix ' "and be decorated with @pytest.fixture:\n%s" % name - assert not name.startswith(self._argprefix), msg + assert not name.startswith(self._argprefix), FIXTURE_MSG.format(name) fixture_def = FixtureDef( self, diff --git a/_pytest/python.py b/_pytest/python.py index 59126161b..05988877d 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -343,7 +343,7 @@ class PyCollector(PyobjMixin, nodes.Collector): obj = safe_getattr(obj, "__func__", False) if obj is False: # Python 2.6 wraps in a different way that we won't try to handle - msg = "cannot collect static method %r because " "it is not a function (always the case in Python 2.6)" + msg = "cannot collect static method %r because it is not a function" self.warn(code="C2", message=msg % name) return False return ( diff --git a/_pytest/python_api.py b/_pytest/python_api.py index 653d8a357..955fb4165 100644 --- a/_pytest/python_api.py +++ b/_pytest/python_api.py @@ -2,14 +2,19 @@ import math import sys import py -from six import binary_type, text_type from six.moves import zip, filterfalse from more_itertools.more import always_iterable from _pytest.compat import isclass + +from _pytest.compat import Mapping, Sequence +from _pytest.compat import STRING_TYPES + from _pytest.outcomes import fail import _pytest._code +BASE_TYPE = (type, STRING_TYPES) + def _cmp_raises_type_error(self, other): """__cmp__ implementation which raises TypeError. Used @@ -439,8 +444,6 @@ def approx(expected, rel=None, abs=None, nan_ok=False): __ https://docs.python.org/3/reference/datamodel.html#object.__ge__ """ - from _pytest.compat import Mapping, Sequence - from _pytest.compat import STRING_TYPES as String from decimal import Decimal # Delegate the comparison to a class that knows how to deal with the type @@ -461,7 +464,7 @@ def approx(expected, rel=None, abs=None, nan_ok=False): cls = ApproxNumpy elif isinstance(expected, Mapping): cls = ApproxMapping - elif isinstance(expected, Sequence) and not isinstance(expected, String): + elif isinstance(expected, Sequence) and not isinstance(expected, STRING_TYPES): cls = ApproxSequence elif isinstance(expected, Decimal): cls = ApproxDecimal @@ -600,8 +603,7 @@ def raises(expected_exception, *args, **kwargs): """ __tracebackhide__ = True - base_type = (type, text_type, binary_type) - for exc in filterfalse(isclass, always_iterable(expected_exception, base_type)): + for exc in filterfalse(isclass, always_iterable(expected_exception, BASE_TYPE)): msg = ( "exceptions must be old-style classes or" " derived from BaseException, not %s" diff --git a/bench/bench.py b/bench/bench.py index 4ca2a9a98..4e72444e7 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -2,7 +2,7 @@ import sys if __name__ == "__main__": import cProfile - import pytest + import pytest # NOQA import pstats script = sys.argv[1:] if len(sys.argv) > 1 else "empty.py" diff --git a/bench/bench_argcomplete.py b/bench/bench_argcomplete.py index dbb3adbe0..495e2c4ed 100644 --- a/bench/bench_argcomplete.py +++ b/bench/bench_argcomplete.py @@ -5,17 +5,18 @@ # FilesCompleter 75.1109 69.2116 # FastFilesCompleter 0.7383 1.0760 +import timeit + +imports = [ + "from argcomplete.completers import FilesCompleter as completer", + "from _pytest._argcomplete import FastFilesCompleter as completer", +] + +count = 1000 # only a few seconds +setup = "%s\nfc = completer()" +run = 'fc("/d")' + if __name__ == "__main__": - import sys - import timeit - from argcomplete.completers import FilesCompleter - from _pytest._argcomplete import FastFilesCompleter - - count = 1000 # only a few seconds - setup = "from __main__ import FastFilesCompleter\nfc = FastFilesCompleter()" - run = 'fc("/d")' - sys.stdout.write( - "%s\n" % (timeit.timeit(run, setup=setup.replace("Fast", ""), number=count)) - ) - sys.stdout.write("%s\n" % (timeit.timeit(run, setup=setup, number=count))) + print(timeit.timeit(run, setup=setup % imports[0], number=count)) + print((timeit.timeit(run, setup=setup % imports[1], number=count))) diff --git a/bench/skip.py b/bench/skip.py index 891b9be13..b105e79f8 100644 --- a/bench/skip.py +++ b/bench/skip.py @@ -1,11 +1,11 @@ - +from six.moves import range import pytest SKIP = True -@pytest.mark.parametrize("x", xrange(5000)) +@pytest.mark.parametrize("x", range(5000)) def test_foo(x): if SKIP: pytest.skip("heh") diff --git a/doc/en/check_sphinx.py b/doc/en/check_sphinx.py index 58cfd48b3..af609624b 100644 --- a/doc/en/check_sphinx.py +++ b/doc/en/check_sphinx.py @@ -1,4 +1,3 @@ -import py import subprocess diff --git a/doc/en/example/assertion/failure_demo.py b/doc/en/example/assertion/failure_demo.py index f311b4ad7..dc27f5dfb 100644 --- a/doc/en/example/assertion/failure_demo.py +++ b/doc/en/example/assertion/failure_demo.py @@ -153,7 +153,7 @@ def globf(x): class TestRaises(object): def test_raises(self): - s = "qwe" + s = "qwe" # NOQA raises(TypeError, "int(s)") def test_raises_doesnt(self): @@ -163,15 +163,15 @@ class TestRaises(object): raise ValueError("demo error") def test_tupleerror(self): - a, b = [1] + a, b = [1] # NOQA def test_reinterpret_fails_with_print_for_the_fun_of_it(self): - l = [1, 2, 3] - print("l is %r" % l) - a, b = l.pop() + items = [1, 2, 3] + print("items is %r" % items) + a, b = items.pop() def test_some_error(self): - if namenotexi: + if namenotexi: # NOQA pass def func1(self): @@ -205,12 +205,12 @@ class TestMoreErrors(object): somefunc(f(), g()) def test_z1_unpack_error(self): - l = [] - a, b = l + items = [] + a, b = items def test_z2_type_error(self): - l = 3 - a, b = l + items = 3 + a, b = items def test_startswith(self): s = "123" diff --git a/doc/en/example/assertion/global_testmodule_config/conftest.py b/doc/en/example/assertion/global_testmodule_config/conftest.py index e4a25a038..4859bea78 100644 --- a/doc/en/example/assertion/global_testmodule_config/conftest.py +++ b/doc/en/example/assertion/global_testmodule_config/conftest.py @@ -1,4 +1,5 @@ -import pytest, py +import pytest +import py mydir = py.path.local(__file__).dirpath() diff --git a/doc/en/example/pythoncollection.py b/doc/en/example/pythoncollection.py index 7a9226bc6..3603361c3 100644 --- a/doc/en/example/pythoncollection.py +++ b/doc/en/example/pythoncollection.py @@ -1,6 +1,7 @@ - # run this with $ pytest --collect-only test_collectonly.py # + + def test_function(): pass diff --git a/extra/get_issues.py b/extra/get_issues.py index 790b4a2cb..c026972b1 100644 --- a/extra/get_issues.py +++ b/extra/get_issues.py @@ -1,10 +1,9 @@ import json import py +import requests issues_url = "https://api.github.com/repos/pytest-dev/pytest/issues" -import requests - def get_issues(): issues = [] @@ -55,7 +54,7 @@ def _get_kind(issue): def report(issues): for issue in issues: title = issue["title"] - body = issue["body"] + # body = issue["body"] kind = _get_kind(issue) status = issue["state"] number = issue["number"] diff --git a/testing/code/test_code.py b/testing/code/test_code.py index 214267286..bfae36918 100644 --- a/testing/code/test_code.py +++ b/testing/code/test_code.py @@ -6,6 +6,7 @@ import _pytest._code import py import pytest from test_excinfo import TWMock +from six import text_type def test_ne(): @@ -93,7 +94,7 @@ def test_unicode_handling(): excinfo = pytest.raises(Exception, f) str(excinfo) if sys.version_info[0] < 3: - unicode(excinfo) + text_type(excinfo) @pytest.mark.skipif(sys.version_info[0] >= 3, reason="python 2 only issue") @@ -106,7 +107,7 @@ def test_unicode_handling_syntax_error(): excinfo = pytest.raises(Exception, f) str(excinfo) if sys.version_info[0] < 3: - unicode(excinfo) + text_type(excinfo) def test_code_getargs(): diff --git a/testing/python/collect.py b/testing/python/collect.py index fd19d52a3..724504b1a 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -847,7 +847,7 @@ class TestSorting(object): assert fn1 == fn2 assert fn1 != modcol if sys.version_info < (3, 0): - assert cmp(fn1, fn2) == 0 + assert cmp(fn1, fn2) == 0 # NOQA assert hash(fn1) == hash(fn2) fn3 = testdir.collect_by_name(modcol, "test_fail") diff --git a/testing/test_capture.py b/testing/test_capture.py index 68655ff9f..5cedc99bb 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -12,7 +12,7 @@ import _pytest._code import py import pytest import contextlib - +from six import binary_type, text_type from _pytest import capture from _pytest.capture import CaptureManager from _pytest.main import EXIT_NOTESTSCOLLECTED @@ -20,34 +20,19 @@ from _pytest.main import EXIT_NOTESTSCOLLECTED needsosdup = pytest.mark.xfail("not hasattr(os, 'dup')") -if sys.version_info >= (3, 0): - def tobytes(obj): - if isinstance(obj, str): - obj = obj.encode("UTF-8") - assert isinstance(obj, bytes) - return obj - - def totext(obj): - if isinstance(obj, bytes): - obj = str(obj, "UTF-8") - assert isinstance(obj, str) - return obj +def tobytes(obj): + if isinstance(obj, text_type): + obj = obj.encode("UTF-8") + assert isinstance(obj, binary_type) + return obj -else: - - def tobytes(obj): - if isinstance(obj, unicode): - obj = obj.encode("UTF-8") - assert isinstance(obj, str) - return obj - - def totext(obj): - if isinstance(obj, str): - obj = unicode(obj, "UTF-8") - assert isinstance(obj, unicode) - return obj +def totext(obj): + if isinstance(obj, binary_type): + obj = text_type(obj, "UTF-8") + assert isinstance(obj, text_type) + return obj def oswritebytes(fd, obj): @@ -800,11 +785,11 @@ class TestCaptureIO(object): f.write("\u00f6") pytest.raises(TypeError, "f.write(bytes('hello', 'UTF-8'))") else: - f.write(unicode("\u00f6", "UTF-8")) + f.write(text_type("\u00f6", "UTF-8")) f.write("hello") # bytes s = f.getvalue() f.close() - assert isinstance(s, unicode) + assert isinstance(s, text_type) @pytest.mark.skipif(sys.version_info[0] == 2, reason="python 3 only behaviour") def test_write_bytes_to_buffer(self): diff --git a/testing/test_collection.py b/testing/test_collection.py index 1c6e2bdf1..657d64c74 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -44,7 +44,7 @@ class TestCollector(object): assert fn1 == fn2 assert fn1 != modcol if sys.version_info < (3, 0): - assert cmp(fn1, fn2) == 0 + assert cmp(fn1, fn2) == 0 # NOQA assert hash(fn1) == hash(fn2) fn3 = testdir.collect_by_name(modcol, "test_fail")