ikamensh / flynt

A tool to automatically convert old string literal formatting to f-strings
MIT License
689 stars 33 forks source link

Use logging instead of global state + verbose #154

Closed akx closed 2 years ago

akx commented 2 years ago

As part of my Secret Grand Plan (not really, I just revealed it) of getting rid of the global state, this switches print()s in non-"frontend" situations to logging calls.

The Verbose logging output may look different from before.

ikamensh commented 2 years ago

Can you please provide a few examples of how user output will look after this change?

akx commented 2 years ago

@ikamensh With flynt --dry-run . in flynt's directory, the output is exactly the same.

Adding --verbose, some examples of differences:

Length limit warnings show the chunk

377c298
< Skipping conversion due to line length limit. Pass -ll 999 to increase it. (999 is an example, as number of characters.)
---
> Skipping conversion of 'Detected multiple Flask applications in module "{}". Use ''"FLASK_APP={}:name" to specify the correct ''one.' . format ( var1 , var2 ) due to line length limit. Pass -ll 999 to increase it. (999 is an example, as number of characters.)
390c311
< Skipping conversion due to line length limit. Pass -ll 999 to increase it. (999 is an example, as number of characters.)
---
> Skipping conversion of "Python %s on %s\nApp: %s [%s]\nInstance: %s" % ( sys . version , sys . platform , app . import_name , app . env , app . instance_path , ) due to line length limit. Pass -ll 999 to increase it. (999 is an example, as number of characters.)

More context for other errors

501,506c422,451
< Failed to parse transformed code 'f"beautiful numbers to follow: {'\\t'.join(lst)}"' given original '"beautiful numbers to follow: {}" . format ( "\t" . join ( lst ) )'
---
> Failed to parse transformed code 'f"beautiful numbers to follow: {'\\t'.join(lst)}"'' given original '"beautiful numbers to follow: {}" . format ( "\t" . join ( lst ) )'
> Traceback (most recent call last):
>   File "flynt/transform/transform.py", line 54, in transform_chunk
>     ast.parse(new_code)
>   File "lib/python3.10/ast.py", line 50, in parse
>     return compile(source, filename, mode, flags,
>   File "<unknown>", line 1
>     f"beautiful numbers to follow: {'\\t'.join(lst)}"
>                                                      ^

Better "not converting" errors

1771,1772c1496,1497
< Exception Default text alignment has changed between percent fmt and fstrings. Proceeding would result in changed code behaviour. during conversion of code '"Setting %20r must be uppercase." % name'
< Exception Skipping %d formatting - fstrings behave differently from % formatting. during conversion of code ''%i' % i'
---
> Not converting code '"Setting %20r must be uppercase." % name': Default text alignment has changed between percent fmt and fstrings. Proceeding would result in changed code behaviour.
> Not converting code ''%i' % i': Skipping %d formatting - fstrings behave differently from % formatting.