abarker / strip-hints

A command-line script and importable function which strips type hints from Python programs.
MIT License
43 stars 7 forks source link

Many trailing spaces are added after : even with to_empty=True #10

Closed Ark-kun closed 3 years ago

Ark-kun commented 4 years ago

When the function has multiline return type annotation, the stripped version has multiple spaces after the : symbol (the original function has none).

strip_string_to_string(to_empty=True, code_string='''
def foo(
  param1: int,
  param2: str = "zzz",
) -> NamedTuple('Outputs', [
  ('out1', str),
  ('out2', int),
]):
  pass
''')
'\ndef foo(\n  param1 ,\n  param2  = "zzz",\n):   \n   \n   \n\n  pass\n'`
abarker commented 4 years ago

It's supposed to do that in order to preserve line numbers in error messages. Using to_empty no longer guarantees that the column numbers are preserved, but line numbers are still supposed to be. The program purposely excludes stripping NL tokens (non-logical newlines) to achieve this.

Does the extra space cause problems? The code is still syntactically valid.

I added an option strip_nl to strip NL tokens, too, but using that option no longer preserves line numbers. I pushed it to GitHub but it isn't yet on PyPI and isn't documented. I also fixed a limitation of annotated assignment stripping when they contained NL characters inside type annotations which were then stripped. The no_equal_move option was added to disable that moving of the assignment line to fix the problem.

abarker commented 4 years ago

Docs are updated, and the new version has been pushed to PyPI.

Ark-kun commented 3 years ago

Does the extra space cause problems? The code is still syntactically valid.

The problem for me was that this mangles YAML encoding.

YAML has some very nice-looking string encoding formats that preserve line endings. But the serializers switch to non-nice JSON-like string formats when there are any trailing whitespace characters.

https://github.com/Ark-kun/pipeline_components/blob/f0667a323681f07f6be3717cd200e2f20c6d26c5/components/pandas/Transform_DataFrame/in_CSV_format/component.yaml#L40