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

Handling of schemas whose fields using reserved keywords for Python variables #812

Open
gurmeetsaran opened this issue Dec 5, 2024 · 1 comment

Comments

@gurmeetsaran
Copy link

Describe the bug
I have avro schema which contains field name like yield and fake. The generated model contains yield and fake as a field. having fake field in the model create issue when using fake method and yield causes issue because its a reserved keyword in python.

fake field resulted in

AssertionError: Invalid default type <class 'method'> for field "fake". Default should be <class 'str'>

To Reproduce

import json

from dataclasses_avroschema import ModelGenerator, ModelType

model_generator = ModelGenerator()
schema = """
	{
		"type": "record",
		"name": "Test",
		"namespace": "com.foo",
		"fields": [{
			"name": "yield",
			"type": "double"
		}, {
			"name": "fake",
			"type": {
				"type": "string"
			}
		}]
	}
"""
new_schema = json.loads(schema)
result = model_generator.render(schema=new_schema, model_type=ModelType.AVRODANTIC.value, include_original_schema=True)
print(result)

==========
class Test(AvroBaseModel):
    yield: float
    fake: str

    
    class Meta:
        namespace = "com.foo"
        original_schema = """{"type": "record", "name": "Test", "namespace": "com.foo", "fields": [{"name": "yield", "type": "double"}, {"name": "fake", "type": {"type": "string"}}]}"""

Expected behavior
It should generate valid model perhaps use alias field

@gurmeetsaran
Copy link
Author

Similar issue #628

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant