Skip to content

Commit 7601ba2

Browse files
committed
Fix type-hinting in base.py
1 parent b9457c9 commit 7601ba2

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

plexapi/base.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import TYPE_CHECKING, Generic, Iterable, List, Optional, TypeVar, Union
2+
from typing import TYPE_CHECKING, Any, Callable, Generic, Iterable, List, Optional, TypeVar, Union, overload
33
import weakref
44
from functools import cached_property
55
from urllib.parse import parse_qsl, urlencode, urlparse
@@ -12,11 +12,13 @@
1212
if TYPE_CHECKING:
1313
from plexapi.server import PlexServer
1414

15-
PlexObjectT = TypeVar("PlexObjectT", bound='PlexObject')
16-
MediaContainerT = TypeVar("MediaContainerT", bound="MediaContainer")
15+
_T = TypeVar('_T')
1716

18-
USER_DONT_RELOAD_FOR_KEYS = set()
19-
_DONT_RELOAD_FOR_KEYS = {'key', 'sourceURI'}
17+
PlexObjectT = TypeVar('PlexObjectT', bound='PlexObject')
18+
MediaContainerT = TypeVar('MediaContainerT', bound='MediaContainer')
19+
20+
USER_DONT_RELOAD_FOR_KEYS: set[str] = set()
21+
_DONT_RELOAD_FOR_KEYS: set[str] = {'key', 'sourceURI'}
2022
OPERATORS = {
2123
'exact': lambda v, q: v == q,
2224
'iexact': lambda v, q: v.lower() == q.lower(),
@@ -38,15 +40,15 @@
3840
}
3941

4042

41-
class cached_data_property(cached_property):
43+
class cached_data_property(cached_property, Generic[_T]):
4244
"""Caching for PlexObject data properties.
4345
4446
This decorator creates properties that cache their values with
4547
automatic invalidation on data changes.
4648
"""
4749

48-
def __new__(cls, *args, **kwargs) -> "cached_data_property":
49-
return super().__new__(cls)
50+
def __init__(self, func: Callable[[Any], _T]) -> None:
51+
super().__init__(func)
5052

5153
def __set_name__(self, owner, name):
5254
"""Register the annotated property in the parent class's _cached_data_properties set."""
@@ -55,6 +57,17 @@ def __set_name__(self, owner, name):
5557
owner._cached_data_properties = set()
5658
owner._cached_data_properties.add(name)
5759

60+
@overload
61+
def __get__(self, instance: None, owner: type | None = None) -> "cached_data_property[_T]":
62+
...
63+
64+
@overload
65+
def __get__(self, instance: object, owner: type | None = None) -> _T:
66+
...
67+
68+
def __get__(self, instance: object | None, owner: type | None = None) -> "cached_data_property[_T] | _T":
69+
return super().__get__(instance, owner)
70+
5871

5972
class PlexObjectMeta(type):
6073
"""Metaclass for PlexObject to handle cached_data_properties."""
@@ -76,6 +89,11 @@ def __new__(mcs, name, bases, attrs):
7689
return super().__new__(mcs, name, bases, attrs)
7790

7891

92+
class MediaContainerMeta(PlexObjectMeta, type(List)):
93+
"""Metaclass for MediaContainer that combines PlexObjectMeta with Generic/List metaclass."""
94+
pass
95+
96+
7997
class PlexObject(metaclass=PlexObjectMeta):
8098
""" Base class for all Plex objects.
8199
@@ -1144,6 +1162,7 @@ class MediaContainer(
11441162
Generic[PlexObjectT],
11451163
List[PlexObjectT],
11461164
PlexObject,
1165+
metaclass=PlexObjectMeta,
11471166
):
11481167
""" Represents a single MediaContainer.
11491168

0 commit comments

Comments
 (0)