orionis 0.312.0__py3-none-any.whl → 0.314.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,14 +14,35 @@ from orionis.services.introspection.instances.reflection_instance import Reflect
14
14
 
15
15
  class ReflectionConcrete(IReflectionConcrete):
16
16
 
17
- def __init__(self, concrete: Type) -> None:
17
+ @staticmethod
18
+ def isConcreteClass(concrete: Type) -> bool:
18
19
  """
19
- Initialize the ReflectionConcrete with the provided class type.
20
+ Checks if the provided concrete type is a valid ReflectionConcrete type.
20
21
 
21
22
  Parameters
22
23
  ----------
23
24
  concrete : Type
24
- The class type to be reflected upon.
25
+ The class type to be validated.
26
+
27
+ Returns
28
+ -------
29
+ bool
30
+ True if the class is a valid ReflectionConcrete type, False otherwise.
31
+ """
32
+ try:
33
+ return ReflectionConcrete.ensureIsConcreteClass(concrete)
34
+ except (ReflectionTypeError, ReflectionValueError):
35
+ return False
36
+
37
+ @staticmethod
38
+ def ensureIsConcreteClass(concrete: Type) -> bool:
39
+ """
40
+ Ensures that the provided concrete type is a valid ReflectionConcrete type.
41
+
42
+ Parameters
43
+ ----------
44
+ concrete : Type
45
+ The class type to be validated.
25
46
 
26
47
  Raises
27
48
  ------
@@ -29,35 +50,57 @@ class ReflectionConcrete(IReflectionConcrete):
29
50
  If the provided argument is not a class type or is already an instance.
30
51
  ReflectionValueError
31
52
  If the provided class is a built-in/primitive type, abstract class, or interface.
32
-
33
- Notes
34
- -----
35
- - Built-in and primitive types (e.g., int, str, list) are not allowed.
36
- - Abstract classes and interfaces (classes with abstract methods) are not allowed.
37
53
  """
54
+ # Check if the concrete is a class type
38
55
  if not isinstance(concrete, type):
39
56
  raise ReflectionTypeError(f"Expected a class, got {type(concrete)}")
40
57
 
58
+ # Define a set of built-in and primitive types
41
59
  builtin_types = {
42
60
  int, float, str, bool, bytes, type(None), complex,
43
61
  list, tuple, dict, set, frozenset
44
62
  }
45
63
 
64
+ # Check if the concrete class is a built-in or primitive type
46
65
  if concrete in builtin_types:
47
66
  raise ReflectionValueError(f"Class '{concrete.__name__}' is a built-in or primitive type and cannot be used.")
48
67
 
49
- # Check for abstract classes (including interfaces)
50
- if hasattr(concrete, '__abstractmethods__') and len(concrete.__abstractmethods__) > 0:
51
- raise ReflectionValueError(f"Class '{concrete.__name__}' is abstract or an interface and cannot be used.")
52
-
53
68
  # Prevent instantiating if it's already an instance
54
69
  if not isinstance(concrete, type):
55
70
  raise ReflectionTypeError(f"Expected a class type, got instance of '{type(concrete).__name__}'.")
56
71
 
57
72
  # Optionally, check for ABCMeta to catch interfaces
58
- if isinstance(concrete, abc.ABCMeta) and getattr(concrete, '__abstractmethods__', False):
73
+ if abc.ABC in concrete.__bases__:
59
74
  raise ReflectionValueError(f"Class '{concrete.__name__}' is an interface and cannot be used.")
60
75
 
76
+ return True
77
+
78
+ def __init__(self, concrete: Type) -> None:
79
+ """
80
+ Initialize the ReflectionConcrete with the provided class type.
81
+
82
+ Parameters
83
+ ----------
84
+ concrete : Type
85
+ The class type to be reflected upon.
86
+
87
+ Raises
88
+ ------
89
+ ReflectionTypeError
90
+ If the provided argument is not a class type or is already an instance.
91
+ ReflectionValueError
92
+ If the provided class is a built-in/primitive type, abstract class, or interface.
93
+
94
+ Notes
95
+ -----
96
+ - Built-in and primitive types (e.g., int, str, list) are not allowed.
97
+ - Abstract classes and interfaces (classes with abstract methods) are not allowed.
98
+ """
99
+
100
+ # Ensure the provided concrete type is a valid ReflectionConcrete class
101
+ ReflectionConcrete.ensureIsConcreteClass(concrete)
102
+
103
+ # Set the concrete class in the instance
61
104
  self._concrete = concrete
62
105
  self.__instance = None
63
106
 
@@ -12,35 +12,103 @@ from orionis.services.introspection.contracts.reflection_instance import IReflec
12
12
 
13
13
  class ReflectionInstance(IReflectionInstance):
14
14
 
15
- def __init__(self, instance: Any) -> None:
15
+ @staticmethod
16
+ def isInstance(instance: Any) -> bool:
16
17
  """
17
- Initialize the ReflectionInstance with a given object instance.
18
+ Check if the given object is a valid instance according to ReflectionInstance rules.
18
19
 
19
20
  Parameters
20
21
  ----------
21
22
  instance : Any
22
- The object instance to be reflected upon.
23
+ The object to check.
24
+
25
+ Returns
26
+ -------
27
+ bool
28
+ True if the object is a valid instance, False otherwise.
29
+
30
+ Notes
31
+ -----
32
+ This method catches and handles exceptions internally; it does not raise.
33
+ """
34
+ try:
35
+ return ReflectionInstance.ensureIsInstance(instance)
36
+ except (ReflectionTypeError, ReflectionValueError):
37
+ return False
38
+
39
+ @staticmethod
40
+ def ensureIsInstance(instance: Any) -> bool:
41
+ """
42
+ Validate that the provided object is a proper instance of a user-defined class.
43
+
44
+ Parameters
45
+ ----------
46
+ instance : Any
47
+ The object to validate.
48
+
49
+ Returns
50
+ -------
51
+ bool
52
+ True if the instance passes all checks.
23
53
 
24
54
  Raises
25
55
  ------
26
56
  ReflectionTypeError
27
- If the provided instance is not a valid object instance.
57
+ If the input is not a valid object instance.
28
58
  ReflectionValueError
29
- If the instance belongs to a built-in, abstract base class, or '__main__' module.
59
+ If the instance belongs to a disallowed module ('builtins', 'abc') or originates from '__main__'.
60
+
61
+ Notes
62
+ -----
63
+ This method performs the following checks:
64
+ 1. Ensures the input is an object instance (not a class/type).
65
+ 2. Disallows instances of types defined in the 'builtins' or 'abc' modules.
66
+ 3. Disallows instances originating from the '__main__' module, requiring importable module origins.
30
67
  """
68
+
69
+ # Ensure the provided instance is a valid object instance
31
70
  if not (isinstance(instance, object) and not isinstance(instance, type)):
32
71
  raise ReflectionTypeError(
33
72
  f"Expected an object instance, got {type(instance).__name__!r}: {instance!r}"
34
73
  )
74
+
75
+ # Check if the instance belongs to a built-in or abstract base class
35
76
  module = type(instance).__module__
36
77
  if module in {'builtins', 'abc'}:
37
78
  raise ReflectionValueError(
38
79
  f"Instance of type '{type(instance).__name__}' belongs to disallowed module '{module}'."
39
80
  )
81
+
82
+ # Check if the instance originates from '__main__'
40
83
  if module == '__main__':
41
84
  raise ReflectionValueError(
42
85
  "Instance originates from '__main__'; please provide an instance from an importable module."
43
86
  )
87
+
88
+ # If all checks pass, return True
89
+ return True
90
+
91
+ def __init__(self, instance: Any) -> None:
92
+ """
93
+ Initialize the ReflectionInstance with a given object instance.
94
+
95
+ Parameters
96
+ ----------
97
+ instance : Any
98
+ The object instance to be reflected upon.
99
+
100
+ Raises
101
+ ------
102
+ ReflectionTypeError
103
+ If the provided instance is not a valid object instance.
104
+ ReflectionValueError
105
+ If the instance belongs to a built-in, abstract base class, or '__main__' module.
106
+ """
107
+
108
+ # Ensure the instance is valid
109
+ ReflectionInstance.ensureIsInstance(instance)
110
+
111
+ # Store the instance for reflection
44
112
  self._instance = instance
45
113
 
46
114
  def getInstance(self) -> Any:
@@ -1,3 +1,4 @@
1
+ import inspect
1
2
  from typing import Any, Type
2
3
  from orionis.services.introspection.abstract.reflection_abstract import ReflectionAbstract
3
4
  from orionis.services.introspection.concretes.reflection_concrete import ReflectionConcrete
@@ -11,80 +12,429 @@ class Reflection:
11
12
  This class offers factory methods to obtain specialized reflection objects for instances,
12
13
  abstract classes, concrete classes, and modules. Each method returns an object that
13
14
  encapsulates the target and provides introspection capabilities.
14
-
15
- Methods
16
- -------
17
- instance(instance: Any) -> ReflectionInstance
18
- Create a reflection object for a class instance.
19
- abstract(abstract: Type) -> ReflectionAbstract
20
- Create a reflection object for an abstract class.
21
- concrete(concrete: Type) -> ReflectionConcrete
22
- Create a reflection object for a concrete class.
23
- module(module: str) -> ReflectionModule
24
- Create a reflection object for a module.
25
15
  """
26
16
 
27
17
  @staticmethod
28
18
  def instance(instance: Any) -> 'ReflectionInstance':
29
19
  """
30
- Create a reflection object for a class instance.
20
+ Create a ReflectionInstance for the given object instance.
31
21
 
32
22
  Parameters
33
23
  ----------
34
24
  instance : Any
35
- The instance to reflect upon.
25
+ The object instance to reflect.
36
26
 
37
27
  Returns
38
28
  -------
39
29
  ReflectionInstance
40
- A reflection object encapsulating the instance.
30
+ A reflection object for the given instance.
41
31
  """
42
32
  return ReflectionInstance(instance)
43
33
 
44
34
  @staticmethod
45
35
  def abstract(abstract: Type) -> 'ReflectionAbstract':
46
- """Create a reflection object for an abstract class.
36
+ """
37
+ Create a ReflectionAbstract for the given abstract class.
47
38
 
48
39
  Parameters
49
40
  ----------
50
41
  abstract : Type
51
- The abstract class to reflect upon
42
+ The abstract class to reflect.
52
43
 
53
44
  Returns
54
45
  -------
55
46
  ReflectionAbstract
56
- A reflection object encapsulating the abstract class
47
+ A reflection object for the given abstract class.
57
48
  """
58
49
  return ReflectionAbstract(abstract)
59
50
 
60
51
  @staticmethod
61
52
  def concrete(concrete: Type) -> 'ReflectionConcrete':
62
- """Create a reflection object for a concrete class.
53
+ """
54
+ Create a ReflectionConcrete for the given concrete class.
63
55
 
64
56
  Parameters
65
57
  ----------
66
58
  concrete : Type
67
- The concrete class to reflect upon
59
+ The concrete class to reflect.
68
60
 
69
61
  Returns
70
62
  -------
71
63
  ReflectionConcrete
72
- A reflection object encapsulating the concrete class
64
+ A reflection object for the given concrete class.
73
65
  """
74
66
  return ReflectionConcrete(concrete)
75
67
 
76
68
  @staticmethod
77
69
  def module(module: str) -> 'ReflectionModule':
78
- """Create a reflection object for a module.
70
+ """
71
+ Create a ReflectionModule for the given module name.
79
72
 
80
73
  Parameters
81
74
  ----------
82
75
  module : str
83
- The module name to reflect upon
76
+ The name of the module to reflect.
84
77
 
85
78
  Returns
86
79
  -------
87
80
  ReflectionModule
88
- A reflection object encapsulating the module
81
+ A reflection object for the given module.
89
82
  """
90
83
  return ReflectionModule(module)
84
+
85
+ @staticmethod
86
+ def isAbstract(obj: Any) -> bool:
87
+ """
88
+ Check if the object is an abstract base class.
89
+
90
+ Parameters
91
+ ----------
92
+ obj : Any
93
+ The object to check.
94
+
95
+ Returns
96
+ -------
97
+ bool
98
+ True if the object is abstract, False otherwise.
99
+ """
100
+ return inspect.isabstract(obj)
101
+
102
+ @staticmethod
103
+ def isAsyncGen(obj: Any) -> bool:
104
+ """
105
+ Check if the object is an asynchronous generator.
106
+
107
+ Parameters
108
+ ----------
109
+ obj : Any
110
+ The object to check.
111
+
112
+ Returns
113
+ -------
114
+ bool
115
+ True if the object is an async generator, False otherwise.
116
+ """
117
+ return inspect.isasyncgen(obj)
118
+
119
+ @staticmethod
120
+ def isAsyncGenFunction(obj: Any) -> bool:
121
+ """
122
+ Check if the object is an asynchronous generator function.
123
+
124
+ Parameters
125
+ ----------
126
+ obj : Any
127
+ The object to check.
128
+
129
+ Returns
130
+ -------
131
+ bool
132
+ True if the object is an async generator function, False otherwise.
133
+ """
134
+ return inspect.isasyncgenfunction(obj)
135
+
136
+ @staticmethod
137
+ def isAwaitable(obj: Any) -> bool:
138
+ """
139
+ Check if the object can be awaited.
140
+
141
+ Parameters
142
+ ----------
143
+ obj : Any
144
+ The object to check.
145
+
146
+ Returns
147
+ -------
148
+ bool
149
+ True if the object is awaitable, False otherwise.
150
+ """
151
+ return inspect.isawaitable(obj)
152
+
153
+ @staticmethod
154
+ def isBuiltin(obj: Any) -> bool:
155
+ """
156
+ Check if the object is a built-in function or method.
157
+
158
+ Parameters
159
+ ----------
160
+ obj : Any
161
+ The object to check.
162
+
163
+ Returns
164
+ -------
165
+ bool
166
+ True if the object is a built-in, False otherwise.
167
+ """
168
+ return inspect.isbuiltin(obj)
169
+
170
+ @staticmethod
171
+ def isClass(obj: Any) -> bool:
172
+ """
173
+ Check if the object is a class.
174
+
175
+ Parameters
176
+ ----------
177
+ obj : Any
178
+ The object to check.
179
+
180
+ Returns
181
+ -------
182
+ bool
183
+ True if the object is a class, False otherwise.
184
+ """
185
+ return inspect.isclass(obj)
186
+
187
+ @staticmethod
188
+ def isCode(obj: Any) -> bool:
189
+ """
190
+ Check if the object is a code object.
191
+
192
+ Parameters
193
+ ----------
194
+ obj : Any
195
+ The object to check.
196
+
197
+ Returns
198
+ -------
199
+ bool
200
+ True if the object is a code object, False otherwise.
201
+ """
202
+ return inspect.iscode(obj)
203
+
204
+ @staticmethod
205
+ def isCoroutine(obj: Any) -> bool:
206
+ """
207
+ Check if the object is a coroutine.
208
+
209
+ Parameters
210
+ ----------
211
+ obj : Any
212
+ The object to check.
213
+
214
+ Returns
215
+ -------
216
+ bool
217
+ True if the object is a coroutine, False otherwise.
218
+ """
219
+ return inspect.iscoroutine(obj)
220
+
221
+ @staticmethod
222
+ def isCoroutineFunction(obj: Any) -> bool:
223
+ """
224
+ Check if the object is a coroutine function.
225
+
226
+ Parameters
227
+ ----------
228
+ obj : Any
229
+ The object to check.
230
+
231
+ Returns
232
+ -------
233
+ bool
234
+ True if the object is a coroutine function, False otherwise.
235
+ """
236
+ return inspect.iscoroutinefunction(obj)
237
+
238
+ @staticmethod
239
+ def isDataDescriptor(obj: Any) -> bool:
240
+ """
241
+ Check if the object is a data descriptor.
242
+
243
+ Parameters
244
+ ----------
245
+ obj : Any
246
+ The object to check.
247
+
248
+ Returns
249
+ -------
250
+ bool
251
+ True if the object is a data descriptor, False otherwise.
252
+ """
253
+ return inspect.isdatadescriptor(obj)
254
+
255
+ @staticmethod
256
+ def isFrame(obj: Any) -> bool:
257
+ """
258
+ Check if the object is a frame object.
259
+
260
+ Parameters
261
+ ----------
262
+ obj : Any
263
+ The object to check.
264
+
265
+ Returns
266
+ -------
267
+ bool
268
+ True if the object is a frame object, False otherwise.
269
+ """
270
+ return inspect.isframe(obj)
271
+
272
+ @staticmethod
273
+ def isFunction(obj: Any) -> bool:
274
+ """
275
+ Check if the object is a Python function.
276
+
277
+ Parameters
278
+ ----------
279
+ obj : Any
280
+ The object to check.
281
+
282
+ Returns
283
+ -------
284
+ bool
285
+ True if the object is a function, False otherwise.
286
+ """
287
+ return inspect.isfunction(obj)
288
+
289
+ @staticmethod
290
+ def isGenerator(obj: Any) -> bool:
291
+ """
292
+ Check if the object is a generator.
293
+
294
+ Parameters
295
+ ----------
296
+ obj : Any
297
+ The object to check.
298
+
299
+ Returns
300
+ -------
301
+ bool
302
+ True if the object is a generator, False otherwise.
303
+ """
304
+ return inspect.isgenerator(obj)
305
+
306
+ @staticmethod
307
+ def isGeneratorFunction(obj: Any) -> bool:
308
+ """
309
+ Check if the object is a generator function.
310
+
311
+ Parameters
312
+ ----------
313
+ obj : Any
314
+ The object to check.
315
+
316
+ Returns
317
+ -------
318
+ bool
319
+ True if the object is a generator function, False otherwise.
320
+ """
321
+ return inspect.isgeneratorfunction(obj)
322
+
323
+ @staticmethod
324
+ def isGetSetDescriptor(obj: Any) -> bool:
325
+ """
326
+ Check if the object is a getset descriptor.
327
+
328
+ Parameters
329
+ ----------
330
+ obj : Any
331
+ The object to check.
332
+
333
+ Returns
334
+ -------
335
+ bool
336
+ True if the object is a getset descriptor, False otherwise.
337
+ """
338
+ return inspect.isgetsetdescriptor(obj)
339
+
340
+ @staticmethod
341
+ def isMemberDescriptor(obj: Any) -> bool:
342
+ """
343
+ Check if the object is a member descriptor.
344
+
345
+ Parameters
346
+ ----------
347
+ obj : Any
348
+ The object to check.
349
+
350
+ Returns
351
+ -------
352
+ bool
353
+ True if the object is a member descriptor, False otherwise.
354
+ """
355
+ return inspect.ismemberdescriptor(obj)
356
+
357
+ @staticmethod
358
+ def isMethod(obj: Any) -> bool:
359
+ """
360
+ Check if the object is a method.
361
+
362
+ Parameters
363
+ ----------
364
+ obj : Any
365
+ The object to check.
366
+
367
+ Returns
368
+ -------
369
+ bool
370
+ True if the object is a method, False otherwise.
371
+ """
372
+ return inspect.ismethod(obj)
373
+
374
+ @staticmethod
375
+ def isMethodDescriptor(obj: Any) -> bool:
376
+ """
377
+ Check if the object is a method descriptor.
378
+
379
+ Parameters
380
+ ----------
381
+ obj : Any
382
+ The object to check.
383
+
384
+ Returns
385
+ -------
386
+ bool
387
+ True if the object is a method descriptor, False otherwise.
388
+ """
389
+ return inspect.ismethoddescriptor(obj)
390
+
391
+ @staticmethod
392
+ def isModule(obj: Any) -> bool:
393
+ """
394
+ Check if the object is a module.
395
+
396
+ Parameters
397
+ ----------
398
+ obj : Any
399
+ The object to check.
400
+
401
+ Returns
402
+ -------
403
+ bool
404
+ True if the object is a module, False otherwise.
405
+ """
406
+ return inspect.ismodule(obj)
407
+
408
+ @staticmethod
409
+ def isRoutine(obj: Any) -> bool:
410
+ """
411
+ Check if the object is a user-defined or built-in function or method.
412
+
413
+ Parameters
414
+ ----------
415
+ obj : Any
416
+ The object to check.
417
+
418
+ Returns
419
+ -------
420
+ bool
421
+ True if the object is a routine, False otherwise.
422
+ """
423
+ return inspect.isroutine(obj)
424
+
425
+ @staticmethod
426
+ def isTraceback(obj: Any) -> bool:
427
+ """
428
+ Check if the object is a traceback object.
429
+
430
+ Parameters
431
+ ----------
432
+ obj : Any
433
+ The object to check.
434
+
435
+ Returns
436
+ -------
437
+ bool
438
+ True if the object is a traceback object, False otherwise.
439
+ """
440
+ return inspect.istraceback(obj)