Skip to content

Commit 720023c

Browse files
committed
artresizer: adjust code & typings to satisfy mypy
Notably, this replaces the `Shareable` metaclass by a different implementation of a descriptor: We don't really need to modify class creation here, because the singleton is only available via the `shared` method, not via the constructor. Additionally, it appears that mypy can understand the new code.
1 parent b18e6e0 commit 720023c

File tree

2 files changed

+170
-73
lines changed

2 files changed

+170
-73
lines changed

beets/util/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
Any,
4141
AnyStr,
4242
Callable,
43+
Generic,
4344
Iterable,
4445
NamedTuple,
4546
TypeVar,
@@ -1041,6 +1042,30 @@ def __get__(self, instance, owner):
10411042
return self.cache[owner]
10421043

10431044

1045+
T = TypeVar("T")
1046+
1047+
1048+
class LazySharedInstance(Generic[T]):
1049+
"""A descriptor that provides access to a lazily-created shared instance of
1050+
the containing class, while calling the class constructor to construct a
1051+
new object works as usual.
1052+
"""
1053+
1054+
_instance: T | None = None
1055+
1056+
def __get__(self, instance: T | None, owner: type[T]) -> T:
1057+
if instance is not None:
1058+
raise RuntimeError(
1059+
"shared instances must be obtained from the class property, "
1060+
"not an instance"
1061+
)
1062+
1063+
if self._instance is None:
1064+
self._instance = owner()
1065+
1066+
return self._instance
1067+
1068+
10441069
def get_module_tempdir(module: str) -> Path:
10451070
"""Return the temporary directory for the given module.
10461071

0 commit comments

Comments
 (0)