From 6ea0de73fe4b76308b84674e0333dcc5ea2827e0 Mon Sep 17 00:00:00 2001 From: Vincent Michel Date: Wed, 8 May 2024 16:51:02 +0200 Subject: [PATCH] Improve operator type naming --- aiostream/core.py | 49 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/aiostream/core.py b/aiostream/core.py index 65807a0..0cb9469 100644 --- a/aiostream/core.py +++ b/aiostream/core.py @@ -369,9 +369,6 @@ async def random(offset=0., width=1.): # Gather attributes class OperatorImplementation: - __qualname__ = name - __module__ = module - __doc__ = doc original = staticmethod(original_func) @@ -411,8 +408,18 @@ def __str__(self) -> str: OperatorImplementation.__call__.__module__ = module OperatorImplementation.__call__.__doc__ = doc - # Create operator class - return OperatorImplementation() + # Create operator singleton + properly_named_class = type( + name, + (OperatorImplementation,), + { + "__qualname__": name, + "__module__": module, + "__doc__": doc, + }, + ) + operator_instance = properly_named_class() + return operator_instance def pipable_operator( @@ -507,9 +514,6 @@ def double(source): # Gather attributes class PipableOperatorImplementation: - __qualname__ = name - __module__ = module - __doc__ = doc original = staticmethod(original_func) @@ -582,8 +586,17 @@ def __str__(self) -> str: if extra_doc: PipableOperatorImplementation.pipe.__doc__ += "\n\n " + extra_doc - # Create operator class - operator_instance = PipableOperatorImplementation() + # Create operator singleton + properly_named_class = type( + name, + (PipableOperatorImplementation,), + { + "__qualname__": name, + "__module__": module, + "__doc__": doc, + }, + ) + operator_instance = properly_named_class() return operator_instance @@ -674,9 +687,6 @@ def chain_twice(*sources): # Gather attributes class SourcesOperatorImplementation: - __qualname__ = name - __module__ = module - __doc__ = doc original = staticmethod(original_func) @@ -741,6 +751,15 @@ def __str__(self) -> str: if extra_doc: SourcesOperatorImplementation.pipe.__doc__ += "\n\n " + extra_doc - # Create operator class - operator_instance = SourcesOperatorImplementation() + # Create operator singleton + properly_named_class = type( + name, + (SourcesOperatorImplementation,), + { + "__qualname__": name, + "__module__": module, + "__doc__": doc, + }, + ) + operator_instance = properly_named_class() return operator_instance