Skip to content

Commit d4cb6d3

Browse files
committed
Using a special class instead of strings for annotation annotation
1 parent b402cc9 commit d4cb6d3

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

named_annotations.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from typing import Iterable, Mapping, Final
2+
from typing import Union, Iterable, Optional, Self, Mapping, Final
33

44

55
class AnnotationFactory(ABC):
@@ -25,16 +25,33 @@ def _create_full_annotation_by(self, annotation: any) -> any:
2525
"""Annotation Creation Method from an input annotation."""
2626

2727

28+
class InputAnnotationAnnotation:
29+
"""
30+
Singleton class for the annotation of the conditional empty space, in which
31+
the input type in the CustomAnnotationFactory should be placed.
32+
"""
33+
34+
_instance: Optional[Self] = None
35+
36+
def __new__(cls, *args, **kwargs):
37+
if not hasattr(cls, '_instance') or cls._instance is None:
38+
cls._instance = object.__new__(cls, *args, **kwargs)
39+
40+
return cls._instance
41+
42+
def __repr__(self) -> str:
43+
return '<input_annotation>'
44+
45+
2846
class CustomAnnotationFactory(AnnotationFactory):
2947
"""
3048
AnnotationFactory class delegating the construction of another factory's
3149
annotation.
3250
33-
When called, replaces the 'annotation' strings from its arguments and their
34-
subcollections with the input annotation.
51+
When called, replaces the InputAnnotationAnnotation instances from its
52+
arguments and their subcollections with the input annotation.
3553
"""
3654

37-
_input_annotation_annotation: str = '<input_annotation>'
3855

3956
def __init__(self, original_factory: Mapping, annotations: Iterable):
4057
self._original_factory = original_factory
@@ -48,11 +65,6 @@ def original_factory(self) -> Mapping:
4865
def annotations(self) -> tuple:
4966
return self._annotations
5067

51-
@classmethod
52-
@property
53-
def input_annotation_annotation(cls) -> str:
54-
return cls._input_annotation_annotation
55-
5668
def __repr__(self) -> str:
5769
return "{factory}{arguments}".format(
5870
factory=(
@@ -78,7 +90,7 @@ def __get_formatted_annotations_from(self, annotations: Iterable, replacement_an
7890
formatted_annotations = list()
7991

8092
for annotation in annotations:
81-
if annotation == self.input_annotation_annotation:
93+
if isinstance(annotation, InputAnnotationAnnotation):
8294
annotation = replacement_annotation
8395
elif isinstance(annotation, Iterable) and not isinstance(annotation, str):
8496
annotation = self.__get_formatted_annotations_from(
@@ -109,5 +121,5 @@ def __recursively_format(self, collection: Iterable) -> list:
109121
return formatted_collection
110122

111123

112-
input_annotation: Final[str] = CustomAnnotationFactory.input_annotation_annotation
113-
124+
# Pre-created instance without permanent formal creation of a new one.
125+
input_annotation: Final[InputAnnotationAnnotation] = InputAnnotationAnnotation()

0 commit comments

Comments
 (0)