code/source: remove Source(deindent: bool) parameter
Not used, except in tests.
This commit is contained in:
parent
2fe178488a
commit
4108174777
|
@ -25,13 +25,14 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
|
|
||||||
class Source:
|
class Source:
|
||||||
""" an immutable object holding a source code fragment,
|
"""An immutable object holding a source code fragment.
|
||||||
possibly deindenting it.
|
|
||||||
|
When using Source(...), the source lines are deindented.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_compilecounter = 0
|
_compilecounter = 0
|
||||||
|
|
||||||
def __init__(self, *parts, deindent: bool = True) -> None:
|
def __init__(self, *parts) -> None:
|
||||||
self.lines = lines = [] # type: List[str]
|
self.lines = lines = [] # type: List[str]
|
||||||
for part in parts:
|
for part in parts:
|
||||||
if not part:
|
if not part:
|
||||||
|
@ -43,9 +44,8 @@ class Source:
|
||||||
elif isinstance(part, str):
|
elif isinstance(part, str):
|
||||||
partlines = part.split("\n")
|
partlines = part.split("\n")
|
||||||
else:
|
else:
|
||||||
partlines = getsource(part, deindent=deindent).lines
|
partlines = getsource(part).lines
|
||||||
if deindent:
|
partlines = deindent(partlines)
|
||||||
partlines = _deindent_function(partlines)
|
|
||||||
lines.extend(partlines)
|
lines.extend(partlines)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
@ -306,24 +306,20 @@ def getrawcode(obj, trycall: bool = True):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def getsource(obj, *, deindent: bool = True) -> Source:
|
def getsource(obj) -> Source:
|
||||||
obj = getrawcode(obj)
|
obj = getrawcode(obj)
|
||||||
try:
|
try:
|
||||||
strsrc = inspect.getsource(obj)
|
strsrc = inspect.getsource(obj)
|
||||||
except IndentationError:
|
except IndentationError:
|
||||||
strsrc = '"Buggy python version consider upgrading, cannot get source"'
|
strsrc = '"Buggy python version consider upgrading, cannot get source"'
|
||||||
assert isinstance(strsrc, str)
|
assert isinstance(strsrc, str)
|
||||||
return Source(strsrc, deindent=deindent)
|
return Source(strsrc)
|
||||||
|
|
||||||
|
|
||||||
def deindent(lines: Sequence[str]) -> List[str]:
|
def deindent(lines: Sequence[str]) -> List[str]:
|
||||||
return textwrap.dedent("\n".join(lines)).splitlines()
|
return textwrap.dedent("\n".join(lines)).splitlines()
|
||||||
|
|
||||||
|
|
||||||
# Internal alias to avoid shadowing with `deindent` parameter.
|
|
||||||
_deindent_function = deindent
|
|
||||||
|
|
||||||
|
|
||||||
def get_statement_startend2(lineno: int, node: ast.AST) -> Tuple[int, Optional[int]]:
|
def get_statement_startend2(lineno: int, node: ast.AST) -> Tuple[int, Optional[int]]:
|
||||||
# flatten all statements and except handlers into one lineno-list
|
# flatten all statements and except handlers into one lineno-list
|
||||||
# AST's line numbers start indexing at 1
|
# AST's line numbers start indexing at 1
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
import ast
|
import ast
|
||||||
import inspect
|
import inspect
|
||||||
import sys
|
import sys
|
||||||
|
import textwrap
|
||||||
from types import CodeType
|
from types import CodeType
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
@ -64,8 +65,6 @@ def test_source_from_inner_function() -> None:
|
||||||
def f():
|
def f():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
source = _pytest._code.Source(f, deindent=False)
|
|
||||||
assert str(source).startswith(" def f():")
|
|
||||||
source = _pytest._code.Source(f)
|
source = _pytest._code.Source(f)
|
||||||
assert str(source).startswith("def f():")
|
assert str(source).startswith("def f():")
|
||||||
|
|
||||||
|
@ -557,7 +556,7 @@ def test_code_of_object_instance_with_call() -> None:
|
||||||
def getstatement(lineno: int, source) -> Source:
|
def getstatement(lineno: int, source) -> Source:
|
||||||
from _pytest._code.source import getstatementrange_ast
|
from _pytest._code.source import getstatementrange_ast
|
||||||
|
|
||||||
src = _pytest._code.Source(source, deindent=False)
|
src = _pytest._code.Source(source)
|
||||||
ast, start, end = getstatementrange_ast(lineno, src)
|
ast, start, end = getstatementrange_ast(lineno, src)
|
||||||
return src[start:end]
|
return src[start:end]
|
||||||
|
|
||||||
|
@ -633,7 +632,7 @@ def test_source_with_decorator() -> None:
|
||||||
assert False
|
assert False
|
||||||
|
|
||||||
src = inspect.getsource(deco_mark)
|
src = inspect.getsource(deco_mark)
|
||||||
assert str(Source(deco_mark, deindent=False)) == src
|
assert textwrap.indent(str(Source(deco_mark)), " ") + "\n" == src
|
||||||
assert src.startswith(" @pytest.mark.foo")
|
assert src.startswith(" @pytest.mark.foo")
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -646,7 +645,9 @@ def test_source_with_decorator() -> None:
|
||||||
# existing behavior here for explicitness, but perhaps we should revisit/change this
|
# existing behavior here for explicitness, but perhaps we should revisit/change this
|
||||||
# in the future
|
# in the future
|
||||||
assert str(Source(deco_fixture)).startswith("@functools.wraps(function)")
|
assert str(Source(deco_fixture)).startswith("@functools.wraps(function)")
|
||||||
assert str(Source(get_real_func(deco_fixture), deindent=False)) == src
|
assert (
|
||||||
|
textwrap.indent(str(Source(get_real_func(deco_fixture))), " ") + "\n" == src
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_single_line_else() -> None:
|
def test_single_line_else() -> None:
|
||||||
|
|
Loading…
Reference in New Issue