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

Prepare for Pydantic v2 migration #232

Merged
merged 3 commits into from Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
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
Orhideous marked this conversation as resolved.
Show resolved Hide resolved

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
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
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
@@ -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.