def try_chunk(self, chunk): start_line, start_idx, end_idx = chunk.start_line, chunk.start_idx, chunk.end_idx line = self.src_lines[start_line] contract_lines = chunk.n_lines - 1 if contract_lines == 0: rest = line[end_idx:] else: next_line = self.src_lines[start_line + contract_lines] rest = next_line[end_idx:] try: quote_type = chunk.tokens[0].get_quote_type() except FlyntException: pass else: converted, meta = transform_chunk(str(chunk), quote_type=quote_type) if meta['changed']: multiline_condition = (not contract_lines or len("".join( [converted, rest])) <= self.len_limit - start_idx) if multiline_condition: self.results.append(converted) self.count_expressions += 1 self.last_line += contract_lines self.last_idx = end_idx
def test_mixed_numbered(): code = '''"Flask Documentation ({1} {0:.2f} {name})".format(version,sprt,name=NAME)''' expected = '''f"""Flask Documentation ({sprt} {version:.2f} {NAME})"""''' new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_fmt_spec(): code = '''"my string {:.2f}".format(var)''' expected = '''f"""my string {var:.2f}"""''' new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_newline(): code = r'''"echo '{}'\n".format(self.FLUSH_CMD)''' expected = '''f"""echo '{self.FLUSH_CMD}'\\n"""''' new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_expr_no_paren(): code = '''"my string {:.2f}".format(var+1)''' expected = '''f"""my string {var + 1:.2f}"""''' new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_digit_grouping(): code = '''"Failed after {:,}".format(x)''' expected = '''f"""Failed after {x:,}"""''' new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_numbered(): code = '''"""Flask Documentation ({0})""".format(version)''' expected = '''f"""Flask Documentation ({version})"""''' new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_implicit_string_concat(): code = '''"Helloo {}" "!!!".format(world)''' expected = '''f"""Helloo {world}!!!"""''' new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_digit_grouping_2(): code = ''' "Search: finished in {0:,} ms.".format(vm.search_time_elapsed_ms) '''.strip() expected = ''' f"""Search: finished in {vm.search_time_elapsed_ms:,} ms.""" '''.strip() new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_multiline(): code = ''' "Flask Documentation ({})".format( version ) '''.strip() expected = '''f"""Flask Documentation ({version})"""''' new, meta = transform_chunk(code) assert meta['changed'] assert new == expected
def test_unpacking_no_change(): code = '''e.description = "KeyError: '{}'".format(*e.args)''' new, meta = transform_chunk(code) assert not meta['changed'] assert new == code