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

Update pydantic to 2.x and run automatic codemods #384

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

deejay1
Copy link

@deejay1 deejay1 commented Dec 30, 2024

No description provided.

@maxim-yegorov
Copy link

@DurgNomis-drol @joro75 @CM000n
Can you guys please take a look? Thanks 🙏

@desosav
Copy link

desosav commented Jan 4, 2025

Hi @deejay1 thank you for your efforts :)
Is this finalized?
Just letting you know that I get the following error on homeassistant:

Logger: custom_components.toyota
Source: helpers/update_coordinator.py:379
integration: Toyota Connected Services ([documentation](https://github.com/DurgNomis-drol/ha_toyota), [issues](https://github.com/DurgNomis-drol/ha_toyota/issues))
First occurred: 08:54:22 (2 occurrences)
Last logged: 08:54:42

Unexpected error fetching toyota data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 379, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 280, in _async_update_data
    return await self.update_method()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/toyota/__init__.py", line 77, in async_get_vehicle_data
    vehicles = await asyncio.wait_for(client.get_vehicles(metric=metric_values), 15)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/asyncio/tasks.py", line 507, in wait_for
    return await fut
           ^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/mytoyota/client.py", line 63, in get_vehicles
    vehicles = await self._api.get_vehicles_endpoint()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/mytoyota/api.py", line 81, in get_vehicles_endpoint
    parsed_response = await self._request_and_parse(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        VehiclesResponseModel, "GET", VEHICLE_GUID_ENDPOINT
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/local/lib/python3.13/site-packages/mytoyota/api.py", line 55, in _request_and_parse
    return model(**response)
  File "/usr/local/lib/python3.13/site-packages/pydantic/main.py", line 214, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
  File "/usr/local/lib/python3.13/site-packages/mytoyota/utils/models.py", line 17, in invalid_to_none
    validated_value, errors = field.validate(
                              ^^^^^^^^^^^^^^
AttributeError: 'FieldInfo' object has no attribute 'validate'

@deejay1
Copy link
Author

deejay1 commented Jan 4, 2025

hi, this isn't finished unfortunately, I'll try to do it today

@CM000n
Copy link
Collaborator

CM000n commented Jan 4, 2025

Have you used the pydantic migration Tool for you changes @deejay1?
https://docs.pydantic.dev/latest/migration/#code-transformation-tool

This will probably solve some of the problems of the changeover itself.

@deejay1
Copy link
Author

deejay1 commented Jan 4, 2025

Yes, this was done using the migration tool, unfortunately the root_validator changed in v2 and for now I can't find any info on how to recreate its previous functionality :/

@CM000n
Copy link
Collaborator

CM000n commented Jan 4, 2025

Yes, this was done using the migration tool, unfortunately the root_validator changed in v2 and for now I can't find any info on how to recreate its previous functionality :/

root_validator is now replaced with model_validator https://docs.pydantic.dev/latest/migration/#changes-to-validators

Hope to find the time to have a look at it soon by myself.

@skgsergio
Copy link
Contributor

skgsergio commented Jan 5, 2025

The problem seems to be with this change, you can't validate a field directly as it was done in v1: pydantic/pydantic#7367

I don't know much about pydantic (just the basics) but it feels like this usage is not "standard".

Probably invalid_to_none can be adapted with the code of this specific comment: pydantic/pydantic#7367 (reply in thread)

@PGnl
Copy link

PGnl commented Jan 7, 2025

I am also not a pydantic/code specialist, but seems that fields can still be used (field_validator) but now need to be combined with model_validator which replaces root_validator.
not sure this help
Note that as far as I find they need to combined however field always needs to be called before model.

example I could find was:

`from pydantic import BaseModel, Field, field_validator, model_validator

class MyModel(BaseModel):
field1: int = Field(...)
field2: int = Field(...)

# Veldvalidatie voor individuele velden
@field_validator("field1", "field2")
def validate_non_negative(cls, value):
    if value < 0:
        raise ValueError("Fields must be non-negative")
    return value

# Modelvalidatie voor afhankelijkheden tussen velden
@model_validator(mode="after")
def validate_combined(cls, values):
    if values["field1"] + values["field2"] > 100:
        raise ValueError("Sum of field1 and field2 cannot exceed 100")
    return values`

apologies that the comments are in Dutch as the source is.

If my understanding is wrong, just forget my comment 😉

@cocothemomo
Copy link

Thank you so much for the hard work 🙇‍♂️

@deejay1
Copy link
Author

deejay1 commented Jan 10, 2025

Ok, I created another pull request using the v1 compatibility layer while we try to figure out how to upgrade properly. This should probably get us over the bridge with dependency issues.

@jee67
Copy link

jee67 commented Jan 12, 2025

Hi,

any news? As far as I can see, @deejay1 ‘s solution works. Installed it and running “simple_client_example.py“ returns the desired values from my RAV4.
So when using this pull request the HA integration could be working again too.

BTW: I get errors on python3.13, Python 3.9 works fine.

Ok, I created another pull request using the v1 compatibility layer while we try to figure out how to upgrade properly. This should probably get us over the bridge with dependency issues.

@deejay1
Copy link
Author

deejay1 commented Jan 14, 2025

BTW: I get errors on python3.13, Python 3.9 works fine.

3.13 finally dropped some obsolete libraries, I'll take a look.

EDIT:
@jee67 what errors do you get and on what branch? I can't seem to recreate this with the code on #386

@jee67
Copy link

jee67 commented Jan 14, 2025

BTW: I get errors on python3.13, Python 3.9 works fine.

3.13 finally dropped some obsolete libraries, I'll take a look.

EDIT: @jee67 what errors do you get and on what branch? I can't seem to recreate this with the code on #386

Probably an incomplete install of python3.13. I have to figure out why, seems to be module six. Under 3.9 no problems.

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

Successfully merging this pull request may close these issues.

8 participants