Elffuss BraidHub

kikocis/auto-code-rover public

main · 14.1 KiBRawPermalink
1{
2 "task_id": "pytest-dev__pytest-5103",
3 "setup_info": {
4 "repo_path": "/home/haifeng/projects/reverse-prompt/SWE-bench/testbed/pytest-dev__pytest/setup_pytest-dev__pytest__4.5",
5 "env_name": "setup_pytest-dev__pytest__4.5",
6 "pre_install": [],
7 "install": "python -m pip install -e .",
8 "test_cmd": "pytest -rA testing/test_assertrewrite.py"
9 },
10 "task_info": {
11 "instance_id": "pytest-dev__pytest-5103",
12 "base_commit": "10ca84ffc56c2dd2d9dc4bd71b7b898e083500cd",
13 "hints_text": "Hello, I am new here and would be interested in working on this issue if that is possible.\n@danielx123 \r\nSure! But I don't think this is an easy issue, since it involved the assertion rewriting - but if you're familar with Python's AST and pytest's internals feel free to pick this up.\r\nWe also have a tag \"easy\" for issues that are probably easier for starting contributors: https://github.com/pytest-dev/pytest/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+easy%22\nI was planning on starting a pr today, but probably won't be able to finish it until next week - @danielx123 maybe we could collaborate? ",
14 "created_at": "2019-04-13T16:17:45Z",
15 "test_patch": "diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py\n--- a/testing/test_assertrewrite.py\n+++ b/testing/test_assertrewrite.py\n@@ -656,6 +656,12 @@ def __repr__(self):\n else:\n assert lines == [\"assert 0 == 1\\n + where 1 = \\\\n{ \\\\n~ \\\\n}.a\"]\n \n+ def test_unroll_expression(self):\n+ def f():\n+ assert all(x == 1 for x in range(10))\n+\n+ assert \"0 == 1\" in getmsg(f)\n+\n def test_custom_repr_non_ascii(self):\n def f():\n class A(object):\n@@ -671,6 +677,53 @@ def __repr__(self):\n assert \"UnicodeDecodeError\" not in msg\n assert \"UnicodeEncodeError\" not in msg\n \n+ def test_unroll_generator(self, testdir):\n+ testdir.makepyfile(\n+ \"\"\"\n+ def check_even(num):\n+ if num % 2 == 0:\n+ return True\n+ return False\n+\n+ def test_generator():\n+ odd_list = list(range(1,9,2))\n+ assert all(check_even(num) for num in odd_list)\"\"\"\n+ )\n+ result = testdir.runpytest()\n+ result.stdout.fnmatch_lines([\"*assert False*\", \"*where False = check_even(1)*\"])\n+\n+ def test_unroll_list_comprehension(self, testdir):\n+ testdir.makepyfile(\n+ \"\"\"\n+ def check_even(num):\n+ if num % 2 == 0:\n+ return True\n+ return False\n+\n+ def test_list_comprehension():\n+ odd_list = list(range(1,9,2))\n+ assert all([check_even(num) for num in odd_list])\"\"\"\n+ )\n+ result = testdir.runpytest()\n+ result.stdout.fnmatch_lines([\"*assert False*\", \"*where False = check_even(1)*\"])\n+\n+ def test_for_loop(self, testdir):\n+ testdir.makepyfile(\n+ \"\"\"\n+ def check_even(num):\n+ if num % 2 == 0:\n+ return True\n+ return False\n+\n+ def test_for_loop():\n+ odd_list = list(range(1,9,2))\n+ for num in odd_list:\n+ assert check_even(num)\n+ \"\"\"\n+ )\n+ result = testdir.runpytest()\n+ result.stdout.fnmatch_lines([\"*assert False*\", \"*where False = check_even(1)*\"])\n+\n \n class TestRewriteOnImport(object):\n def test_pycache_is_a_file(self, testdir):\n",
16 "repo": "pytest-dev/pytest",
17 "problem_statement": "Unroll the iterable for all/any calls to get better reports\nSometime I need to assert some predicate on all of an iterable, and for that the builtin functions `all`/`any` are great - but the failure messages aren't useful at all!\r\nFor example - the same test written in three ways:\r\n\r\n- A generator expression\r\n```sh \r\n def test_all_even():\r\n even_stevens = list(range(1,100,2))\r\n> assert all(is_even(number) for number in even_stevens)\r\nE assert False\r\nE + where False = all(<generator object test_all_even.<locals>.<genexpr> at 0x101f82ed0>)\r\n```\r\n- A list comprehension\r\n```sh\r\n def test_all_even():\r\n even_stevens = list(range(1,100,2))\r\n> assert all([is_even(number) for number in even_stevens])\r\nE assert False\r\nE + where False = all([False, False, False, False, False, False, ...])\r\n```\r\n- A for loop\r\n```sh\r\n def test_all_even():\r\n even_stevens = list(range(1,100,2))\r\n for number in even_stevens:\r\n> assert is_even(number)\r\nE assert False\r\nE + where False = is_even(1)\r\n\r\ntest_all_any.py:7: AssertionError\r\n```\r\nThe only one that gives a meaningful report is the for loop - but it's way more wordy, and `all` asserts don't translate to a for loop nicely (I'll have to write a `break` or a helper function - yuck)\r\nI propose the assertion re-writer \"unrolls\" the iterator to the third form, and then uses the already existing reports.\r\n\r\n- [x] Include a detailed description of the bug or suggestion\r\n- [x] `pip list` of the virtual environment you are using\r\n```\r\nPackage Version\r\n-------------- -------\r\natomicwrites 1.3.0 \r\nattrs 19.1.0 \r\nmore-itertools 7.0.0 \r\npip 19.0.3 \r\npluggy 0.9.0 \r\npy 1.8.0 \r\npytest 4.4.0 \r\nsetuptools 40.8.0 \r\nsix 1.12.0 \r\n```\r\n- [x] pytest and operating system versions\r\n`platform darwin -- Python 3.7.3, pytest-4.4.0, py-1.8.0, pluggy-0.9.0`\r\n- [x] Minimal example if possible\r\n\n",
18 "version": "4.5",
19 "FAIL_TO_PASS": [
20 "testing/test_assertrewrite.py::TestAssertionRewrite::test_unroll_expression"
21 ],
22 "PASS_TO_PASS": [
23 "testing/test_assertrewrite.py::TestAssertionRewrite::test_assert_already_has_message",
24 "testing/test_assertrewrite.py::TestAssertionRewrite::test_assert_raising_nonzero_in_comparison",
25 "testing/test_assertrewrite.py::TestAssertionRewrite::test_assertion_message",
26 "testing/test_assertrewrite.py::TestAssertionRewrite::test_assertion_message_escape",
27 "testing/test_assertrewrite.py::TestAssertionRewrite::test_assertion_message_expr",
28 "testing/test_assertrewrite.py::TestAssertionRewrite::test_assertion_message_multiline",
29 "testing/test_assertrewrite.py::TestAssertionRewrite::test_assertion_message_tuple",
30 "testing/test_assertrewrite.py::TestAssertionRewrite::test_assertion_messages_bytes",
31 "testing/test_assertrewrite.py::TestAssertionRewrite::test_at_operator_issue1290",
32 "testing/test_assertrewrite.py::TestAssertionRewrite::test_attribute",
33 "testing/test_assertrewrite.py::TestAssertionRewrite::test_binary_op",
34 "testing/test_assertrewrite.py::TestAssertionRewrite::test_boolop",
35 "testing/test_assertrewrite.py::TestAssertionRewrite::test_boolop_percent",
36 "testing/test_assertrewrite.py::TestAssertionRewrite::test_call",
37 "testing/test_assertrewrite.py::TestAssertionRewrite::test_comparisons",
38 "testing/test_assertrewrite.py::TestAssertionRewrite::test_custom_repr",
39 "testing/test_assertrewrite.py::TestAssertionRewrite::test_custom_repr_non_ascii",
40 "testing/test_assertrewrite.py::TestAssertionRewrite::test_custom_reprcompare",
41 "testing/test_assertrewrite.py::TestAssertionRewrite::test_dont_rewrite",
42 "testing/test_assertrewrite.py::TestAssertionRewrite::test_dont_rewrite_if_hasattr_fails",
43 "testing/test_assertrewrite.py::TestAssertionRewrite::test_dont_rewrite_plugin",
44 "testing/test_assertrewrite.py::TestAssertionRewrite::test_for_loop",
45 "testing/test_assertrewrite.py::TestAssertionRewrite::test_formatchar",
46 "testing/test_assertrewrite.py::TestAssertionRewrite::test_len",
47 "testing/test_assertrewrite.py::TestAssertionRewrite::test_name",
48 "testing/test_assertrewrite.py::TestAssertionRewrite::test_place_initial_imports",
49 "testing/test_assertrewrite.py::TestAssertionRewrite::test_short_circuit_evaluation",
50 "testing/test_assertrewrite.py::TestAssertionRewrite::test_starred_with_side_effect",
51 "testing/test_assertrewrite.py::TestAssertionRewrite::test_unary_op",
52 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_get_data_support",
53 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_loader_is_package_false_for_module",
54 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_loader_is_package_true_for_package",
55 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_read_pyc",
56 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_reload_is_same",
57 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_reload_reloads",
58 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_resources_provider_for_loader",
59 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_sys_meta_path_munged",
60 "testing/test_assertrewrite.py::TestAssertionRewriteHookDetails::test_write_pyc",
61 "testing/test_assertrewrite.py::TestEarlyRewriteBailout::test_basic",
62 "testing/test_assertrewrite.py::TestEarlyRewriteBailout::test_cwd_changed",
63 "testing/test_assertrewrite.py::TestEarlyRewriteBailout::test_pattern_contains_subdirectories",
64 "testing/test_assertrewrite.py::TestIssue2121::test_rewrite_python_files_contain_subdirs",
65 "testing/test_assertrewrite.py::TestIssue925::test_long_case",
66 "testing/test_assertrewrite.py::TestIssue925::test_many_brackets",
67 "testing/test_assertrewrite.py::TestIssue925::test_simple_case",
68 "testing/test_assertrewrite.py::TestRewriteOnImport::test_dont_write_bytecode",
69 "testing/test_assertrewrite.py::TestRewriteOnImport::test_orphaned_pyc_file",
70 "testing/test_assertrewrite.py::TestRewriteOnImport::test_package",
71 "testing/test_assertrewrite.py::TestRewriteOnImport::test_package_without__init__py",
72 "testing/test_assertrewrite.py::TestRewriteOnImport::test_pyc_vs_pyo",
73 "testing/test_assertrewrite.py::TestRewriteOnImport::test_pycache_is_a_file",
74 "testing/test_assertrewrite.py::TestRewriteOnImport::test_pycache_is_readonly",
75 "testing/test_assertrewrite.py::TestRewriteOnImport::test_readonly",
76 "testing/test_assertrewrite.py::TestRewriteOnImport::test_remember_rewritten_modules",
77 "testing/test_assertrewrite.py::TestRewriteOnImport::test_rewrite_module_imported_from_conftest",
78 "testing/test_assertrewrite.py::TestRewriteOnImport::test_rewrite_warning",
79 "testing/test_assertrewrite.py::TestRewriteOnImport::test_rewrite_warning_using_pytest_plugins",
80 "testing/test_assertrewrite.py::TestRewriteOnImport::test_rewrite_warning_using_pytest_plugins_env_var",
81 "testing/test_assertrewrite.py::TestRewriteOnImport::test_translate_newlines",
82 "testing/test_assertrewrite.py::TestRewriteOnImport::test_zipfile",
83 "testing/test_assertrewrite.py::test_issue731",
84 "testing/test_assertrewrite.py::test_rewrite_infinite_recursion",
85 "testing/test_assertrewrite.py::test_source_mtime_long_long[-1]",
86 "testing/test_assertrewrite.py::test_source_mtime_long_long[1]"
87 ],
88 "environment_setup_commit": "693c3b7f61d4d32f8927a74f34ce8ac56d63958e",
89 "patch": "diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py\n--- a/src/_pytest/assertion/rewrite.py\n+++ b/src/_pytest/assertion/rewrite.py\n@@ -964,6 +964,8 @@ def visit_Call_35(self, call):\n \"\"\"\n visit `ast.Call` nodes on Python3.5 and after\n \"\"\"\n+ if isinstance(call.func, ast.Name) and call.func.id == \"all\":\n+ return self._visit_all(call)\n new_func, func_expl = self.visit(call.func)\n arg_expls = []\n new_args = []\n@@ -987,6 +989,27 @@ def visit_Call_35(self, call):\n outer_expl = \"%s\\n{%s = %s\\n}\" % (res_expl, res_expl, expl)\n return res, outer_expl\n \n+ def _visit_all(self, call):\n+ \"\"\"Special rewrite for the builtin all function, see #5062\"\"\"\n+ if not isinstance(call.args[0], (ast.GeneratorExp, ast.ListComp)):\n+ return\n+ gen_exp = call.args[0]\n+ assertion_module = ast.Module(\n+ body=[ast.Assert(test=gen_exp.elt, lineno=1, msg=\"\", col_offset=1)]\n+ )\n+ AssertionRewriter(module_path=None, config=None).run(assertion_module)\n+ for_loop = ast.For(\n+ iter=gen_exp.generators[0].iter,\n+ target=gen_exp.generators[0].target,\n+ body=assertion_module.body,\n+ orelse=[],\n+ )\n+ self.statements.append(for_loop)\n+ return (\n+ ast.Num(n=1),\n+ \"\",\n+ ) # Return an empty expression, all the asserts are in the for_loop\n+\n def visit_Starred(self, starred):\n # From Python 3.5, a Starred node can appear in a function call\n res, expl = self.visit(starred.value)\n@@ -997,6 +1020,8 @@ def visit_Call_legacy(self, call):\n \"\"\"\n visit `ast.Call nodes on 3.4 and below`\n \"\"\"\n+ if isinstance(call.func, ast.Name) and call.func.id == \"all\":\n+ return self._visit_all(call)\n new_func, func_expl = self.visit(call.func)\n arg_expls = []\n new_args = []\n",
90 "pr_link": "https://github.com/pytest-dev/pytest/pull/5103"
91 }
92}