comtypes icon indicating copy to clipboard operation
comtypes copied to clipboard

Remove the dead code that performs an early return with `if bases == (object,)` in `_coclass_meta`.

Open junkmd opened this issue 1 year ago • 2 comments

Whether it’s a remnant of Python 2.x or a legacy from the early stages of this project, there is dead code as shown below.

https://github.com/enthought/comtypes/blob/d80810b50eb642053b8dc1c3a2a4735bedf0a059/comtypes/_meta.py#L44-L62

If it can be confirmed that removing it poses no problems, I believe the codebase would be more readable without this section.

     # the coclass' _com_interfaces_ list.
     def __new__(cls, name, bases, namespace):
         self = type.__new__(cls, name, bases, namespace)
-        if bases == (object,):
-            # HACK: Could this conditional branch be removed since it is never reached? 
-            # Since definition is `class CoClass(COMObject, metaclass=_coclass_meta)`,  
-            # the `bases` parameter passed to the `_coclass_meta.__new__` would be      
-            # `(COMObject,)`.
-            # Moreover, since the `COMObject` derives from `object` and does not specify
-            # a metaclass, `(object,)` will not be passed as the `bases` parameter      
-            # to the `_coclass_meta.__new__`.
-            # The reason for this implementation might be a remnant of the differences  
-            # in how metaclasses work between Python 3.x and Python 2.x.
-            # If there are no problems with the versions of Python that `comtypes`
-            # supports, this removal could make the process flow easier to understand.
-            return self
         # XXX We should insist that a _reg_clsid_ is present.
         if "_reg_clsid_" in namespace:
             clsid = namespace["_reg_clsid_"]

junkmd avatar Nov 04 '24 14:11 junkmd

This seems to be dead code, because, as we can see in the coverage, the if is never entered.

moi15moi avatar Jun 11 '25 13:06 moi15moi

Thank you for the investigation! With multiple confirmations that this code block seems safe to remove, this is a straightforward good first issue for anyone to tackle.

junkmd avatar Jun 16 '25 14:06 junkmd