fastly / fastly-py

A Fastly API client for Python
https://pypi.org/project/fastly/
MIT License
77 stars 60 forks source link

EnabledProductsApi TypeError #100

Open findmyname666 opened 6 months ago

findmyname666 commented 6 months ago

Version

Lib version: 5.2.0.

What happened

I tried to use Enable Product API but it is failing on type error when using multiple methods on instance class of EnabledProductsApi:

    api_response = api_instance.get_enabled_product(**options)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/api/enabled_products_api.py", line 471, in get_enabled_product
    return self.get_enabled_product_endpoint.call_with_http_info(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/api_client.py", line 872, in call_with_http_info
    return self.api_client.call_api(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/api_client.py", line 427, in call_api
    return self.__call_api(resource_path, method,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/api_client.py", line 236, in __call_api
    return_data = self.deserialize(
                  ^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/api_client.py", line 341, in deserialize
    deserialized_data = validate_and_convert_types(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 1569, in validate_and_convert_types
    converted_instance = attempt_convert_item(
                         ^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 1453, in attempt_convert_item
    return deserialize_model(input_value, valid_class,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 1373, in deserialize_model
    return model_class._new_from_openapi_data(**kw_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 45, in wrapped_init
    return fn(_self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 369, in _new_from_openapi_data
    return cls._from_openapi_data(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 45, in wrapped_init
    return fn(_self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model/enabled_product_response.py", line 186, in _from_openapi_data
    setattr(self, var_name, var_value)
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 184, in __setattr__
    self[attr] = value
    ~~~~^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 509, in __setitem__
    self.set_attribute(name, value)
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 156, in set_attribute
    value = validate_and_convert_types(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 1569, in validate_and_convert_types
    converted_instance = attempt_convert_item(
                         ^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 1453, in attempt_convert_item
    return deserialize_model(input_value, valid_class,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 1373, in deserialize_model
    return model_class._new_from_openapi_data(**kw_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 45, in wrapped_init
    return fn(_self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/q9k17i98pxvzxxqk7mazb8gnbcpwffj9-python3-3.11.8-env/lib/python3.11/site-packages/fastly/model_utils.py", line 369, in _new_from_openapi_data
    return cls._from_openapi_data(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: EnabledProductResponseLinks._from_openapi_data() got multiple values for argument '_self'

The above error is raised also for python example code from the Fastly documentation.

kpfleming commented 2 months ago

Please try to reproduce this with version 5.7.0 or later; if it's still an issue we'll try to get it resolved.

NeevCohen commented 1 month ago

Hey @kpfleming, I can confirm this issue still exists. I looked into it a bit and the problem is the way wrapped_init changes the response json keys into python keys.

The enabled-product APIs return an object with a key named "self". In change_keys_js_to_python "self" is converted to "_self". This makes it so that fn is called with 2 _self arguments.

This is an example response from the API as per the documentation

{
  "product": {
    "id": "product-id",
    "object": "product"
  },
  "service": {
    "id": "4nWURjwmQjbMB5OefSM75i",
    "object": "service"
  },
  "_links": {
    "self": "https://api.fastly.com/enabled-products/v1/product-id/services/4nWURjwmQjbMB5OefSM75i",
    "service": "https://api.fastly.com/service/4nWURjwmQjbMB5OefSM75i"
  }
}

The "self" key in the _"links" object is turned into "_self", which is then forwarded to fn like so when deserializing it into the EnabledProductResponseLinks -

return fn(_self, *args, **kwargs)  # kwargs contains _self='...'

which causes the _self argument to be passed into fn multiple times.

I hope explained the issue clearly 😄

I don't mind opening a PR to fix this. My first thought was to rename the _self variable to _self_ in order to overcome the name collision. WDYT?

kpfleming commented 1 month ago

Thanks for finding the issue! We can't accept a PR directly against this repo as the code here is produced by a generator (in a private repository), but we can take these details and produce a fix for the generator.

NeevCohen commented 1 month ago

@kpfleming Happy to help!