cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

Fix: GenConverter syntax errors with certain types

Open xaviergmail opened this issue 3 years ago • 1 comments

Certain uses of typing.Literal or typing.Annotated cause syntax errors in generated code. (Related: https://github.com/python-attrs/cattrs/issues/277)

In the case of annotated types (from 3.7 typing_extensions), I get the following syntax error:

    eval(compile(script, "", "exec"), globs)
E     File "<string>", line 17
E       raise IterableValidationError('While structuring typing.Dict[typing_extensions.Annotated[str, <class 'housecats.annotations.sanitization.PIIAnnotation'>], vnxpy.jobs.types.leaf.AssigneeRemoval]', errors, __cattr_mapping_cl)
E                                                                                                                     ^
E   SyntaxError: invalid syntax

As mentioned in #277 by @Tinche; we could escape quotes manually but using repr() on the string is much easier (it returns a string literal)

I tried to write some minimal tests for this but was having some unrelated issues with Poetry/tox that I don't have bandwidth to figure out at the moment. I can however confirm that these changes work when modifying cattrs 22.1.0 in my project's site_packages.

xaviergmail avatar Aug 02 '22 02:08 xaviergmail

Thanks for this, I'm kind of stuck implementing converter.copy() atm so don't be discouraged it it takes me a while to come back to this.

Tinche avatar Aug 02 '22 21:08 Tinche

Looks good, can you add a small test or two and a changelog snippet?

Tinche avatar Sep 12 '22 21:09 Tinche

Codecov Report

Merging #291 (9d3db6d) into main (9b9205e) will not change coverage. The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #291   +/-   ##
=======================================
  Coverage   96.96%   96.96%           
=======================================
  Files          16       16           
  Lines        1382     1382           
=======================================
  Hits         1340     1340           
  Misses         42       42           
Impacted Files Coverage Δ
src/cattrs/gen.py 94.18% <ø> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov-commenter avatar Sep 12 '22 21:09 codecov-commenter

Hello, since I want to get this release out and you mentioned you'd prefer not to do tests I wrote one myself.

So I'm merging this in now. Thanks!

Tinche avatar Sep 18 '22 16:09 Tinche

No worries! I have been busier than usual lately. Thank you for wrapping this up. Your work on this project is appreciated!

xaviergmail avatar Sep 19 '22 04:09 xaviergmail