code: simplify Code construction

This commit is contained in:
Ran Benita
2020-10-27 16:07:03 +02:00
parent 6506f016ac
commit 531416cc5a
7 changed files with 40 additions and 45 deletions

View File

@@ -28,11 +28,12 @@ def test_code_gives_back_name_for_not_existing_file() -> None:
assert code.fullsource is None
def test_code_with_class() -> None:
def test_code_from_function_with_class() -> None:
class A:
pass
pytest.raises(TypeError, Code, A)
with pytest.raises(TypeError):
Code.from_function(A)
def x() -> None:
@@ -40,13 +41,13 @@ def x() -> None:
def test_code_fullsource() -> None:
code = Code(x)
code = Code.from_function(x)
full = code.fullsource
assert "test_code_fullsource()" in str(full)
def test_code_source() -> None:
code = Code(x)
code = Code.from_function(x)
src = code.source()
expected = """def x() -> None:
raise NotImplementedError()"""
@@ -73,7 +74,7 @@ def test_getstatement_empty_fullsource() -> None:
def test_code_from_func() -> None:
co = Code(test_frame_getsourcelineno_myself)
co = Code.from_function(test_frame_getsourcelineno_myself)
assert co.firstlineno
assert co.path
@@ -92,25 +93,25 @@ def test_code_getargs() -> None:
def f1(x):
raise NotImplementedError()
c1 = Code(f1)
c1 = Code.from_function(f1)
assert c1.getargs(var=True) == ("x",)
def f2(x, *y):
raise NotImplementedError()
c2 = Code(f2)
c2 = Code.from_function(f2)
assert c2.getargs(var=True) == ("x", "y")
def f3(x, **z):
raise NotImplementedError()
c3 = Code(f3)
c3 = Code.from_function(f3)
assert c3.getargs(var=True) == ("x", "z")
def f4(x, *y, **z):
raise NotImplementedError()
c4 = Code(f4)
c4 = Code.from_function(f4)
assert c4.getargs(var=True) == ("x", "y", "z")

View File

@@ -147,7 +147,7 @@ class TestTraceback_f_g_h:
]
def test_traceback_cut(self):
co = _pytest._code.Code(f)
co = _pytest._code.Code.from_function(f)
path, firstlineno = co.path, co.firstlineno
traceback = self.excinfo.traceback
newtraceback = traceback.cut(path=path, firstlineno=firstlineno)
@@ -290,7 +290,7 @@ class TestTraceback_f_g_h:
excinfo = pytest.raises(ValueError, f)
tb = excinfo.traceback
entry = tb.getcrashentry()
co = _pytest._code.Code(h)
co = _pytest._code.Code.from_function(h)
assert entry.frame.code.path == co.path
assert entry.lineno == co.firstlineno + 1
assert entry.frame.code.name == "h"
@@ -307,7 +307,7 @@ class TestTraceback_f_g_h:
excinfo = pytest.raises(ValueError, f)
tb = excinfo.traceback
entry = tb.getcrashentry()
co = _pytest._code.Code(g)
co = _pytest._code.Code.from_function(g)
assert entry.frame.code.path == co.path
assert entry.lineno == co.firstlineno + 2
assert entry.frame.code.name == "g"

View File

@@ -16,8 +16,8 @@ import py.path
import pytest
from _pytest._code import Code
from _pytest._code import Frame
from _pytest._code import Source
from _pytest._code import getfslineno
from _pytest._code import Source
def test_source_str_function() -> None:
@@ -291,7 +291,7 @@ def test_source_of_class_at_eof_without_newline(tmpdir, _sys_snapshot) -> None:
# does not return the "x = 1" last line.
source = Source(
"""
class A(object):
class A:
def method(self):
x = 1
"""
@@ -374,14 +374,6 @@ def test_getfslineno() -> None:
B.__name__ = B.__qualname__ = "B2"
assert getfslineno(B)[1] == -1
co = compile("...", "", "eval")
assert co.co_filename == ""
if hasattr(sys, "pypy_version_info"):
assert getfslineno(co) == ("", -1)
else:
assert getfslineno(co) == ("", 0)
def test_code_of_object_instance_with_call() -> None:
class A:
@@ -393,14 +385,14 @@ def test_code_of_object_instance_with_call() -> None:
def __call__(self) -> None:
pass
code = Code(WithCall())
code = Code.from_function(WithCall())
assert "pass" in str(code.source())
class Hello:
def __call__(self) -> None:
pass
pytest.raises(TypeError, lambda: Code(Hello))
pytest.raises(TypeError, lambda: Code.from_function(Hello))
def getstatement(lineno: int, source) -> Source:

View File

@@ -42,7 +42,7 @@ def getmsg(
f, extra_ns: Optional[Mapping[str, object]] = None, *, must_pass: bool = False
) -> Optional[str]:
"""Rewrite the assertions in f, run it, and get the failure message."""
src = "\n".join(_pytest._code.Code(f).source().lines)
src = "\n".join(_pytest._code.Code.from_function(f).source().lines)
mod = rewrite(src)
code = compile(mod, "<test>", "exec")
ns: Dict[str, object] = {}