From 2fe178488acbfe8eed32727680c884bacbcec7c2 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 1 Jul 2020 20:20:06 +0300 Subject: [PATCH] code/source: expose deindent kwarg in signature Probably was done to avoid the shadowing issue, but work around it instead. --- src/_pytest/_code/source.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py index 2ccbaf657..1c69498ea 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -31,9 +31,8 @@ class Source: _compilecounter = 0 - def __init__(self, *parts, **kwargs) -> None: + def __init__(self, *parts, deindent: bool = True) -> None: self.lines = lines = [] # type: List[str] - de = kwargs.get("deindent", True) for part in parts: if not part: partlines = [] # type: List[str] @@ -44,9 +43,9 @@ class Source: elif isinstance(part, str): partlines = part.split("\n") else: - partlines = getsource(part, deindent=de).lines - if de: - partlines = deindent(partlines) + partlines = getsource(part, deindent=deindent).lines + if deindent: + partlines = _deindent_function(partlines) lines.extend(partlines) def __eq__(self, other): @@ -307,20 +306,24 @@ def getrawcode(obj, trycall: bool = True): return obj -def getsource(obj, **kwargs) -> Source: +def getsource(obj, *, deindent: bool = True) -> Source: obj = getrawcode(obj) try: strsrc = inspect.getsource(obj) except IndentationError: strsrc = '"Buggy python version consider upgrading, cannot get source"' assert isinstance(strsrc, str) - return Source(strsrc, **kwargs) + return Source(strsrc, deindent=deindent) def deindent(lines: Sequence[str]) -> List[str]: 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]]: # flatten all statements and except handlers into one lineno-list # AST's line numbers start indexing at 1