pythonpython-3.xinterfacestub

How to create a stub Python package for PyCharm?


Let's say we have a builtin module car.

We have no autocomplete in PyCharm for it.

It has interface:

def getState(id, optional_id = None):
    pass

def log(value):
    pass

So, end usage would be:

import car


car.getState(5)
car.log('Test')

How to create a package and join it into PyCharm to make autocomplete work for a described interface?

If I created a package car-stub with car.py file, and installed it in the project, I will have to use code line from car-stub import car.

But how to make it in such a way it would not require code changes?
The system would provide autocomplete for import car.


Solution

  • Figured this out. You need to create a stub package with methods (at least this solution working).

    Create the following structure for your package:

    /car-stabs
      __init__.py
    setup.py
    

    Where car is your package name to stub (the problem might be in naming if such package does exist).

    __init__.py shall contain target interface methods.

    def getState(id, optional_id = ...): ...
    
    def log(value): ...
    

    (Or use "pass" like in the question for Python lower v3.5)

    Now push the package normally to PyPi, and import it in your project and/or share with somebody who need it. =)