[clang] [libclang/python] Fix some type errors, add type annotations (PR #98745)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 20 16:45:04 PDT 2024


================
@@ -169,25 +198,32 @@ def __init__(self, enumeration, message):
 
 ### Structures and Utility Classes ###
 
+TInstance = TypeVar("TInstance")
+TResult = TypeVar("TResult")
+
 
-class CachedProperty:
+class CachedProperty(Generic[TInstance, TResult]):
     """Decorator that lazy-loads the value of a property.
 
     The first time the property is accessed, the original property function is
     executed. The value it returns is set as the new value of that instance's
     property, replacing the original method.
     """
 
-    def __init__(self, wrapped):
+    def __init__(self, wrapped: Callable[[TInstance], TResult]):
         self.wrapped = wrapped
         try:
             self.__doc__ = wrapped.__doc__
         except:
             pass
 
-    def __get__(self, instance, instance_type=None):
+    def __get__(self, instance: TInstance, instance_type: Any = None) -> TResult:
         if instance is None:
-            return self
+            property_name = self.wrapped.__name__
+            class_name = instance_type.__name__
+            raise TypeError(
+                f"'{property_name}' is not a static attribute of '{class_name}'"
+            )
----------------
Endilll wrote:

Makes sense. Worth noting that this is technically a breaking change.

https://github.com/llvm/llvm-project/pull/98745


More information about the cfe-commits mailing list