gemini-code-assist[bot] commented on code in PR #18534:
URL: https://github.com/apache/tvm/pull/18534#discussion_r2573340714


##########
python/tvm/meta_schedule/search_strategy/search_strategy.py:
##########
@@ -87,6 +87,25 @@ class SearchStrategy(Object):
         ],
     ]
 
+    def __init__(self, *args, **kwargs):
+        """Prevent direct instantiation of abstract SearchStrategy class.
+        
+        SearchStrategy is an abstract class and cannot be directly 
instantiated.
+        Use SearchStrategy.create() or a concrete subclass instead.
+        """
+        # Check if this is a direct instantiation of SearchStrategy (not a 
subclass)
+        # Subclasses will have a different type
+        if type(self) is SearchStrategy:
+            raise TypeError(
+                "Cannot instantiate abstract class SearchStrategy. "
+                "Use SearchStrategy.create() with a valid strategy type "
+                "(e.g., 'evolutionary', 'replay-trace', 'replay-func') "
+                "or use a concrete subclass instead."
+            )
+        
+        # Allow parent class initialization to proceed for proper subclasses
+        super().__init__(*args, **kwargs)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   While using `__init__` to prevent instantiation of this abstract base class 
works, a more idiomatic and slightly cleaner approach in Python is to use the 
`__new__` class method. The `__new__` method is responsible for creating the 
instance, while `__init__` is for initializing it. By raising the `TypeError` 
in `__new__`, you prevent the instance from being created at all, which is 
semantically more correct for this check. This also removes the need for the 
`super().__init__` call and the now-unused `*args` and `**kwargs`.
   
   ```python
       def __new__(cls):
           """Prevent direct instantiation of abstract SearchStrategy class.
   
           SearchStrategy is an abstract class and cannot be directly 
instantiated.
           Use SearchStrategy.create() or a concrete subclass instead.
           """
           if cls is SearchStrategy:
               raise TypeError(
                   "Cannot instantiate abstract class SearchStrategy. "
                   "Use SearchStrategy.create() with a valid strategy type "
                   "(e.g., 'evolutionary', 'replay-trace', 'replay-func') "
                   "or use a concrete subclass instead."
               )
           return super().__new__(cls)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to