ostr00000 / freecad-stubs Goto Github PK
View Code? Open in Web Editor NEWPython stubs generated from FreeCAD source code
License: GNU General Public License v3.0
Python stubs generated from FreeCAD source code
License: GNU General Public License v3.0
Describe the bug
ProgressIndicator
is in another module (FreeCAD.Base
).
To Reproduce
import FreeCAD
progressBar = FreeCAD.Base.ProgressIndicator()
The code is correct, but stubs say it cannot reference it.
Expected behavior
No error in type checker.
PyObject* module = PyDict_GetItemString(modules, "FreeCADGui");
PyModule_AddObject(module, "PySideUic", Base::Interpreter().addModule(new PySideUicModule));
Hello, and thank you for a great project.
I am curious if you considered asking the "FreeCAD people" to include this as standard part of FreeCAD distribution?
Why? I would hope that would motivate "them" to fix multiple errors and omissions in cpp/py bridge faster :-)
Describe the bug
VS code with Pylance: wont find stubs for native library with submodules.
According https://peps.python.org/pep-0484/#stub-files
- Modules and variables imported into the stub are not considered exported from the stub unless the import uses the import ... as ... form or the equivalent from ... import ... as ... form.
- Just like in normal Python files, submodules automatically become exported attributes of their parent module when imported. For example, if the spam package has the following directory structure:
spam/ __init__.pyi ham.pyi
where
__init__.pyi
contains a line such asfrom . import ham
orfrom .ham import Ham
, then ham is an exported attribute of spam.
To Reproduce
Steps to reproduce the behavior:
#!/usr/bin/env python
import sys
import FreeCAD as FreeCAD
import FreeCADGui as Gui
import Part
from PySide import QtGui
if __name__ == "__main__":
# This will start FreeCAD and draw a box.
app = QtGui.QApplication(sys.argv)
Gui.showMainWindow()
doc = FreeCAD.newDocument()
box = Part.makeBox(100, 100, 100)
FreeCAD.Console.PrintMessage('Hello, World!')
Part.show(box)
sys.exit(app.exec_())
and .env file
FREECAD_BIN="C:/Program Files/FreeCAD 0.21/bin"
FREECAD_BIN_LIB="C:/Program Files/FreeCAD 0.21/bin/Lib"
FREECAD_BIN_LIB_PAC="C:/Program Files/FreeCAD 0.21/bin/Lib/site-packages"
FREECAD_LIB="C:/Program Files/FreeCAD 0.21/lib"
FREECAD_MOD="C:/Program Files/FreeCAD 0.21/Mod"
FREECAD_EXT="C:/Program Files/FreeCAD 0.21/EXT"
PYTHONPATH=${FREECAD_BIN};${FREECAD_LIB};${FREECAD_MOD};${FREECAD_EXT};${FREECAD_BIN_LIB};${FREECAD_BIN_LIB_PAC}
Expected behavior
If the mouse cursor hover on Part.makeBox there is information about the function.
Used versions (please complete the following information):
Additional context
To fix the issue I've added from . import Console
instead of import FreeCAD.Console
to the file __init__.pyi
in the C:\Program Files\FreeCAD 0.21\bin\Lib\site-packages\FreeCAD-stubs\
folder.
Hello again. I hacked around the limitations described below with my own scripts and have no problem,
but would like to see "stubs" be more cool. :-) Therefore:
OPTION ONE
currently, there is a qtpy
dependency:
dependencies = [
# 'pivy', # cannot install it in a normal way, should be installed by FreeCAD anyway
'qtpy',
]
and FreeCAD
also provides its own package proxy called PySide
https://github.com/FreeCAD/FreeCAD/blob/main/cMake/FreeCAD_Helpers/SetupShibokenAndPyside.cmake
which is deployed on the target system at:
/usr/lib/freecad/Ext
and used throughout the internal code, for example:
https://github.com/FreeCAD/FreeCAD/blob/main/src/Mod/Path/Path/Tool/Gui/BitCmd.py#L28
So: would it make sense for "stubs" to drop qtpy
and include PySide
form the FreeCAD
instead?
Why? so that templates.py
could be copied as is :-) and all other PySide
imports resolve OK
https://github.com/ostr00000/freecad-stubs/blob/main/freecad_stubs/FreeCADTemplates/templates.py
OPTION TWO
Alternatively, "stubs" could go even further, and instead run FreeCAD
folder discovery,
and generate during pip install
automagically a freecad.pth
file:
${PROJECT_DIR}/.venv/lib/python3.11/site-packages/freecad.pth
with content similar to this:
/usr/lib/freecad/lib
/usr/lib/freecad/Ext
/usr/lib/freecad/Mod/AddonManager
that would also take care of dll/so
dependency, so that tests of the plugin-under-development could run from the same venv
against the production FreeCAD
installation
more info on the pth
files:
https://docs.python.org/3/library/site.html
after upgrading to the latest stubs
:
https://pypi.org/project/freecad-stubs/1.0.16/
and building to the latest FreeCAD
:
OS: Arch Linux (KDE/plasma)
Word size of FreeCAD: 64-bit
Version: 0.22.0dev.35325 (Git)
Build type: None
Branch: makepkg
Hash: 5c2eedbd149536f88cf70b085d7afff101fa2037
Python 3.11.6, Qt 5.15.11, Coin 4.0.1, Vtk 9.3.0, OCC 7.7.2
and copying templates.py
"almost-as-is" into the plugin-under-development,
there is an import error on startup:
During initialization the error "module 'FreeCADGui' has no attribute 'View3DInventorViewerPy'" occurred ...
without bothering about "why and where", one way to future-proof against this is to include in templates.py
:
from __future__ import annotations
Hi,
thanks for all your work, this is great and I was looking a long time for something like this. And sorry for creating an issue, I didn't know how to reach you.
I have a question regarding an import error message and intellisense.
I read that this kind of error occures when no library and only definition is found? (Issue here).
But I would expect that intellisense should find the package when typing?
Or do I misunderstand something here?
Thanks!
Describe the bug
Currently, there are many hacky ways to generate stubs:
A better solution is to generate files with replaced macros and then based on these files generate stub files.
Also, maybe we should use a more advanced library for parsing C++ code: this library looks very promising.
This repo is great, thanks for putting it together.
While using it for the first time today, I noticed that for the Vector class in the generated stub file Base.pyi, the mul magic method is included but rmul is not.
This results in no mypy errors for something like:
Vector(1,1,1) * 10
But an error of: 'Unsupported operand types for * ("float" and "Vector")' error for:
10 * Vector(1,1,1)
Once I get time, I'll poke around the generation code to try and add this in or would be happy for someone else to do so.
currently, in the stubs
:
while verification in python console
shows:
>>> FreeCAD.Document
<class 'App.Document'>
>>> FreeCADGui.Document
<class 'Gui.Document'>
>>> type(FreeCAD.getDocument("your_document_name"))
<class 'App.Document'>
local setup:
OS: Arch Linux (KDE/plasmax11)
Word size of FreeCAD: 64-bit
Version: 0.22.0dev.36733 (Git)
Build type: None
Branch: makepkg
Hash: 99da05f8a63fbe8870dab34dbdc9e94ab922c670
Python 3.11.8, Qt 5.15.12, Coin 4.0.2, Vtk 9.3.0, OCC 7.7.2
Locale: English/United States (en_US)
{"addWorkbenchManipulator", (PyCFunction) Application::sAddWbManipulator, METH_VARARGS,
"addWorkbenchManipulator(obj) -> None\n"
/*!
* \brief modifyMenuBar
* Method to manipulate the menu structure of a workbench.
*/
void modifyMenuBar(MenuItem* menuBar) override;
/*!
* \brief modifyContextMenu
* Method to manipulate the contextmenu structure of a workbench.
*/
void modifyContextMenu(const char* recipient, MenuItem* menuBar) override;
/*!
* \brief modifyToolBars
* Method to manipulate the toolbar structure of a workbench
*/
void modifyToolBars(ToolBarItem* toolBar) override;
/*!
* \brief modifyDockWindows
* Method to manipulate the dock window structure of a workbench
*/
void modifyDockWindows(DockWindowItems* dockWindow) override;
/*!
* \brief WorkbenchManipulatorPython::modifyMenuBar
* \param menuBar
* The Python manipulator can be implemented as
* \code
* class Manipulator:
* def modifyMenuBar(self):
* return [{"remove" : "Std_Quit"},
* {"append" : "Std_About", "menuItem" : "Std_DlgMacroRecord"},
* {"insert" : "Std_About", "menuItem" : "Std_DlgParameter"}
* {"insert" : "Std_Windows", "menuItem" : "Std_DlgParameter", "after" : ""}]
*
* manip = Manipulator()
* Gui.addWorkbenchManipulator(manip)
* \endcode
* This manipulator removes the Std_Quit command, appends the Std_About command
* to the Macro menu, inserts it to the Tools menu before the Std_DlgParameter
* and adds the Std_Windows after the Std_DlgParameter command.
*/
void WorkbenchManipulatorPython::modifyMenuBar(MenuItem* menuBar)
SelectionObserverPython
has no xml
and so it is missing from the stubs:
https://forum.freecad.org/viewtopic.php?p=497885
#define FC_PY_SEL_OBSERVER \
FC_PY_ELEMENT(onSelectionChanged) \
FC_PY_ELEMENT(addSelection) \
FC_PY_ELEMENT(removeSelection) \
FC_PY_ELEMENT(setSelection) \
FC_PY_ELEMENT(clearSelection) \
FC_PY_ELEMENT(setPreselection) \
FC_PY_ELEMENT(removePreselection) \
FC_PY_ELEMENT(pickedListChanged)
Hi! I've just found this project and it seems like it would be very useful for people who create workbench or macro for freecad.
I'm currently writing a macro and I've installed this package. The problem is that error message from lsp such as "App" is not defined
shows up.
I know that it is because I don't define App
in my code. Freecad would use the App
variable inside the program when I run the macro. But are there any way I can get rid of this error message?
Another thing is when I write doc = App.ActiveDocument
, the doc
type would be Unknown
. How can I change the type into Document
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.