Skip to content

Syntax Error in generated code #277

Closed

Description

@phiresky
  • cattrs version: current master
  • Python version: 3.10
  • Operating System: Linux

The error

 File ".venv/lib/python3.10/site-packages/cattrs/dispatch.py", line 122, in dispatch                                                
   return handler(typ)                                                                                                                                       
 File ".venv/lib/python3.10/site-packages/cattrs/converters.py", line 829, in gen_structure_mapping                                      
   h = make_mapping_structure_fn(                                                                                                                                         
 File ".venv/lib/python3.10/site-packages/cattrs/gen.py", line 726, in make_mapping_structure_fn
   eval(compile(script, "", "exec"), globs)                                                                                                                                
 File "<string>", line 17                                                                                                                                                 
   raise IterableValidationError('While structuring dict[typing.Union[typing.Literal['ca', 'pro', 'fa', 'car', 'ing_emb'], typing.Literal['wgr', 'cwg', 'fcc', 'wcc']], statistics.FrequenciesInfo]', errors, __cattr_mapping_cl)

    | SyntaxError: invalid syntax. Perhaps you forgot a comma?                                                                                                                 

The offending code is here gen :

image

It seems to generate code that assumes that the repr of the offending type does not include '. ' is not escaped.

Activity

  1. Tinche commented on Jun 20, 2022

    @Tinche
    Member

    Thanks, I'm aware of this (there's another, similar issue), it's on my radar for the next version. Just out of curiosity, if you change the line to

        raise IterableValidatoinError("While structuring {cl!r}", errors, __cattr_mapping_cl)
    

    i.e. single quotes to double quotes, does it fix it?

  2. phiresky commented on Jun 20, 2022

    @phiresky
    Author

    Yes, that does seem to fix it. Though I'm not sure if I would trust all types to not contain a " either.

  3. Tinche commented on Jun 20, 2022

    @Tinche
    Member

    Yeah, the play might be to just escape any single quotes in the repr.

  4. Tinche commented on Sep 18, 2022

    @Tinche
    Member

    @phiresky this should be fixed on main, can you verify before I release the next version?

  5. phiresky commented on Sep 18, 2022

    @phiresky
    Author

    Thanks for fixing it! Sorry, it might take a while before I get back to the code that for me generates this error, so probably you shouldn't wait for me.

  6. phiresky commented on Sep 21, 2022

    @phiresky
    Author

    Seems to work, though I'm getting a .venv/lib/python3.10/site-packages/cattrs/converters.py:541: error: Invalid "type: ignore" comment mypy error on master

  7. Tinche commented on Sep 21, 2022

    @Tinche
    Member

    Confirmed. Will test on 3.7 too before removing the comment.

  8. Tinche commented on Sep 21, 2022

    @Tinche
    Member

    I fixed it by removing the comment, seems to work fine. Give it one more test for me? <3

  9. aarnphm commented on Sep 21, 2022

    @aarnphm
    Contributor

    I'm I thought that mypy also accept # type: ignore syntax 🤔

  10. Tinche commented on Sep 21, 2022

    @Tinche
    Member

    It does. It can be configured to not take it though, which I think is happening (at least for me, I have that option set up).

  11. aarnphm commented on Sep 22, 2022

    @aarnphm
    Contributor

    got it. I think I also configure it to accept the syntax on pyright end. Removing it makes sense then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions