ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
289 stars 41 forks source link

Cython compiled methods not correctly identified #123

Open pdtilton opened 4 years ago

pdtilton commented 4 years ago

When dumping Cython compiled objects, methods are not handled, or identified correctly.

class Network:
    def __init__(self, name: str) -> None:
    self.name = name

    def shared(self) -> bool:
        return False

jsons.dumps(Network("Foobaz"), verbose=True)

Output:

{"name": "Foobaz", "shared": {}, "-meta": {"classes": {"/shared": "typing.Callable[[], bool]", "/": "models.Network"}, "dump_time": "2020-11-18T19:03:35Z"}}

Expected:

{"name": "Foobaz", "-meta": {"classes": {"/": "models.Network"}, "dump_time": "2020-11-18T19:19:59Z"}}

I have narrowed this down to inspect.isfunction() in serializers/default_object.py. It seems that the issue really lies within the inspect module; isfunction() does not correctly recognize Cython compiled methods.

I've tried a number of workarounds with my project, but have settled on replacing inspect.isfunction() with callable(), for the time being.

Using cythonize -X annotation_typing=False

jsons 1.3.0 Python 3.6, Python 3.8 Cython 0.28.1, 0.29.14