Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
('"bobro"', QuoteTypes.double),
("'''abra'''", QuoteTypes.triple_single),
('"""bobro"""', QuoteTypes.triple_double),
],
)
def test_get_quote_type(code, quote_type):
assert get_quote_type(code) == quote_type
def transform_concat(code: str, *args, **kwargs) -> Tuple[str, bool]:
tree = ast.parse(f"({code})")
ft = ConcatTransformer()
ft.visit(tree)
il = FstrInliner()
il.visit(tree)
new_code = astor.to_source(tree)
if new_code[-1] == "\n":
new_code = new_code[:-1]
new_code = new_code.replace("\n", "\\n")
if new_code[:4] == 'f"""':
new_code = set_quote_type(new_code, QuoteTypes.double)
return new_code, ft.counter > 0
def try_chunk(self, chunk):
for line in self.src_lines[chunk.start_line : chunk.start_line + chunk.n_lines]:
if noqa_regex.findall(line):
# user does not wish for this line to be converted.
return
try:
if chunk.string_in_string:
quote_type = qt.double
else:
quote_type = chunk.quote_type
except FlyntException as e:
if state.verbose:
print(f"Exception {e} during conversion of code '{str(chunk)}'")
traceback.print_exc()
else:
converted, changed = self.transform_func(str(chunk), quote_type=quote_type)
if changed:
contract_lines = chunk.n_lines - 1
if contract_lines == 0:
line = self.src_lines[chunk.start_line]
rest = line[chunk.end_idx:]
else:
except (SyntaxError, FlyntException, Exception) as e:
if state.verbose:
if isinstance(e, ConversionRefused):
print(f"Not converting code '{code}': {e}")
print(e)
else:
print(f"Exception {e} during conversion of code '{code}'")
traceback.print_exc()
state.invalid_conversions += 1
return code, False
else:
if changed:
new_code = astor.to_source(converted)
new_code = new_code.strip()
new_code = set_quote_type(
new_code, quote_type if not str_in_str else QuoteTypes.double
)
new_code = new_code.replace("\n", "\\n")
new_code = new_code.replace("\t", "\\t")
try:
ast.parse(new_code)
except Exception as e:
if state.verbose:
print(
f"Failed to parse transformed code '{new_code}' given original '{code}'"
)
print(e)
traceback.print_exc()
state.invalid_conversions += 1
return code, False
else:
return new_code, changed