Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
junkmd opened this issue Nov 4, 2024 · 0 comments
Labels
good first issue Good for newcomers

Comments

@junkmd
Copy link
Collaborator

junkmd commented Nov 4, 2024

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.

# 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_"]

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 junkmd added the good first issue Good for newcomers label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant