Skip to main content

Python stubs for FreeCAD

Project description

freecad-stubs

PyPI version GitHub license PyPI pyversions

Python stubs generated from FreeCAD source code.

Stub source

Stubs are generated based on several available info:

  • *Py.xml files - mainly docstrings, argument names, properties.
  • corresponding *PyImp.cpp file - argument types are extracted from C code.
  • other *.cpp files - functions for modules.

Unfortunately not all typing information may be generated. For example some objects are added dynamically. There are also many special cases therefore not all object are correctly mapped. Moreover, some of C function has errors - invalid types, missing arguments (you can see more these errors if you change logger flag in configuration file freecad-stubs/lib/freecad_stub_gen/config.py)

Install stubs

You can install all stubs for freeCAD packages by running:

python -m pip install freecad-stubs 

Adding stubs to python path

There are a lot of possible methods. This is only example:

  1. Copy stubs from a desired branch from this repository.
    Checkout to desired branch and copy folder /freecad_stubs.

  2. (Optional) Or generate stubs manually. Copy/move generated stubs to location of your choice. For example:

    cp -r ./freecad_stubs "$HOME/.local/lib/python3.9/freecad_stubs" 
    
  3. Add location with stubs to python search path. For example:

    echo "$HOME/.local/lib/python3.9/freecad_stubs" > "$HOME/.local/lib/python3.9/site-packages/freecad_stubs.pth" 
    

Stub Generation

  1. Clone freecad repository.

    git clone https://github.com/FreeCAD/FreeCAD.git
    

    Warning: FreeCAD repository has over 1.29 GB

    You may download only these required folders:

    • /src/App
    • /src/Base
    • /src/Gui
    • /src/Mod
  2. Clone this repository

    git clone https://github.com/ostr00000/freecad-stubs
    
  3. Configure paths
    In the file freecad-stubs/lib/freecad_stub_gen/config.py set desired configuration:

    • SOURCE_DIR - src folder from FreeCAD repository,
    • TARGET_DIR - target folder where stubs should be generated.

      Warning: TARGET_DIR folder and its content may be removed when generating stubs.

  4. Run the main file from this project in Python

    pytohn freecad-stubs/lib/freecad_stub_gen/__main__.py
    

    Required python version: >=3.10.

Progress

Stub source

  • generate class stub (xml files):
    • property in xml,
    • dynamic property added in cpp,
    • method (+ static/class method),
    • rich comparison (ex. __eq__ method),
    • number protocol (ex. __add__ method),
  • generate class stub (cpp files):
    • dynamically added in init_type(),
  • generate functions stub (cpp files):
    • declared in PyMethodDef array,
    • dynamically added in module constructor (subclass Py::ExtensionModule),

Stub quality

  • found class/function/method name,
  • copy docstring (+ generate property docstring),
  • guess argument names from docstrings,
  • found function/method argument types based on c-api,
  • function/method return type,
  • property getter type,
  • property setter type,
  • add comment "safe to import" for importable object,

/# TODO find direct types and add comment - Base::Interpreter().addType

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

freecad-stubs-1.0.2.tar.gz (148.2 kB view hashes)

Uploaded Source

Built Distribution

freecad_stubs-1.0.2-py3-none-any.whl (156.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page