From 71490a77339a6bbcb0414056a243fe361b24e065 Mon Sep 17 00:00:00 2001 From: Will Da Silva Date: Fri, 11 Jun 2021 15:22:30 -0400 Subject: [PATCH 1/2] Add venv directories to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 9df665e..6998e82 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,9 @@ doc/_build/ src/pyq/version.py html/ +.venv*/ +venv*/ + *.pyc .coverage .coverage.* From 79786e8c848c4499c4a4d573d9a46e4965897f93 Mon Sep 17 00:00:00 2001 From: Will Da Silva Date: Fri, 11 Jun 2021 15:32:43 -0400 Subject: [PATCH 2/2] Patch `pandas.core.dtypes.inference.is_dict_like` Pandas thinks all non-atom K objects are dicts, which leads to issues like #133. This commit provides a hacky solution to this problem by overwriting the `__code__` attribute of the function Pandas uses for checking if an object is dict-like. Patching the function through safer means (e.g. with `unittest.mock`) won't work well because the function is imported all over Pandas, and we would have to patch all of them. Fixes #133 --- src/pyq/__init__.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/pyq/__init__.py b/src/pyq/__init__.py index 51c1a2b..1babaa4 100755 --- a/src/pyq/__init__.py +++ b/src/pyq/__init__.py @@ -1052,3 +1052,27 @@ def _trp_call(*args, **kwds): _set_excepthook(sys.excepthook) K._k = classmethod(_trp_k) K.__call__ = _trp_call + + +# HACK: Fixes https://github.com/KxSystems/pyq/issues/133 +try: + import pandas +except ImportError: + pass +else: + # Pandas thinks all non-atom K objects are dicts. This function performs + # the same dict-checking logic used by Pandas, but with a special case for + # K objects. + def _is_dict_like_override(x): + # The import is necessary for K to be in scope because the __code__ + # of this function is used elsewhere, with different globals and locals + from pyq import K + if isinstance(x, K): + return x.type() == 99 + dict_like_attrs = ("__getitem__", "keys", "__contains__") + return all(hasattr(x, attr) for attr in dict_like_attrs) and not \ + isinstance(x, type) + + # XXX + pandas.core.dtypes.inference.is_dict_like.__code__ = \ + _is_dict_like_override.__code__