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

Error with some specific field types #21

Closed
abnerjacobsen opened this issue Apr 19, 2023 · 4 comments
Closed

Error with some specific field types #21

abnerjacobsen opened this issue Apr 19, 2023 · 4 comments

Comments

@abnerjacobsen
Copy link

Hi,

With an model like this:

class PostContractBodySchema(BaseModel):
    config_id: str = Field(
        ...,
        max_length=25,
        title="Config ID",
        description="The ID of the config used by this notification channel.",
        example="ckasokq6g0000yvxuigfa2agy",
    )
    channel: Literal["whatsapp"] = Field(
        ...,
        # max_length=25,
        title="Channel",
        description="Channel used by this notification.",
        example="whatsapp",
    )
    channel_category: Literal["DIRECT_MESSAGE"] = Field(
        ...,
        title="Category",
        description="Communication channel category.",
        example="DIRECT_MESSAGE",
    )
    language: Literal["pt_BR", "en"] = Field(
        ...,
        # max_length=5,
        title="Language",
        description="language of this channel notification.",
        example="pt_BR",
    )
    template_type: Literal["TRANSACTIONAL", "MARKETING", "OTP",] = Field(
        ...,
        title="Template Type",
        description="Your template should fall under one of these categories. This will be used by some providers to approve your template.",
        example="MARKETING",
    )
    tags: str = Field(
        ...,
        max_length=64,
        title="Tags",
        description="Tags of this template.",
        example="PROPOSAL SEND",
    )
    config: Dict[str, Any] = Field(
        ...,
        title="Configuration",
        description="""Notification extra configuration, like buttons and images in Whatsapp templates.""",
        example={
            "header": "Importante",
            "footer": "Clique no botão para continuar a conversa.",
            "buttons": [{"type": "QUICK_REPLY", "text": "Sim"}],
        },
    )

I got this exceptions:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.9/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 68, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1501, in uvloop.loop.Loop.run_until_complete
  File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 76, in serve
    config.load()
  File "/usr/local/lib/python3.9/site-packages/uvicorn/config.py", line 448, in load
    self.loaded_app = import_from_string(self.app)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/app/./app/main.py", line 34, in <module>
    from app.api.api import api_router
  File "/app/./app/api/api.py", line 11, in <module>
    from app.api.api_v1.endpoints.notification_channel.endpoint import notification_channel_router
  File "/app/./app/api/api_v1/endpoints/notification_channel/endpoint.py", line 343, in <module>
    payload: PostContractBodySchema = FormDepends(PostContractBodySchema),
  File "/usr/local/lib/python3.9/site-packages/pyfa_converter/depends.py", line 42, in __new__
    return super().generate(model=model_type, _type=cls._TYPE)
  File "/usr/local/lib/python3.9/site-packages/pyfa_converter/depends.py", line 26, in generate
    obj = PydanticConverter.reformat_model_signature(model_cls=model, _type=_type)
  File "/usr/local/lib/python3.9/site-packages/pyfa_converter/main.py", line 105, in reformat_model_signature
    new_params = PydanticConverterUtils.override_signature_parameters(
  File "/usr/local/lib/python3.9/site-packages/pyfa_converter/main.py", line 50, in override_signature_parameters
    return [
  File "/usr/local/lib/python3.9/site-packages/pyfa_converter/main.py", line 54, in <listcomp>
    default=param_maker(field),
  File "/usr/local/lib/python3.9/site-packages/pyfa_converter/main.py", line 94, in make_form_parameter
    if issubclass(field.type_, BaseModel):
  File "/usr/local/lib/python3.9/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

If I remove the fields of type Literal and Dict[str, Any] pyfa works fine.

Any idea on how to solve this problem ?

Thanks

@dotX12
Copy link
Owner

dotX12 commented Apr 23, 2023

@abnerjacobsen, hello, I have checked, this problem exists. There will be a fix for Literal in the next two days. But no don't expect a fix from Dict[..., ...]

@abnerjacobsen
Copy link
Author

@dotX12 Thanks, I will be wait for the fix for Literal type.

I understand the difficulty of handling a dict field in the openapi/swagger web interface, for now I am uploading a JSON file to work around the problem.
I tested some options proposed in this Stackoverflow discussion link and in openapi the dict field is displayed correctly, as if it were a memo field, but when Pydantic validates it, an error occurs stating that it is not the expected type.

@dotX12 dotX12 mentioned this issue Apr 24, 2023
@dotX12 dotX12 closed this as completed in 124f1d2 Apr 24, 2023
@dotX12 dotX12 reopened this Apr 24, 2023
@dotX12
Copy link
Owner

dotX12 commented Apr 24, 2023

@dotX12
Copy link
Owner

dotX12 commented Apr 28, 2023

@abnerjacobsen, hi!
Have you tried, is your problem solved?

@dotX12 dotX12 closed this as completed May 5, 2023
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

2 participants