cztomczak / cefpython

Python bindings for the Chromium Embedded Framework (CEF)
Other
3.08k stars 473 forks source link

Create stub for cefpython module or other way to improve auto completion and type checking in PyCharm #356

Open cztomczak opened 7 years ago

cztomczak commented 7 years ago

For example PyCharm doesn't know methods on the Browser object returned by cef.CreateBrowserSync. Figure out how to enhance PyCharm auto completion and checking of argument types and returned types.

Questions:

Useful links:

cztomczak commented 6 years ago

I've added a docstring comments with param types and return type, but PyCharm did not use these in auto completion. I saw that the docstring appeared in stub generated by PyCharm, but it had no effect. I've added it in CookieManager.CreateManager (src/cookie.pyx):

    @classmethod
    def CreateManager(cls, py_string path,
                      py_bool persist_session_cookies=False):
        """
        Create a new cookie manager.
        :param path:
        :type path: str
        :param persist_session_cookies:
        :type path: bool
        :return: CookieManager object
        :rtype: CookieManager
        """
        # When PyCharm generates a stub for the cefpython module
        # it doesn't use the above docstring for code inspections.
        # No idea why.
        cdef CefRefPtr[CefCookieManager] cefCookieManager
        cefCookieManager = CefCookieManager_CreateManager(
                PyToCefStringValue(path), bool(persist_session_cookies),
                <CefRefPtr[CefCompletionCallback]?>NULL)
        if <void*>cefCookieManager != NULL and cefCookieManager.get():
            return CreatePyCookieManager(cefCookieManager)
        return None

Ref: https://github.com/cztomczak/cefpython/blob/f5d4696024e680b26770fbda63a0e342cb95eee8/src/cookie.pyx#L203

major-mayer commented 3 years ago

This is actually not that hard, you can simply use stubgen included in the Mypy package to automatically generate the stub files for your project. Install the cefpython3 package somewhere, maybe inside a virtual environment, and then execute this command:

stubgen -p cefpython3

It will generate a folder called .out with the nessecary interface files inside. You then have to copy these files into the folder where cefpython3 is installed. It would be nice to automate this process tho, maybe I can create a PR sometimes.

For now I attached my stubfiles to this comment (rename the file extension to ".pyi"). Documentation for this process can be found here.
init.txt cefpython_py39.txt

Edit: Well I should have checked this before, the stubfiles do all have "any" set as the return value. So this solution is not complete, but maybe it's a point to start.