question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RecursionError: maximum recursion depth exceeded while calling a Python object

See original GitHub issue

Describe the bug Trying to create code from an OpenAPI spec fails with a RecursionError.

To Reproduce

Example schema:

https://developer.here.com/documentation/routing-api/swagger/v8.yaml

Used commandline:

$ datamodel-codegen --url https://developer.here.com/documentation/routing-api/swagger/v8.yaml --input-file-type openapi --output result_model.py

Expected behavior No RecursionError

Version:

  • OS: Ubuntu 22.04
  • Python version: 3.8
  • datamodel-code-generator version: 0.13.0

Additional context

Not sure if the OpenAPI is defect or this package.

Traceback (most recent call last):
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/__main__.py", line 541, in main
    generate(
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/__init__.py", line 367, in generate
    results = parser.parse()
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/base.py", line 449, in parse
    self.parse_raw()
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/openapi.py", line 393, in parse_raw
    self.parse_raw_obj(
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1149, in parse_raw_obj
    self.parse_obj(name, JsonSchemaObject.parse_obj(raw), path)
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1164, in parse_obj
    self.parse_object(name, obj, path)
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 599, in parse_object
    fields=self.parse_object_fields(
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 545, in parse_object_fields
    field_type = self.parse_item(modular_name, field, [*path, field_name])
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 645, in parse_item
    return self.parse_array_fields(
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 753, in parse_array_fields
    data_types=self.parse_list_item(
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 720, in parse_list_item
    return [
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 721, in <listcomp>
    self.parse_item(
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 639, in parse_item
    return self.get_ref_data_type(item.ref)
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/parser/jsonschema.py", line 436, in get_ref_data_type
    return self.data_type(reference=reference)
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/types.py", line 209, in __init__
    super().__init__(**values)
  File "/home/eifinger/.cache/pypoetry/virtualenvs/here-routing-cgjRCcGK-py3.8/lib/python3.8/site-packages/datamodel_code_generator/reference.py", line 45, in __init__
    super().__init__(**values)
  File "pydantic/main.py", line 339, in pydantic.main.BaseModel.__init__
  File "pydantic/main.py", line 1038, in pydantic.main.validate_model
  File "pydantic/fields.py", line 857, in pydantic.fields.ModelField.validate
  File "pydantic/fields.py", line 1074, in pydantic.fields.ModelField._validate_singleton
  File "pydantic/fields.py", line 1121, in pydantic.fields.ModelField._apply_validators
  File "pydantic/class_validators.py", line 313, in pydantic.class_validators._generic_validator_basic.lambda12
  File "pydantic/main.py", line 679, in pydantic.main.BaseModel.validate
  File "pydantic/main.py", line 605, in pydantic.main.BaseModel._copy_and_set_values
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.8/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 205, in _deepcopy_list
    append(deepcopy(a, memo))
  File "/usr/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
...
RecursionError: maximum recursion depth exceeded while calling a Python object

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
eyalmor-entcommented, Aug 10, 2022

Also encountered this problem.

It seems like pydantic 1.9.1 added deepcopy to model validation:

Config.copy_on_model_validation does a deep copy and not a shallow one, https://github.com/samuelcolvin/pydantic/issues/3641 by @PrettyWood

(from the release notes)

This isn’t apparent in the datamodel-code-generator project because it’s currently locked on Pydantic 1.9.0.

The fix seems simple enough, hopefully I can a get a PR in today.

1reaction
eifingercommented, Jul 20, 2022

@pmbrull thank you doing that, pinning markupsafe==2.0.1 and following the fix from this link helped: https://github.com/koxudaxi/fastapi-code-generator/issues/232#issuecomment-1059146715

Read more comments on GitHub >

github_iconTop Results From Across the Web

maximum recursion depth exceeded while calling a Python ...
This means that each call to your recursive function will create a function call stack and because there is a limit of stack...
Read more >
Python RecursionError: Maximum Recursion Depth ...
A Python RecursionError exception is raised when the execution of your program exceeds the recursion limit of the Python interpreter.
Read more >
Python maximum recursion depth exceeded in comparison
The “maximum recursion depth exceeded in comparison” error is raised when you try to execute a function that exceeds Python's built in recursion ......
Read more >
maximum recursion depth exceeded while calling a Python ...
When the interpreter detects that the maximum depth for recursion has reached, it throws the recursionerror ...
Read more >
Python: Maximum Recursion Depth Exceeded [How to Fix It]
The maximum recursion depth in Python is 1000. To check it, call sys.getrecursionlimit() function. To change it, call sys.setrecursionlimit().
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found