Skip to content

Commit 6633044

Browse files
authored
Merge pull request #6611 from blueyed/test_code-imports
tests: test_code: improve/clarify imports
2 parents b429384 + 3dbc61d commit 6633044

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

testing/code/test_code.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@
55

66
import py.path
77

8-
import _pytest._code
98
import pytest
9+
from _pytest._code import Code
10+
from _pytest._code import ExceptionInfo
11+
from _pytest._code import Frame
1012
from _pytest._code import getfslineno
13+
from _pytest._code.code import ReprFuncArgs
1114

1215

1316
def test_ne() -> None:
14-
code1 = _pytest._code.Code(compile('foo = "bar"', "", "exec"))
17+
code1 = Code(compile('foo = "bar"', "", "exec"))
1518
assert code1 == code1
16-
code2 = _pytest._code.Code(compile('foo = "baz"', "", "exec"))
19+
code2 = Code(compile('foo = "baz"', "", "exec"))
1720
assert code2 != code1
1821

1922

2023
def test_code_gives_back_name_for_not_existing_file() -> None:
2124
name = "abc-123"
2225
co_code = compile("pass\n", name, "exec")
2326
assert co_code.co_filename == name
24-
code = _pytest._code.Code(co_code)
27+
code = Code(co_code)
2528
assert str(code.path) == name
2629
assert code.fullsource is None
2730

@@ -30,21 +33,21 @@ def test_code_with_class() -> None:
3033
class A:
3134
pass
3235

33-
pytest.raises(TypeError, _pytest._code.Code, A)
36+
pytest.raises(TypeError, Code, A)
3437

3538

3639
def x() -> None:
3740
raise NotImplementedError()
3841

3942

4043
def test_code_fullsource() -> None:
41-
code = _pytest._code.Code(x)
44+
code = Code(x)
4245
full = code.fullsource
4346
assert "test_code_fullsource()" in str(full)
4447

4548

4649
def test_code_source() -> None:
47-
code = _pytest._code.Code(x)
50+
code = Code(x)
4851
src = code.source()
4952
expected = """def x() -> None:
5053
raise NotImplementedError()"""
@@ -55,7 +58,7 @@ def test_frame_getsourcelineno_myself() -> None:
5558
def func() -> FrameType:
5659
return sys._getframe(0)
5760

58-
f = _pytest._code.Frame(func())
61+
f = Frame(func())
5962
source, lineno = f.code.fullsource, f.lineno
6063
assert source is not None
6164
assert source[lineno].startswith(" return sys._getframe(0)")
@@ -65,13 +68,13 @@ def test_getstatement_empty_fullsource() -> None:
6568
def func() -> FrameType:
6669
return sys._getframe(0)
6770

68-
f = _pytest._code.Frame(func())
71+
f = Frame(func())
6972
with mock.patch.object(f.code.__class__, "fullsource", None):
7073
assert f.statement == ""
7174

7275

7376
def test_code_from_func() -> None:
74-
co = _pytest._code.Code(test_frame_getsourcelineno_myself)
77+
co = Code(test_frame_getsourcelineno_myself)
7578
assert co.firstlineno
7679
assert co.path
7780

@@ -90,51 +93,51 @@ def test_code_getargs() -> None:
9093
def f1(x):
9194
raise NotImplementedError()
9295

93-
c1 = _pytest._code.Code(f1)
96+
c1 = Code(f1)
9497
assert c1.getargs(var=True) == ("x",)
9598

9699
def f2(x, *y):
97100
raise NotImplementedError()
98101

99-
c2 = _pytest._code.Code(f2)
102+
c2 = Code(f2)
100103
assert c2.getargs(var=True) == ("x", "y")
101104

102105
def f3(x, **z):
103106
raise NotImplementedError()
104107

105-
c3 = _pytest._code.Code(f3)
108+
c3 = Code(f3)
106109
assert c3.getargs(var=True) == ("x", "z")
107110

108111
def f4(x, *y, **z):
109112
raise NotImplementedError()
110113

111-
c4 = _pytest._code.Code(f4)
114+
c4 = Code(f4)
112115
assert c4.getargs(var=True) == ("x", "y", "z")
113116

114117

115118
def test_frame_getargs() -> None:
116119
def f1(x) -> FrameType:
117120
return sys._getframe(0)
118121

119-
fr1 = _pytest._code.Frame(f1("a"))
122+
fr1 = Frame(f1("a"))
120123
assert fr1.getargs(var=True) == [("x", "a")]
121124

122125
def f2(x, *y) -> FrameType:
123126
return sys._getframe(0)
124127

125-
fr2 = _pytest._code.Frame(f2("a", "b", "c"))
128+
fr2 = Frame(f2("a", "b", "c"))
126129
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))]
127130

128131
def f3(x, **z) -> FrameType:
129132
return sys._getframe(0)
130133

131-
fr3 = _pytest._code.Frame(f3("a", b="c"))
134+
fr3 = Frame(f3("a", b="c"))
132135
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})]
133136

134137
def f4(x, *y, **z) -> FrameType:
135138
return sys._getframe(0)
136139

137-
fr4 = _pytest._code.Frame(f4("a", "b", c="d"))
140+
fr4 = Frame(f4("a", "b", c="d"))
138141
assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})]
139142

140143

@@ -146,12 +149,12 @@ def test_bad_getsource(self) -> None:
146149
else:
147150
assert False
148151
except AssertionError:
149-
exci = _pytest._code.ExceptionInfo.from_current()
152+
exci = ExceptionInfo.from_current()
150153
assert exci.getrepr()
151154

152155
def test_from_current_with_missing(self) -> None:
153156
with pytest.raises(AssertionError, match="no current exception"):
154-
_pytest._code.ExceptionInfo.from_current()
157+
ExceptionInfo.from_current()
155158

156159

157160
class TestTracebackEntry:
@@ -162,7 +165,7 @@ def test_getsource(self) -> None:
162165
else:
163166
assert False
164167
except AssertionError:
165-
exci = _pytest._code.ExceptionInfo.from_current()
168+
exci = ExceptionInfo.from_current()
166169
entry = exci.traceback[0]
167170
source = entry.getsource()
168171
assert source is not None
@@ -172,8 +175,6 @@ def test_getsource(self) -> None:
172175

173176
class TestReprFuncArgs:
174177
def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None:
175-
from _pytest._code.code import ReprFuncArgs
176-
177178
args = [("unicode_string", "São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")]
178179

179180
r = ReprFuncArgs(args)

0 commit comments

Comments
 (0)