# ----------------------------------------------------------------------------# SymForce - Copyright 2022, Skydio, Inc.# This source code is under the Apache 2.0 license found in the LICENSE file.# ----------------------------------------------------------------------------from__future__importannotationsimporttypingasTimportpybind11_stubgenfrompybind11_stubgen.structsimportClassfrompybind11_stubgen.structsimportDocstringfrompybind11_stubgen.structsimportFieldfrompybind11_stubgen.structsimportIdentifierfrompybind11_stubgen.structsimportImportfrompybind11_stubgen.structsimportInvalidExpressionfrompybind11_stubgen.structsimportQualifiedNamefrompybind11_stubgen.structsimportResolvedTypefrompybind11_stubgen.structsimportValue
[docs]classFixMissingImports(pybind11_stubgen.parser.mixins.fix.FixMissingImports):def_add_import(self,name:QualifiedName)->None:iflen(name)==0:returnifname[0]==Identifier("lcmtypes"):self.__extra_imports.add(Import(name=None,origin=name.parent))returnsuper()._add_import(name)# NOTE(aaron): Fixed in https://github.com/sizmailov/pybind11-stubgen/pull/263
[docs]defpatch_current_module_prefix()->None:""" Fix use of the current module in nested types Could upstream """defparse_annotation_str(self:pybind11_stubgen.parser.mixins.fix.FixCurrentModulePrefixInTypeNames,annotation_str:str,)->ResolvedType|InvalidExpression|Value:result=super(# type: ignore[safe-super]pybind11_stubgen.parser.mixins.fix.FixCurrentModulePrefixInTypeNames,self).parse_annotation_str(annotation_str)defhandle_annotation(annotation:ResolvedType|InvalidExpression|Value)->None:ifisinstance(annotation,ResolvedType):annotation.name=self._strip_current_module(annotation.name)ifannotation.parametersisnotNone:forpinannotation.parameters:handle_annotation(p)handle_annotation(result)returnresultpybind11_stubgen.parser.mixins.fix.FixCurrentModulePrefixInTypeNames.parse_annotation_str=(# type: ignore[method-assign]parse_annotation_str)
[docs]defpatch_handle_docstring()->None:""" Patch BaseParser.handle_docstring to always strip empty lines from the start or end of docstrings """defhandle_docstring(self:pybind11_stubgen.IParser,path:QualifiedName,value:T.Any)->T.Optional[Docstring]:ifisinstance(value,str):assertisinstance(self,pybind11_stubgen.parser.mixins.parse.ExtractSignaturesFromPybind11Docstrings)returnself._strip_empty_lines(value.splitlines())returnNonepybind11_stubgen.parser.mixins.parse.BaseParser.handle_docstring=handle_docstring# type: ignore[method-assign]
[docs]defpatch_fix_missing_none_hash_field_annotation()->None:""" See https://github.com/sizmailov/pybind11-stubgen/pull/236 """defhandle_field(self:pybind11_stubgen.parser.mixins.fix.FixMissingNoneHashFieldAnnotation,path:QualifiedName,field:T.Any,)->T.Optional[Field]:result=super(# type: ignore[safe-super]pybind11_stubgen.parser.mixins.fix.FixMissingNoneHashFieldAnnotation,self).handle_field(path,field)ifresultisNone:returnNoneiffieldisNoneandpath[-1]=="__hash__":result.attribute.annotation=self.parse_annotation_str("typing.ClassVar[typing.Any]")returnresultpybind11_stubgen.parser.mixins.fix.FixMissingNoneHashFieldAnnotation.handle_field=handle_field# type: ignore[method-assign]
[docs]defpatch_remove_parameters()->None:""" Fix NumpyArrayRemoveParameters to work with pybind 3.x and deduplicate overloads """pybind11_stubgen.parser.mixins.fix.FixNumpyArrayRemoveParameters=FixNumpyArrayRemoveParameters# type: ignore[misc,assignment]pybind11_stubgen.FixNumpyArrayRemoveParameters=FixNumpyArrayRemoveParameters# type: ignore[misc,assignment]