@@ -478,8 +478,17 @@ class PythonStubGenerator {
478478 }
479479 break ;
480480 }
481- stub << " def " << name << " Length(self) -> int: ...\n " ;
482- stub << " def " << name << " IsNone(self) -> bool: ...\n " ;
481+ const bool is_array = field_type.base_type == BASE_TYPE_ARRAY ;
482+ if (is_array) {
483+ stub << " @staticmethod\n " ;
484+ }
485+ stub << " def " << name << " Length(" << (is_array ? " " : " self" )
486+ << " ) -> int: ...\n " ;
487+ if (is_array) {
488+ stub << " @staticmethod\n " ;
489+ }
490+ stub << " def " << name << " IsNone(" << (is_array ? " " : " self" )
491+ << " ) -> bool: ...\n " ;
483492 break ;
484493 }
485494 case BASE_TYPE_UNION : {
@@ -805,13 +814,15 @@ class PythonGenerator : public BaseGenerator {
805814 std::string* code_ptr) const {
806815 auto & code = *code_ptr;
807816
808- GenReceiver (struct_def, code_ptr);
809- code += namer_.Method (field) + " Length(self)" ;
817+ const bool is_array = IsArray (field.value .type );
818+ GenReceiver (struct_def, code_ptr, is_array);
819+ code += namer_.Method (field) + " Length(" +
820+ (is_array ? std::string () : " self" ) + " )" ;
810821 if (parser_.opts .python_typing ) {
811822 code += " -> int" ;
812823 }
813824 code += " :" ;
814- if (!IsArray (field. value . type ) ) {
825+ if (!is_array ) {
815826 code += OffsetPrefix (field, false );
816827 code += GenIndents (3 ) + " return self._tab.VectorLen(o)" ;
817828 code += GenIndents (2 ) + " return 0\n\n " ;
@@ -826,13 +837,15 @@ class PythonGenerator : public BaseGenerator {
826837 std::string* code_ptr) const {
827838 auto & code = *code_ptr;
828839
829- GenReceiver (struct_def, code_ptr);
830- code += namer_.Method (field) + " IsNone(self)" ;
840+ const bool is_array = IsArray (field.value .type );
841+ GenReceiver (struct_def, code_ptr, is_array);
842+ code += namer_.Method (field) + " IsNone(" +
843+ (is_array ? std::string () : " self" ) + " )" ;
831844 if (parser_.opts .python_typing ) {
832845 code += " -> bool" ;
833846 }
834847 code += " :" ;
835- if (!IsArray (field. value . type ) ) {
848+ if (!is_array ) {
836849 code += GenIndents (2 ) +
837850 " o = flatbuffers.number_types.UOffsetTFlags.py_type" +
838851 " (self._tab.Offset(" + NumToString (field.value .offset ) + " ))" ;
@@ -1599,9 +1612,13 @@ class PythonGenerator : public BaseGenerator {
15991612 }
16001613
16011614 // Generate the receiver for function signatures.
1602- void GenReceiver (const StructDef& struct_def, std::string* code_ptr) const {
1615+ void GenReceiver (const StructDef& struct_def, std::string* code_ptr,
1616+ bool is_static = false ) const {
16031617 auto & code = *code_ptr;
16041618 code += Indent + " # " + namer_.Type (struct_def) + " \n " ;
1619+ if (is_static) {
1620+ code += Indent + " @staticmethod\n " ;
1621+ }
16051622 code += Indent + " def " ;
16061623 }
16071624
0 commit comments