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

[BUG] from . import AnonymousSchemaX breaks types (?) #2076

Open
2 tasks done
black-snow opened this issue Jul 23, 2024 · 4 comments
Open
2 tasks done

[BUG] from . import AnonymousSchemaX breaks types (?) #2076

black-snow opened this issue Jul 23, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@black-snow
Copy link

Describe the bug.

(ref. asyncapi/cli#1485)

So with --pyDantic, the CLI generates models like:

from . import AnonymousSchema15

class AnonymousSchema13(BaseModel): 
  command: Optional[str] = Field(default=None)
  meta: Optional[AnonymousSchema15.AnonymousSchema15] = Field(default=None)

with

class AnonymousSchema15(BaseModel): 
  whatever: str = Field()

But when I try to use the models I end up with the unhelpful message:

...
/opt/homebrew/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/typing.py:415: in _eval_type
return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
/opt/homebrew/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/typing.py:938: in _evaluate
eval(self.forward_code, globalns, locals_to_pass),
:1: in
???
../.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py:237: in getattr
raise AttributeError(item)
E AttributeError: AnonymousSchema15

The import seems to break the types for pydantic. It works fine when I go with from .AnonymousSchema15 import AnonymousSchema15 and then Optional[AnonymousSchema15].
This seems to be some deep crappy typehinting black magic eff-up.

So the fix might just be to adjust the import + the usage.

Expected behavior

Nested models should work out of the box.

Screenshots

How to Reproduce

  • Have a message with allOf:
      payload:
        allOf:
          - $ref: "#/components/schemas/Base"
          - $ref: "#/components/schemas/SomethingOnTop"
  1. asyncapi generate models python asyncapi.yaml --pyDantic
  2. Try to use the generated model(s)

🥦 Browser

None

👀 Have you checked for similar open issues?

  • I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

None

@black-snow black-snow added the bug Something isn't working label Jul 23, 2024
Copy link
Contributor

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@black-snow
Copy link
Author

black-snow commented Jul 23, 2024

I can strip down the schema to a minimal example if the issue isn't apparent.

My quick workaround is to patch it after code generation:

LC_ALL=C find models/generated -type f -exec sed -i '' -r 's/^from . import (AnonymousSchema[0-9]+)/from .\1 import \1/' {} \; \
	&& LC_ALL=C find models/generated -type f -exec sed -i '' -r 's/Optional\[AnonymousSchema[0-9]+\./Optional[/' {} \;

Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Nov 23, 2024
@black-snow
Copy link
Author

Stale - yes, but still as valid as it's been half a year ago.

@github-actions github-actions bot removed the stale label Nov 26, 2024
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
Development

No branches or pull requests

1 participant