kosiew commented on code in PR #1277:
URL: 
https://github.com/apache/datafusion-python/pull/1277#discussion_r2483186413


##########
python/datafusion/user_defined.py:
##########
@@ -22,15 +22,31 @@
 import functools
 from abc import ABCMeta, abstractmethod
 from enum import Enum
-from typing import TYPE_CHECKING, Any, Callable, Optional, Protocol, TypeVar, 
overload
+from typing import (
+    TYPE_CHECKING,
+    Any,
+    Callable,
+    Optional,
+    Protocol,
+    TypeVar,
+    cast,
+    overload,
+)
 
 import pyarrow as pa
+from typing_extensions import TypeGuard
 
 import datafusion._internal as df_internal
 from datafusion.expr import Expr
 
 if TYPE_CHECKING:
+    from _typeshed import CapsuleType as _PyCapsule
+
     _R = TypeVar("_R", bound=pa.DataType)
+else:
+
+    class _PyCapsule:
+        """Lightweight typing proxy for CPython ``PyCapsule`` objects."""

Review Comment:
   _typeshed.CapsuleType only exists for static type checkers (TYPE_CHECKING), 
so inside the else: branch (runtime, not TYPE_CHECKING) we provide a 
lightweight runtime proxy to keep the _PyCapsule symbol defined. 
   
   Without the class _PyCapsule in the else branch, the following will error:
   
   ```python
   from typing import TYPE_CHECKING, TypeGuard
   
   if TYPE_CHECKING:
       from _typeshed import CapsuleType as _PyCapsule
   
   def is_capsule(obj: object) -> TypeGuard[_PyCapsule]:
       return hasattr(obj, "__capsule__")
   ```
   
   ```
   Traceback (most recent call last):
     File "/Users/kosiew/GitHub/datafusion-python/examples/example_fail.py", 
line 6, in <module>
       def is_capsule(obj: object) -> TypeGuard[_PyCapsule]:
                                                ^^^^^^^^^^
   NameError: name '_PyCapsule' is not defined
   
   ```
   
   



-- 
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