Skip to content

Commit

Permalink
Prepare for Pydantic v2 migration (#232)
Browse files Browse the repository at this point in the history
* Prepare for Pydantic v2 migration

* Fix naming for decoding unit test

* Test against pydantic 1/2 on CI
  • Loading branch information
Orhideous authored Feb 17, 2024
1 parent 8c8c25f commit 42aa885
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
pydantic-version: ["1", "2"]
fail-fast: false

steps:
Expand Down Expand Up @@ -87,6 +88,9 @@ jobs:
- name: Install library
run: poetry install --no-interaction

- name: Install specific pydantic version
run: poetry add pydantic@^${{ matrix.pydantic-version }}

- name: Start Mosquitto
uses: namoshek/mosquitto-github-action@v1
with:
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ roomba-password = "roombapy.entry_points:password"
python = ">=3.10,<4.0"
orjson = ">=3.9.13"
paho-mqtt = ">=1.5.1,<3.0.0"
pydantic = "^2.6.1"
pydantic = ">=1"

[tool.poetry.dev-dependencies]
black = "^24.2"
Expand Down
2 changes: 1 addition & 1 deletion roombapy/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _decode_data(raw_response: bytes) -> Optional[RoombaInfo]:
return None

try:
return RoombaInfo.model_validate_json(data)
return RoombaInfo.parse_raw(data)
except ValidationError:
# Malformed json from robots
return None
Expand Down
12 changes: 10 additions & 2 deletions roombapy/roomba_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from functools import cached_property
from typing import Dict, Optional

from pydantic import BaseModel, Field, computed_field, field_validator
try:
from pydantic.v1 import BaseModel, Field, field_validator
except ImportError:
from pydantic import BaseModel, Field
from pydantic import validator as field_validator


class RoombaInfo(BaseModel):
Expand All @@ -26,11 +30,15 @@ def hostname_validator(cls, value: str) -> str:
raise ValueError(f"unsupported model in hostname: {value}")
return value

@computed_field
@cached_property
def blid(self) -> str:
return self.hostname.split("-")[1]

class Config:
# NOTE: Used to ensure Pydantic v1 backwards compatibility
# See https://github.com/samuelcolvin/pydantic/issues/1241
keep_untouched = (cached_property,)

def __hash__(self) -> int:
return hash(self.mac)

Expand Down
File renamed without changes.

0 comments on commit 42aa885

Please sign in to comment.