Add type annotations to _pytest._code.code

This commit is contained in:
Ran Benita
2019-11-15 23:02:55 +02:00
parent 562d4811d5
commit eaa34a9df0
4 changed files with 299 additions and 214 deletions

View File

@@ -1,18 +1,19 @@
import sys
from types import FrameType
from unittest import mock
import _pytest._code
import pytest
def test_ne():
def test_ne() -> None:
code1 = _pytest._code.Code(compile('foo = "bar"', "", "exec"))
assert code1 == code1
code2 = _pytest._code.Code(compile('foo = "baz"', "", "exec"))
assert code2 != code1
def test_code_gives_back_name_for_not_existing_file():
def test_code_gives_back_name_for_not_existing_file() -> None:
name = "abc-123"
co_code = compile("pass\n", name, "exec")
assert co_code.co_filename == name
@@ -21,68 +22,67 @@ def test_code_gives_back_name_for_not_existing_file():
assert code.fullsource is None
def test_code_with_class():
def test_code_with_class() -> None:
class A:
pass
pytest.raises(TypeError, _pytest._code.Code, A)
def x():
def x() -> None:
raise NotImplementedError()
def test_code_fullsource():
def test_code_fullsource() -> None:
code = _pytest._code.Code(x)
full = code.fullsource
assert "test_code_fullsource()" in str(full)
def test_code_source():
def test_code_source() -> None:
code = _pytest._code.Code(x)
src = code.source()
expected = """def x():
expected = """def x() -> None:
raise NotImplementedError()"""
assert str(src) == expected
def test_frame_getsourcelineno_myself():
def func():
def test_frame_getsourcelineno_myself() -> None:
def func() -> FrameType:
return sys._getframe(0)
f = func()
f = _pytest._code.Frame(f)
f = _pytest._code.Frame(func())
source, lineno = f.code.fullsource, f.lineno
assert source is not None
assert source[lineno].startswith(" return sys._getframe(0)")
def test_getstatement_empty_fullsource():
def func():
def test_getstatement_empty_fullsource() -> None:
def func() -> FrameType:
return sys._getframe(0)
f = func()
f = _pytest._code.Frame(f)
f = _pytest._code.Frame(func())
with mock.patch.object(f.code.__class__, "fullsource", None):
assert f.statement == ""
def test_code_from_func():
def test_code_from_func() -> None:
co = _pytest._code.Code(test_frame_getsourcelineno_myself)
assert co.firstlineno
assert co.path
def test_unicode_handling():
def test_unicode_handling() -> None:
value = "ąć".encode()
def f():
def f() -> None:
raise Exception(value)
excinfo = pytest.raises(Exception, f)
str(excinfo)
def test_code_getargs():
def test_code_getargs() -> None:
def f1(x):
raise NotImplementedError()
@@ -108,26 +108,26 @@ def test_code_getargs():
assert c4.getargs(var=True) == ("x", "y", "z")
def test_frame_getargs():
def f1(x):
def test_frame_getargs() -> None:
def f1(x) -> FrameType:
return sys._getframe(0)
fr1 = _pytest._code.Frame(f1("a"))
assert fr1.getargs(var=True) == [("x", "a")]
def f2(x, *y):
def f2(x, *y) -> FrameType:
return sys._getframe(0)
fr2 = _pytest._code.Frame(f2("a", "b", "c"))
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))]
def f3(x, **z):
def f3(x, **z) -> FrameType:
return sys._getframe(0)
fr3 = _pytest._code.Frame(f3("a", b="c"))
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})]
def f4(x, *y, **z):
def f4(x, *y, **z) -> FrameType:
return sys._getframe(0)
fr4 = _pytest._code.Frame(f4("a", "b", c="d"))
@@ -135,7 +135,7 @@ def test_frame_getargs():
class TestExceptionInfo:
def test_bad_getsource(self):
def test_bad_getsource(self) -> None:
try:
if False:
pass
@@ -145,13 +145,13 @@ class TestExceptionInfo:
exci = _pytest._code.ExceptionInfo.from_current()
assert exci.getrepr()
def test_from_current_with_missing(self):
def test_from_current_with_missing(self) -> None:
with pytest.raises(AssertionError, match="no current exception"):
_pytest._code.ExceptionInfo.from_current()
class TestTracebackEntry:
def test_getsource(self):
def test_getsource(self) -> None:
try:
if False:
pass
@@ -161,12 +161,13 @@ class TestTracebackEntry:
exci = _pytest._code.ExceptionInfo.from_current()
entry = exci.traceback[0]
source = entry.getsource()
assert source is not None
assert len(source) == 6
assert "assert False" in source[5]
class TestReprFuncArgs:
def test_not_raise_exception_with_mixed_encoding(self, tw_mock):
def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None:
from _pytest._code.code import ReprFuncArgs
args = [("unicode_string", "São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")]