Skip to content

Commit 2ce1182

Browse files
committed
Rename _single_instances to _singular_instances in Singular class and update related references
1 parent 1c72061 commit 2ce1182

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

ipylab/common.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ class Singular(HasTraits):
609609
"""A base class that ensures only one instance of a class exists for each unique
610610
key (except for None).
611611
612-
This class uses a class-level dictionary `_single_instances` to store instances,
613-
keyed by a value obtained from the `get_single_key` method. Subsequent calls to
612+
This class uses a class-level dictionary `_singular_instances` to store instances,
613+
keyed by a value obtained from the `get_single_key` classmethod. Subsequent calls to
614614
the constructor with the same key will return the existing instance. If key is
615615
None, a new instance is always created and a reference is not kept to the object.
616616
@@ -619,13 +619,13 @@ class Singular(HasTraits):
619619
"""
620620

621621
singular_init_started = traitlets.Bool(read_only=True)
622-
_single_instances: ClassVar[dict[Hashable, Self]] = {}
622+
_singular_instances: ClassVar[dict[Hashable, Self]] = {}
623623
single_key = AnyTrait(default_value=None, allow_none=True, read_only=True)
624624
closed = Bool(read_only=True)
625625
singular: ClassVar[_SingularInstances[Self]]
626626

627627
def __init_subclass__(cls) -> None:
628-
cls._single_instances = {}
628+
cls._singular_instances = {}
629629
cls.singular = _SingularInstances()
630630

631631
@classmethod
@@ -634,11 +634,11 @@ def get_single_key(cls, *args, **kwgs) -> Hashable: # noqa: ARG003
634634

635635
def __new__(cls, /, *args, **kwgs) -> Self:
636636
key = cls.get_single_key(*args, **kwgs)
637-
if key is None or not (inst := cls._single_instances.get(key)):
637+
if key is None or not (inst := cls._singular_instances.get(key)):
638638
new = super().__new__
639639
inst = new(cls) if new is object.__new__ else new(cls, *args, **kwgs)
640640
if key:
641-
cls._single_instances[key] = inst
641+
cls._singular_instances[key] = inst
642642
inst.set_trait("single_key", key)
643643
return inst
644644

@@ -647,13 +647,13 @@ def __init__(self, /, *args, **kwgs):
647647
return
648648
self.set_trait("singular_init_started", True)
649649
super().__init__(*args, **kwgs)
650-
self.singular.set_trait("instances", tuple(self._single_instances.values()))
650+
self.singular.set_trait("instances", tuple(self._singular_instances.values()))
651651

652652
@observe("closed")
653653
def _singular_observe_closed(self, _):
654654
if self.closed and self.single_key is not None:
655-
self._single_instances.pop(self.single_key, None)
656-
self.singular.set_trait("instances", tuple(self._single_instances.values()))
655+
self._singular_instances.pop(self.single_key, None)
656+
self.singular.set_trait("instances", tuple(self._singular_instances.values()))
657657

658658
def close(self):
659659
if close := getattr(super(), "close", None):

ipylab/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def get_single_key(cls, cid: str, **kwgs) -> Hashable:
6060

6161
@classmethod
6262
def exists(cls, cid: str) -> bool:
63-
return cid in cls._single_instances
63+
return cid in cls._singular_instances
6464

6565
@classmethod
6666
def close_if_exists(cls, cid: str):
67-
if inst := cls._single_instances.pop(cid, None):
67+
if inst := cls._singular_instances.pop(cid, None):
6868
inst.close()
6969

7070
def __init_subclass__(cls, **kwargs) -> None:

tests/test_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class MySingular(Singular):
181181
obj2 = MySingular()
182182
assert obj1 is obj2
183183
obj1.close()
184-
assert obj1 not in obj1._single_instances
184+
assert obj1 not in obj1._singular_instances
185185
assert obj1.closed
186186

187187
async def test_limited_newget_single_keyed(self):
@@ -204,12 +204,12 @@ def get_single_key(cls, key: str, **kwgs):
204204
obj5 = KeyedSingle(None)
205205
obj6 = KeyedSingle(None)
206206

207-
assert obj1 in KeyedSingle._single_instances.values()
207+
assert obj1 in KeyedSingle._singular_instances.values()
208208
assert obj1 is obj2
209209
assert obj1 is not obj3
210210
assert obj4 is obj3
211211
assert obj5 is not obj6
212-
assert obj5 not in KeyedSingle._single_instances.values()
212+
assert obj5 not in KeyedSingle._singular_instances.values()
213213

214214

215215
class TestFixed:

0 commit comments

Comments
 (0)