Skip to content

Commit

Permalink
fix: revert pydantic changes for HA compat (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jun 9, 2024
1 parent b5616d7 commit c7770c1
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 16 deletions.
25 changes: 25 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[run]
source = src/uiprotect

omit =
site

[report]
# Regexes for lines to exclude from consideration
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

# Don't complain about missing debug-only code:
def __repr__

# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError
raise exceptions.NotSupportedError

# TYPE_CHECKING and @overload blocks are never executed during pytest run
# except ImportError: are never executed as well
if TYPE_CHECKING:
@overload
except ImportError:
5 changes: 5 additions & 0 deletions src/uiprotect/cli/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from uiprotect.exceptions import BadRequest, NvrError, StreamError
from uiprotect.utils import run_async

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError # type: ignore[assignment]

T = TypeVar("T")

OPTION_FORCE = typer.Option(False, "-f", "--force", help="Skip confirmation prompt")
Expand Down
23 changes: 19 additions & 4 deletions src/uiprotect/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
from typing import TYPE_CHECKING, Any, ClassVar, TypeVar
from uuid import UUID

from pydantic.v1 import BaseModel
from pydantic.v1.fields import SHAPE_DICT, SHAPE_LIST, PrivateAttr

from uiprotect.data.types import (
ModelType,
PercentFloat,
Expand All @@ -37,17 +34,35 @@
to_snake_case,
)

try:
from pydantic.v1 import BaseModel
from pydantic.v1.fields import SHAPE_DICT, SHAPE_LIST, PrivateAttr
except ImportError:
from pydantic import BaseModel # type: ignore[assignment, no-redef]
from pydantic.fields import ( # type: ignore[attr-defined, assignment, no-redef]
SHAPE_DICT,
SHAPE_LIST,
PrivateAttr,
)

if TYPE_CHECKING:
from asyncio.events import TimerHandle

from pydantic.v1.typing import DictStrAny, SetStr
from typing_extensions import Self # requires Python 3.11+

from uiprotect.api import ProtectApiClient
from uiprotect.data.devices import Bridge
from uiprotect.data.nvr import Event
from uiprotect.data.user import User

try:
from pydantic.v1.typing import DictStrAny, SetStr
except ImportError:
from pydantic.typing import ( # type: ignore[assignment, no-redef]
DictStrAny,
SetStr,
)


ProtectObject = TypeVar("ProtectObject", bound="ProtectBaseObject")
RECENT_EVENT_MAX = timedelta(seconds=30)
Expand Down
6 changes: 5 additions & 1 deletion src/uiprotect/data/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from uuid import UUID

from aiohttp.client_exceptions import ServerDisconnectedError
from pydantic.v1 import PrivateAttr, ValidationError

try:
from pydantic.v1 import PrivateAttr, ValidationError
except ImportError:
from pydantic import PrivateAttr, ValidationError # type: ignore[assignment]

from uiprotect.data.base import (
RECENT_EVENT_MAX,
Expand Down
5 changes: 4 additions & 1 deletion src/uiprotect/data/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, cast

from pydantic.v1.fields import PrivateAttr
try:
from pydantic.v1.fields import PrivateAttr
except ImportError:
from pydantic.fields import PrivateAttr

from uiprotect.data.base import (
EVENT_PING_INTERVAL,
Expand Down
11 changes: 9 additions & 2 deletions src/uiprotect/data/nvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import aiofiles
import orjson
from aiofiles import os as aos
from pydantic.v1.fields import PrivateAttr

from uiprotect.data.base import (
ProtectBaseObject,
Expand Down Expand Up @@ -58,8 +57,16 @@
from uiprotect.exceptions import BadRequest, NotAuthorized
from uiprotect.utils import RELEASE_CACHE, process_datetime

try:
from pydantic.v1.fields import PrivateAttr
except ImportError:
from pydantic.fields import PrivateAttr

if TYPE_CHECKING:
from pydantic.v1.typing import SetStr
try:
from pydantic.v1.typing import SetStr
except ImportError:
from pydantic.typing import SetStr # type: ignore[assignment, no-redef]


_LOGGER = logging.getLogger(__name__)
Expand Down
15 changes: 12 additions & 3 deletions src/uiprotect/data/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
from typing import Any, Literal, Optional, TypeVar, Union

from packaging.version import Version as BaseVersion
from pydantic.v1 import BaseModel, ConstrainedInt
from pydantic.v1.color import Color as BaseColor
from pydantic.v1.types import ConstrainedFloat, ConstrainedStr

try:
from pydantic.v1 import BaseModel, ConstrainedInt
from pydantic.v1.color import Color as BaseColor
from pydantic.v1.types import ConstrainedFloat, ConstrainedStr
except ImportError:
from pydantic import BaseModel, ConstrainedInt # type: ignore[assignment, no-redef]
from pydantic.color import Color as BaseColor # type: ignore[assignment, no-redef]
from pydantic.types import ( # type: ignore[assignment, no-redef]
ConstrainedFloat,
ConstrainedStr,
)

KT = TypeVar("KT")
VT = TypeVar("VT")
Expand Down
5 changes: 4 additions & 1 deletion src/uiprotect/data/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from functools import cache
from typing import Any

from pydantic.v1.fields import PrivateAttr
try:
from pydantic.v1.fields import PrivateAttr
except ImportError:
from pydantic.fields import PrivateAttr

from uiprotect.data.base import ProtectBaseObject, ProtectModel, ProtectModelWithId
from uiprotect.data.types import ModelType, PermissionNode
Expand Down
14 changes: 12 additions & 2 deletions src/uiprotect/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

import jwt
from aiohttp import ClientResponse
from pydantic.v1.fields import SHAPE_DICT, SHAPE_LIST, SHAPE_SET, ModelField
from pydantic.v1.utils import to_camel

from uiprotect.data.types import (
Color,
Expand All @@ -40,6 +38,18 @@
)
from uiprotect.exceptions import NvrError

try:
from pydantic.v1.fields import SHAPE_DICT, SHAPE_LIST, SHAPE_SET, ModelField
from pydantic.v1.utils import to_camel
except ImportError:
from pydantic.fields import ( # type: ignore[assignment, no-redef, attr-defined]
SHAPE_DICT,
SHAPE_LIST,
SHAPE_SET,
ModelField,
)
from pydantic.utils import to_camel # type: ignore[assignment, no-redef]

if TYPE_CHECKING:
from uiprotect.api import ProtectApiClient
from uiprotect.data import CoordType, Event
Expand Down
5 changes: 5 additions & 0 deletions tests/data/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
from uiprotect.exceptions import BadRequest
from uiprotect.utils import to_js_time

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError


@pytest.mark.skipif(not TEST_CAMERA_EXISTS, reason="Missing testdata")
@pytest.mark.parametrize("status", [True, False])
Expand Down
5 changes: 5 additions & 0 deletions tests/data/test_chime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
from uiprotect.data import RingSetting
from uiprotect.exceptions import BadRequest

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError

if TYPE_CHECKING:
from uiprotect.data import Camera, Chime

Expand Down
5 changes: 5 additions & 0 deletions tests/data/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
from uiprotect.exceptions import BadRequest
from uiprotect.utils import to_ms

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError

if TYPE_CHECKING:
from uiprotect.data import Camera, Light

Expand Down
5 changes: 5 additions & 0 deletions tests/data/test_nvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from uiprotect.exceptions import BadRequest
from uiprotect.utils import to_ms

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError


@pytest.mark.parametrize("status", [True, False])
@pytest.mark.asyncio()
Expand Down
5 changes: 5 additions & 0 deletions tests/data/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
from uiprotect.data.types import MountType
from uiprotect.exceptions import BadRequest

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError

if TYPE_CHECKING:
from uiprotect.data import Camera, Light, Sensor

Expand Down
9 changes: 7 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
from uuid import UUID

import pytest
from pydantic.v1.config import BaseConfig
from pydantic.v1.fields import ModelField

from uiprotect.utils import convert_unifi_data, dict_diff, to_snake_case

try:
from pydantic.v1.config import BaseConfig
from pydantic.v1.fields import ModelField
except ImportError:
from pydantic.config import BaseConfig
from pydantic.fields import ModelField # type: ignore[attr-defined]


def test_dict_diff_equal():
assert dict_diff({}, {}) == {}
Expand Down

0 comments on commit c7770c1

Please sign in to comment.