Package: types-unopy
This project allow typings for the fullLibreOffice APIWHYWorking withLibreOffice APIin a modern code editor such asVisual Studio Codethere is not type support forLibreOffice APIThis project solves that Issue.VERSIONThis package is for Version7.4+ ofLibreOffice 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 ofLibreOffice API Typingswill work fine with other versions of LibreOffice. This a typing package so not much can go wrong in other versions.InstallationPIPtypes-unopyonPyPI$pipinstalltypes-unopyFor version7.3(or less) of LibreOffice.$pipinstall"types-unopy<1.0"CONDAtypes-unopyonAnaconda$condainstall-cconda-forgetypes-unopyFor version7.3(or less) of LibreOffice.$condainstall-cconda-forge"types-unopy<1.0"RelatedTypes-ScriptForge leveragestypes-unopy. By installing
Types-ScriptForge into your project you will also automatically installtypes-unopy.USAGENot all object inLibreOffice APIcan be directly imported.Any UNO object that is aservicecannot be imported at runtime.For instance if you need to importSheetCellRangeso it can be used
as type the following will fail at runtime.>>>fromcom.sun.star.sheetimportSheetCellRangeImportError:Nomodulenamed'com'(or'com.sun.star.sheet.SheetCellRange'isunknown)The solution is to useTYPE_CHECKING.>>>from__future__importannotations>>>fromtypingimportTYPE_CHECKING>>>ifTYPE_CHECKING:...fromcom.sun.star.sheetimportSheetCellRange...Anything imported in theTYPE_CHECKINGblock will not be available at runtime.
For this reason types inside theTYPE_CHECKINGmust be wrapped in quotesORfrom __future__ import annotationsmust be the first line of the module.Example of wrapping type in quotes.defdo_work(range:'SheetCellRange')->None:...Known IssuesEnumsThere is no enum classes in API only enum members.To access the enum members they must be imported directly.For example to importcom.sun.star.beans.PropertyState.DIRECT_VALUEIf you need the behavior of regular Enum Classes consider usingooouno>>>fromcom.sun.star.beansimportPropertyStateImportError:Nomodulenamed'com'(or'com.sun.star.beans.PropertyState'isunknown>>>>>>fromcom.sun.star.beans.PropertyStateimportDIRECT_VALUE>>>DIRECT_VALUE.value'DIRECT_VALUE'>>>>>>type(DIRECT_VALUE)<class'uno.Enum'>DemoSpecial CasesImportErrorBy default anImportErroris raised when importing formcom.sun.starat runtime.
This is by design as the import error triggersunoto search LibreOffice API for actual import;
Otherwise,com.sun.staris seen a namespace import andunois ignored.In some cases theImportErrormay need to be suppressed.SuppressingImportErroris accomplished by adding"ooouno_ignore_import_error"to environment and setting it to"True">>>importos>>>os.environ["ooouno_ignore_import_error"]="True"# must be stringWhen building withSphinxandautodocit may be necessary to exclude uno related imports.
This can be accomplished using theautodoc_mock_importsoption.# docs conf.pyautodoc_mock_imports=['uno','unohelper','com']For a reference seeooo-dev-toolsconf.py.Enum ProtocolsAs mentioned above there are no enum classes in API only enum members.For this reason this package implements protocols for enums.fromcom.sun.star.beans.PropertyStateimportDIRECT_VALUE# DIRECT_VALUE is a type of PropertyStateProtoThe implemented protocol forPropertyStateis as follows:classPropertyStateProto(Protocol):"""Protocol for PropertyState"""@propertydeftypeName(self)->Literal["com.sun.star.beans.PropertyState"]:...value:AnyAMBIGUOUS_VALUE:PropertyStateProtoDEFAULT_VALUE:PropertyStateProtoDIRECT_VALUE:PropertyStateProtoImplemented methods such ascom.sun.star.beans.PropertyState.XPropertyState.getPropertyState()return a protocol, in this casePropertyStateProto.If you need to import a protocol for type hinting in your project then it will need to be guarded.Type Guarding ProtocolSincetyping.TYPE_CHECKINGis alwaysFalseat runtime we can use it.There are two way to handle importing a protocol class.
The first way is by importingannotationsfrom__future__importannotationsimportunofromcom.sun.star.sheet.SolverConstraintOperatorimportSolverConstraintOperatorProto# ...defsolve_operation(value:int,x:SolverConstraintOperatorProto)->int:...Note when usingannotationsthecastto protocol must be wrapped in a string.fromtypingimportcastfromcom.sun.star.sheet.SolverConstraintOperatorimportSolverConstraintOperatorProtofromooo.dyn.sheet.solver_constraint_operatorimportSolverConstraintOperator# ...# 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.fromtypingimportTYPE_CHECKINGimportunofromcom.sun.star.sheet.SolverConstraintOperatorimportSolverConstraintOperatorProtoifTYPE_CHECKING:# While writing code we have the advantages of protocolfromcom.sun.star.sheet.SolverConstraintOperatorimportSolverConstraintOperatorProtoelse:# code is executing. Now protocol is an object and basically ignoredSolverConstraintOperatorProto=objectRelated ProjectsOOO Development ToolsooounoScriptForge TypingsAccess2base TypingsLibreOffice Python UNO ExamplesLibreOffice UNO TypingsLibreOffice Developer SearchOOO UNO TEMPLATE