Skip to main content

Type annotations for LibreOffice API

Project description

This project allow typings for the full LibreOffice API

WHY

Working with LibreOffice API in a modern code editor such as Visual Studio Code there is not type support for LibreOffice API This project solves that Issue.

VERSION

This package is for Version 2024.2 + of LibreOffice API.

From one version of LibreOffice to the next, generally speaking, the API does not changed much. Because this is the case it is very likely this current version of LibreOffice API Typings will work fine with other versions of LibreOffice. This a typing package so not much can go wrong in other versions.

Installation

PIP

types-unopy on PyPI

$ pip install types-unopy

CONDA

types-unopy on Anaconda

$ conda install -c conda-forge types-unopy

USAGE

Not all object in LibreOffice API can be directly imported.

Any UNO object that is a service cannot be imported at runtime.

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 access the enum members they must be imported directly.

For example to import com.sun.star.beans.PropertyState.DIRECT_VALUE

If you need the behavior 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

Example image.

Special Cases

ImportError

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; Otherwise, com.sun.star is seen a namespace import and uno is ignored.

In some cases the ImportError may need to be suppressed.

Suppressing ImportError is accomplished 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']

For a reference see ooo-dev-tools conf.py.

Enum Protocols

As mentioned above there are no enum classes in API only enum members.

For this reason this package implements protocols for enums.

from com.sun.star.beans.PropertyState import DIRECT_VALUE
# DIRECT_VALUE is a type of PropertyStateProto

The implemented protocol for PropertyState is as follows:

class PropertyStateProto(Protocol):
    """Protocol for PropertyState"""

    @property
    def typeName(self) -> Literal["com.sun.star.beans.PropertyState"]:
        ...
    value: Any
    AMBIGUOUS_VALUE: PropertyStateProto
    DEFAULT_VALUE: PropertyStateProto
    DIRECT_VALUE: PropertyStateProto

Implemented methods such as com.sun.star.beans.PropertyState.XPropertyState.getPropertyState() return a protocol, in this case PropertyStateProto.

If you need to import a protocol for type hinting in your project then it will need to be guarded.

Type Guarding Protocol

Since typing.TYPE_CHECKING is always False at runtime we can use it.

There are two way to handle importing a protocol class. The first way is by importing annotations

from __future__ import annotations
import uno
from com.sun.star.sheet.SolverConstraintOperator import SolverConstraintOperatorProto
# ...

def solve_operation(value: int, x: SolverConstraintOperatorProto) -> int:
    ...

Note when using annotations the cast to protocol must be wrapped in a string.

from typing import cast
from com.sun.star.sheet.SolverConstraintOperator import SolverConstraintOperatorProto
from ooo.dyn.sheet.solver_constraint_operator import SolverConstraintOperator
# ...

# SolverConstraintOperatorProto must be wrapped in a string
# if it has not been assigned to object at runtime.
solve_operation(
    11, cast("SolverConstraintOperatorProto", SolverConstraintOperator.BINARY)
)

The other way is to assign the protocol class as an object at runtime.

from typing import TYPE_CHECKING
import uno
from com.sun.star.sheet.SolverConstraintOperator import SolverConstraintOperatorProto

if TYPE_CHECKING:
    # While writing code we have the advantages of protocol
    from com.sun.star.sheet.SolverConstraintOperator import SolverConstraintOperatorProto
else:
    # code is executing. Now protocol is an object and basically ignored
    SolverConstraintOperatorProto = object

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

types_unopy-2.0.0.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

types_unopy-2.0.0-py3-none-any.whl (5.2 MB view details)

Uploaded Python 3

File details

Details for the file types_unopy-2.0.0.tar.gz.

File metadata

  • Download URL: types_unopy-2.0.0.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.5 Linux/6.8.0-41-generic

File hashes

Hashes for types_unopy-2.0.0.tar.gz
Algorithm Hash digest
SHA256 b3bacd0cc95d63efd5c8db052d72c7edd4756acbd90761d88ea51cf171f7223d
MD5 4f750aea74094a9e9891593bc9ac25bf
BLAKE2b-256 29c935d81c34d98239178c246cc095f5d62168498be6c51f6020138aa572ea6a

See more details on using hashes here.

File details

Details for the file types_unopy-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: types_unopy-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.5 Linux/6.8.0-41-generic

File hashes

Hashes for types_unopy-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 748673362338851088d7ab88b51132c5f42a4e56600a55c27448b44327b023bf
MD5 d47b988b2535750dd3ac726d282988ef
BLAKE2b-256 3d510717c18e667d53cff55b30af07ccd00f4bdbce9ab278fcc57724d340f1f3

See more details on using hashes here.

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