encode / apistar

The Web API toolkit. 🛠
https://docs.apistar.com
BSD 3-Clause "New" or "Revised" License
5.57k stars 411 forks source link

apistar is not working in python3.7+ #595

Closed ghost closed 6 years ago

ghost commented 6 years ago

Framework use types.NewType for create new types. For example: WSGIEnviron = typing.NewType('WSGIEnviron', dict) And used it's in annotations for handlers which in turn checked with issubclass: elif issubclass(param.annotation, types.Type): # apistar/server/core.py line 70

But is incorrect. types.NewType don't return a type (class), it's return a function.

Why did it work? At first, only if second type will be created with ABCMeta.

class A(metaclass=ABCMeta): pass
T = NewType('T', int)
issublclass(A, T) # False

There was such an option that the first argument was simply not checked.

In Python3.7, the abc module has been rewritten, and the issubclass become:

class ABCMeta(type):
    ...
    def __subclasscheck__(cls, subclass):
        """Override for issubclass(subclass, cls)."""
        return _abc_subclasscheck(cls, subclass)
    ...

And C impl:

static PyObject *
_abc__abc_subclasscheck_impl(PyObject *module, PyObject *self, PyObject *subclass)
{
    if (!PyType_Check(subclass)) { // <-- HERE
        PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
        return NULL;
    }

Result, the "hello, world" from tutorial with Python3.7:

Traceback (most recent call last): File "main.py", line 13, in application = App(routes) File "/home/r/w/python/tps/.env/lib/python3.7/site-packages/apistar/server/app.py", line 54, in init routes = routes + self.include_extra_routes(schema_url, docs_url, static_url) File "/home/r/w/python/tps/.env/lib/python3.7/site-packages/apistar/server/app.py", line 84, in include_extra_routes name='static', documented=False, standalone=True File "/home/r/w/python/tps/.env/lib/python3.7/site-packages/apistar/server/core.py", line 17, in init self.link = self.generate_link(url, method, handler, self.name) File "/home/r/w/python/tps/.env/lib/python3.7/site-packages/apistar/server/core.py", line 20, in generate_link fields = self.generate_fields(url, method, handler) File "/home/r/w/python/tps/.env/lib/python3.7/site-packages/apistar/server/core.py", line 70, in generate_fields elif issubclass(param.annotation, types.Type): File "/home/r/w/python/tps/.env/lib/python3.7/abc.py", line 143, in subclasscheck return _abc_subclasscheck(cls, subclass) TypeError: issubclass() arg 1 must be a class

ghost commented 6 years ago

UPDATE: https://mail.python.org/pipermail/python-dev/2018-June/154031.html

alkovpro commented 6 years ago

finishing PR for this issue is #612

alkovpro commented 6 years ago

I think this issue can be closed now)