Vonage / vonage-python-sdk

Vonage Server SDK for Python. API support for Voice, SMS, WhatsApp, Verify (2FA), Video Meetings and more.
https://developer.vonage.com
Apache License 2.0
185 stars 112 forks source link

Avoid breaking changes in Pydantic 2.0 #266

Closed willcroft closed 1 year ago

willcroft commented 1 year ago

Pins the pydantic package dependency to the last known release of v1. Breaking changes in Pydantic v2.0 (released June 30) means that the Vonage SDK now throws exceptions due to use of removed functionality.

For example, for Vonage SDK version 3.2.x:

Traceback (most recent call last):
  File "/path/to/code.py", line 1, in <module>
    import vonage
  File "/path/to/env/lib/python3.9/site-packages/vonage/__init__.py", line 2, in <module>
    from .ncco_builder.ncco import *
  File "/path/to/env/lib/python3.9/site-packages/vonage/ncco_builder/__init__.py", line 1, in <module>
    from .ncco import *
  File "/path/to/env/lib/python3.9/site-packages/vonage/ncco_builder/ncco.py", line 5, in <module>
    from .connect_endpoints import ConnectEndpoints
  File "/path/to/env/lib/python3.9/site-packages/vonage/ncco_builder/connect_endpoints.py", line 6, in <module>
    class ConnectEndpoints:
  File "/path/to/env/lib/python3.9/site-packages/vonage/ncco_builder/connect_endpoints.py", line 10, in ConnectEndpoints
    class PhoneEndpoint(Endpoint):
  File "/path/to/env/lib/python3.9/site-packages/vonage/ncco_builder/connect_endpoints.py", line 11, in PhoneEndpoint
    type = Field('phone', const=True)
  File "/path/to/env/lib/python3.9/site-packages/pydantic/fields.py", line 675, in Field
    raise PydanticUserError('`const` is removed, use `Literal` instead', code='removed-kwargs')
pydantic.errors.PydanticUserError: `const` is removed, use `Literal` instead

Or for Vonage SDK version 3.7.x (the latest release at the time of this PR):

Traceback (most recent call last):
  File "/path/to/code.py", line 1, in <module>
    import vonage
  File "/path/to/env/lib/python3.9/site-packages/vonage/__init__.py", line 1, in <module>
    from .client import *
  File "/path/to/env/lib/python3.9/site-packages/vonage/client.py", line 19, in <module>
    from .verify2 import Verify2
  File "/path/to/env/lib/python3.9/site-packages/vonage/verify2.py", line 17, in <module>
    class Verify2:
  File "/path/to/env/lib/python3.9/site-packages/vonage/verify2.py", line 67, in Verify2
    class VerifyRequest(BaseModel):
  File "/path/to/env/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py", line 171, in __new__
    set_model_fields(cls, bases, config_wrapper, types_namespace)
  File "/path/to/env/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py", line 361, in set_model_fields
    fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
  File "/path/to/env/lib/python3.9/site-packages/pydantic/_internal/_fields.py", line 96, in collect_model_fields
    type_hints = get_cls_type_hints_lenient(cls, types_namespace)
  File "/path/to/env/lib/python3.9/site-packages/pydantic/_internal/_typing_extra.py", line 212, in get_cls_type_hints_lenient
    hints[name] = eval_type_lenient(value, globalns, localns)
  File "/path/to/env/lib/python3.9/site-packages/pydantic/_internal/_typing_extra.py", line 224, in eval_type_lenient
    return typing._eval_type(value, globalns, localns)  # type: ignore
  File "/path/to/env/lib/python3.9/typing.py", line 292, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/path/to/env/lib/python3.9/typing.py", line 554, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
TypeError: constr() got an unexpected keyword argument 'regex'

I suggest the same is done for all other dependencies, though I am not in a position to ascertain the version at which they should be pinned to maintain compatibility with SDK functionality.

mehedikhan72 commented 1 year ago

Hi, @willcroft . I've been trying to setup vonage for SMS integration in my django app but I'm getting the 2nd error you've mentioned (TypeError: constr() got an unexpected keyword argument 'regex'). I currently have vonage v3.7.0 and pydantic v2.0. I tried going back to pydantic v1.9.1 and running the server and I got another error(something related to 'to_upper' function not working correctly). So it seems to be a compatibility issue.

I'm not sure how to really fix this. What should I do?

mehedikhan72 commented 1 year ago

Update: I tried with pydantic v1.10.10 and the error seemed to have gone away and the SMS worked too. Is this a solid solution? considering it's a very older version. Thanks in advance.

willcroft commented 1 year ago

Considering it's a very older version

The 2.0 release is an entirely new and diverges significantly from the 1.x series. So 1.10.10 is actually also a recent release (for those who are not ready to update to 2.x), and was also released on June 30.

mehedikhan72 commented 1 year ago

@willcroft thanks. Also is this good enough for production or should I keep an eye for possible updates in pydantic?

willcroft commented 1 year ago

Also is this good enough for production

I neither work for Vonage nor am I a contributor/maintainer for Pydantic, so I can't speak to compatibility. The Pydantic release is a regular, supported release of the 1.x branch. Nothing different there.

mehedikhan72 commented 1 year ago

Alright, Thanks!

maxkahan commented 1 year ago

Hi, thanks for raising this! I'll merge this in and release a new patch version of the SDK to get rid of this problem.

willcroft commented 1 year ago

Fantastic, thanks!

maxkahan commented 1 year ago

@willcroft I chose to pin to the v1 version but allow minor updates for security reasons. No breaking changes though! You can get the new version from PyPI now ๐Ÿ™‚

willcroft commented 1 year ago

Awesome, this'll be added to our next release then! Thanks for the speedy turnaround ๐Ÿ‘

maxkahan commented 1 year ago

No problem, if you have any other issues feel free to raise an issue or PR ๐Ÿ™‚