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

forward_refs updates not running in all classes that need it #27

Open
agostof opened this issue Jun 7, 2021 · 4 comments
Open

forward_refs updates not running in all classes that need it #27

agostof opened this issue Jun 7, 2021 · 4 comments

Comments

@agostof
Copy link

agostof commented Jun 7, 2021

When running a simple example client I get the following error:

pydantic.errors.ConfigError: field "status" not yet prepared so type is still a ForwardRef, 
  you might need to call Metadata.update_forward_refs().

It seems that the initialization code in client/__init__.py is not finding all the classes that need the fwd_refs to be updated.
My solution was to run update_forward_refs in all classes (openapi-python-templates/__init__package.mustache)

for model in inspect.getmembers(models, inspect.isclass):
    if model[1].__module__ == "client.models":
        model_class = model[1]
        if isinstance(model_class, BaseModel) or hasattr(model_class, "update_forward_refs"):
            model_class.update_forward_refs()
@colindjk
Copy link

colindjk commented Jul 6, 2021

I tried changing isinstance to issubclass and it worked.

The reason is b/c isinstance will always return false. The model_class isn't an instance of the BaseModel, it's a class itself that subclasses BaseModel.

@agostof
Copy link
Author

agostof commented Jul 6, 2021

That sounds good. To be honest, I did my patch keeping as much of the original code (e.g. isinstance()) as possible. And in this case the assumption was that the "update_forward_refs" property was available to be called which is what I tested for.

@pndiku
Copy link

pndiku commented Jul 14, 2021

@agostof when I try your fix I get an error:

  File "pydantic/main.py", line 832, in pydantic.main.BaseModel.update_forward_refs
  File "pydantic/typing.py", line 382, in pydantic.typing.update_field_forward_refs
  File "pydantic/typing.py", line 62, in pydantic.typing.evaluate_forwardref
  File "/usr/lib64/python3.8/typing.py", line 518, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
NameError: name 'IO' is not defined

@pndiku
Copy link

pndiku commented Nov 18, 2021

If anyone is still looking into using this, then the proper patch is this:

Edit file openapi-python-templates/__init__package.mustache

if model[1].__module__ == "@[email protected]":
    model_class = model[1]
    if isinstance(model_class, BaseModel) or hasattr(model_class, "update_forward_refs"):
       try:
            model_class.update_forward_refs()
        except Exception:
            pass

tonybajan-globality added a commit to JP-Globality/fastapi_client that referenced this issue Nov 15, 2022
The current package-level __init__.py is buggy and doesn't correctly
identify classes to call update_forward_refs() on. We're currently
replacing this file at a later stage, but it would be better to generate
it correctly.

See also the upstream issue: dmontagu#27
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

3 participants