Type annotations for LibreOffice API
Project description
This project allow typings for the full LibreOffice API
WHY
Working with LibreOffice API in a modern IDE such as Visual Studio Code there is not type support for LibreOffice API This project solves that Issue.
VERSION
This package is for Version 7.2 of LibreOffice API.
Installation
PIP
types-unopy PyPI
Versions of this package for LibreOffice API 7.2 are below version 0.2.0.
$ pip install "types-unopy<0.2"
USAGE
Not all object in LibreOffice API can be directly imported.
For instance if you need to import SheetCellRange so it can be used as type the following will fail at runtime.
>>> from com.sun.star.sheet import SheetCellRange
ImportError: No module named 'com' (or 'com.sun.star.sheet.SheetCellRange' is unknown)
The solution is to use TYPE_CHECKING.
>>> from __future__ import annotations
>>> from typing import TYPE_CHECKING
>>> if TYPE_CHECKING:
... from com.sun.star.sheet import SheetCellRange
...
Anything imported in the TYPE_CHECKING block will not be available at runtime. For this reason types inside the TYPE_CHECKING must be wrapped in quotes OR from __future__ import annotations must be the first line of the module.
Example of wrapping type in quotes.
def do_work(range: 'SheetCellRange') -> None: ...
Known Issues
Enums
There is no enum classes in API only enum members.
To acces the enum members they must be imported directly.
For example to import com.sun.star.beans.PropertyState.DIRECT_VALUE
If you need the behaviour of regular Enum Classes consider using ooouno
>>> from com.sun.star.beans import PropertyState
ImportError: No module named 'com' (or 'com.sun.star.beans.PropertyState' is unknown
>>>
>>> from com.sun.star.beans.PropertyState import DIRECT_VALUE
>>> DIRECT_VALUE.value
'DIRECT_VALUE'
>>>
>>> type(DIRECT_VALUE)
<class 'uno.Enum'>
Demo
Special Cases
By default an ImportError is raised when importing form com.sun.star at runtime. This is by design as the import error triggers uno to search LibreOffice API for actual import; Othwewise, com.sun.star is seen a namesapce import and uno is ignored.
In some cases the ImportError may need to be suppressed. This is the case in setup.py of this project.
Suppressing ImportError is acomplished by adding "ooouno_ignore_import_error" to environment and setting it to "True"
>>> import os
>>> os.environ["ooouno_ignore_import_error"] = "True" # must be string
When building with Sphinx and autodoc it may be necessary to exclude uno related imports. This can be accomplished using the autodoc_mock_imports option.
# docs conf.py
autodoc_mock_imports = ['uno', 'unohelper', 'com']
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for types_unopy-0.1.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8be586b866dc9f73913ac5b7131b87da519dff71058f69ef824e0a8d91fd0c40 |
|
MD5 | 826413988c88c9e740b73f18487f6342 |
|
BLAKE2b-256 | 66fb410073ecc96a02f0bcee9651c8c8beadb65ec642cac2fb7995240248b1df |