Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imports raise SyntaxError with FieldName as originalCase #646

Closed
fteule opened this issue Jan 26, 2022 · 6 comments · Fixed by #647 or #653
Closed

Imports raise SyntaxError with FieldName as originalCase #646

fteule opened this issue Jan 26, 2022 · 6 comments · Fixed by #647 or #653
Labels
bug Something isn't working

Comments

@fteule
Copy link

fteule commented Jan 26, 2022

Hi,
I some cases I detected that generated bindings could be invalid when I set in the config FieldName to originalCase.
The generation ends successfully but when I import the module an exception is raised:

In [1]: import SampleBindings.my_schema
Traceback (most recent call last):

  File "/home/user/xsdata/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3457, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-1-c0390bf7dfdc>", line 1, in <module>
    import SampleBindings.my_schema

  File "/home/user/SampleBindings/__init__.py", line 1, in <module>
    from SampleBindings.my_schema import (

  File "/home/user/SampleBindings/my_schema.py", line 12
    @value: Optional[float] = field(
          ^
SyntaxError: invalid syntax

If I set in configuration FieldName to pascalCase, the field is correctly generated as "Value".

I prepared a sample package that reproduce problem:
Sample.tar.gz

I tried to change safePrefix value but it doesn't change this behavior.

Thank you for this library

@tefra tefra added the bug Something isn't working label Jan 26, 2022
@tefra
Copy link
Owner

tefra commented Jan 26, 2022

I can confirm the issue @fteule thanks for reporting!

@tefra
Copy link
Owner

tefra commented Jan 26, 2022

Hey @fteule give the pr #647 a try, I have to check a few things but it should also resolve your issue

@fteule
Copy link
Author

fteule commented Jan 27, 2022

Hi,

Thanks for this correction but I encounter an other mistake with it on my data model.
I prepared a new package :
Sample2.tar.gz

For information my original data package is the same that in #643
We are clearly interested by recursive mode :)

With the correct Sample2 example and below the error:
TypeError: typing.Optional requires a single type. Got Field(name=None,type=None,default=None,default_factory=<dataclasses._MISSING_TYPE object at 0x7f9eb2.

@tefra
Copy link
Owner

tefra commented Jan 27, 2022

Please don't use the same convention for both class and field names, in the generated models we have a field with the same name and type as another class.

Its the cause of the issue and it's pretty impossible to solve, the genrator would have to cross check all field names with all class names and start appending suffixes and thus loosing the whole point of the originalCase convention.

class unit(Enum):
    M = "m"
    KG = "kg"
    VALUE = "%"
    NA = "NA"


@dataclass
class myComplexType2:
    ....
    unit: Optional[unit] = field(
   ....

@tefra
Copy link
Owner

tefra commented Jan 27, 2022

The fix for the initial issue is on master and will be on the next release!

@fteule
Copy link
Author

fteule commented Jan 28, 2022

perfect, thanks for all !

tefra added a commit that referenced this issue Jan 29, 2022
tefra added a commit that referenced this issue Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants