pinecone.core.grpc.protos.vector_column_service_pb2
Generated protocol buffer code.
1# -*- coding: utf-8 -*- 2# Generated by the protocol buffer compiler. DO NOT EDIT! 3# source: vector_column_service.proto 4"""Generated protocol buffer code.""" 5from google.protobuf import descriptor as _descriptor 6from google.protobuf import message as _message 7from google.protobuf import reflection as _reflection 8from google.protobuf import symbol_database as _symbol_database 9 10# @@protoc_insertion_point(imports) 11 12_sym_db = _symbol_database.Default() 13 14 15from google.protobuf import struct_pb2 as google_dot_protobuf_dot_struct__pb2 16 17 18DESCRIPTOR = _descriptor.FileDescriptor( 19 name="vector_column_service.proto", 20 package="pinecone_columnar", 21 syntax="proto3", 22 serialized_options=b"\n\021io.pinecone.protoZ+github.com/pinecone-io/go-pinecone/pinecone", 23 create_key=_descriptor._internal_create_key, 24 serialized_pb=b'\n\x1bvector_column_service.proto\x12\x11pinecone_columnar\x1a\x1cgoogle/protobuf/struct.proto"K\n\x07NdArray\x12\x0e\n\x06\x62uffer\x18\x01 \x01(\x0c\x12\r\n\x05shape\x18\x02 \x03(\r\x12\r\n\x05\x64type\x18\x03 \x01(\t\x12\x12\n\ncompressed\x18\x04 \x01(\x08"\xbc\x01\n\rScoredResults\x12\'\n\x03ids\x18\x01 \x01(\x0b\x32\x1a.pinecone_columnar.NdArray\x12*\n\x06scores\x18\x02 \x01(\x0b\x32\x1a.pinecone_columnar.NdArray\x12(\n\x04\x64\x61ta\x18\x03 \x01(\x0b\x32\x1a.pinecone_columnar.NdArray\x12,\n\x08metadata\x18\x04 \x01(\x0b\x32\x1a.pinecone_columnar.NdArray"\x84\x01\n\rUpsertRequest\x12\x11\n\tnamespace\x18\x01 \x01(\t\x12\x0b\n\x03ids\x18\x02 \x03(\t\x12(\n\x04\x64\x61ta\x18\x03 \x01(\x0b\x32\x1a.pinecone_columnar.NdArray\x12)\n\x08metadata\x18\x04 \x03(\x0b\x32\x17.google.protobuf.Struct"(\n\x0eUpsertResponse\x12\x16\n\x0eupserted_count\x18\x01 \x01(\r"C\n\rDeleteRequest\x12\x11\n\tnamespace\x18\x01 \x01(\t\x12\x0b\n\x03ids\x18\x02 \x03(\t\x12\x12\n\ndelete_all\x18\x03 \x01(\x08"\x10\n\x0e\x44\x65leteResponse".\n\x0c\x46\x65tchRequest\x12\x11\n\tnamespace\x18\x01 \x01(\t\x12\x0b\n\x03ids\x18\x02 \x03(\t"\x87\x01\n\rFetchResponse\x12\x11\n\tnamespace\x18\x01 \x01(\t\x12\x0b\n\x03ids\x18\x02 \x03(\t\x12+\n\x07vectors\x18\x03 \x03(\x0b\x32\x1a.pinecone_columnar.NdArray\x12)\n\x08metadata\x18\x04 \x03(\x0b\x32\x17.google.protobuf.Struct"\xa1\x02\n\x0cQueryRequest\x12\x11\n\tnamespace\x18\x01 \x01(\t\x12\x1b\n\x13namespace_overrides\x18\x06 \x03(\t\x12\r\n\x05top_k\x18\x02 \x01(\r\x12\x17\n\x0ftop_k_overrides\x18\x05 \x03(\r\x12\'\n\x06\x66ilter\x18\x07 \x01(\x0b\x32\x17.google.protobuf.Struct\x12\x31\n\x10\x66ilter_overrides\x18\x08 \x03(\x0b\x32\x17.google.protobuf.Struct\x12\x16\n\x0einclude_values\x18\x03 \x01(\x08\x12\x18\n\x10include_metadata\x18\t \x01(\x08\x12+\n\x07queries\x18\x04 \x01(\x0b\x32\x1a.pinecone_columnar.NdArray"B\n\rQueryResponse\x12\x31\n\x07matches\x18\x01 \x03(\x0b\x32 .pinecone_columnar.ScoredResults"\x1b\n\x19\x44\x65scribeIndexStatsRequest"(\n\x10NamespaceSummary\x12\x14\n\x0cvector_count\x18\x01 \x01(\r"\xda\x01\n\x1a\x44\x65scribeIndexStatsResponse\x12Q\n\nnamespaces\x18\x01 \x03(\x0b\x32=.pinecone_columnar.DescribeIndexStatsResponse.NamespacesEntry\x12\x11\n\tdimension\x18\x02 \x01(\r\x1aV\n\x0fNamespacesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\x32\n\x05value\x18\x02 \x01(\x0b\x32#.pinecone_columnar.NamespaceSummary:\x02\x38\x01\x32\xc8\x03\n\x13VectorColumnService\x12O\n\x06Upsert\x12 .pinecone_columnar.UpsertRequest\x1a!.pinecone_columnar.UpsertResponse"\x00\x12O\n\x06\x44\x65lete\x12 .pinecone_columnar.DeleteRequest\x1a!.pinecone_columnar.DeleteResponse"\x00\x12L\n\x05\x46\x65tch\x12\x1f.pinecone_columnar.FetchRequest\x1a .pinecone_columnar.FetchResponse"\x00\x12L\n\x05Query\x12\x1f.pinecone_columnar.QueryRequest\x1a .pinecone_columnar.QueryResponse"\x00\x12s\n\x12\x44\x65scribeIndexStats\x12,.pinecone_columnar.DescribeIndexStatsRequest\x1a-.pinecone_columnar.DescribeIndexStatsResponse"\x00\x42@\n\x11io.pinecone.protoZ+github.com/pinecone-io/go-pinecone/pineconeb\x06proto3', 25 dependencies=[ 26 google_dot_protobuf_dot_struct__pb2.DESCRIPTOR, 27 ], 28) 29 30 31_NDARRAY = _descriptor.Descriptor( 32 name="NdArray", 33 full_name="pinecone_columnar.NdArray", 34 filename=None, 35 file=DESCRIPTOR, 36 containing_type=None, 37 create_key=_descriptor._internal_create_key, 38 fields=[ 39 _descriptor.FieldDescriptor( 40 name="buffer", 41 full_name="pinecone_columnar.NdArray.buffer", 42 index=0, 43 number=1, 44 type=12, 45 cpp_type=9, 46 label=1, 47 has_default_value=False, 48 default_value=b"", 49 message_type=None, 50 enum_type=None, 51 containing_type=None, 52 is_extension=False, 53 extension_scope=None, 54 serialized_options=None, 55 file=DESCRIPTOR, 56 create_key=_descriptor._internal_create_key, 57 ), 58 _descriptor.FieldDescriptor( 59 name="shape", 60 full_name="pinecone_columnar.NdArray.shape", 61 index=1, 62 number=2, 63 type=13, 64 cpp_type=3, 65 label=3, 66 has_default_value=False, 67 default_value=[], 68 message_type=None, 69 enum_type=None, 70 containing_type=None, 71 is_extension=False, 72 extension_scope=None, 73 serialized_options=None, 74 file=DESCRIPTOR, 75 create_key=_descriptor._internal_create_key, 76 ), 77 _descriptor.FieldDescriptor( 78 name="dtype", 79 full_name="pinecone_columnar.NdArray.dtype", 80 index=2, 81 number=3, 82 type=9, 83 cpp_type=9, 84 label=1, 85 has_default_value=False, 86 default_value=b"".decode("utf-8"), 87 message_type=None, 88 enum_type=None, 89 containing_type=None, 90 is_extension=False, 91 extension_scope=None, 92 serialized_options=None, 93 file=DESCRIPTOR, 94 create_key=_descriptor._internal_create_key, 95 ), 96 _descriptor.FieldDescriptor( 97 name="compressed", 98 full_name="pinecone_columnar.NdArray.compressed", 99 index=3, 100 number=4, 101 type=8, 102 cpp_type=7, 103 label=1, 104 has_default_value=False, 105 default_value=False, 106 message_type=None, 107 enum_type=None, 108 containing_type=None, 109 is_extension=False, 110 extension_scope=None, 111 serialized_options=None, 112 file=DESCRIPTOR, 113 create_key=_descriptor._internal_create_key, 114 ), 115 ], 116 extensions=[], 117 nested_types=[], 118 enum_types=[], 119 serialized_options=None, 120 is_extendable=False, 121 syntax="proto3", 122 extension_ranges=[], 123 oneofs=[], 124 serialized_start=80, 125 serialized_end=155, 126) 127 128 129_SCOREDRESULTS = _descriptor.Descriptor( 130 name="ScoredResults", 131 full_name="pinecone_columnar.ScoredResults", 132 filename=None, 133 file=DESCRIPTOR, 134 containing_type=None, 135 create_key=_descriptor._internal_create_key, 136 fields=[ 137 _descriptor.FieldDescriptor( 138 name="ids", 139 full_name="pinecone_columnar.ScoredResults.ids", 140 index=0, 141 number=1, 142 type=11, 143 cpp_type=10, 144 label=1, 145 has_default_value=False, 146 default_value=None, 147 message_type=None, 148 enum_type=None, 149 containing_type=None, 150 is_extension=False, 151 extension_scope=None, 152 serialized_options=None, 153 file=DESCRIPTOR, 154 create_key=_descriptor._internal_create_key, 155 ), 156 _descriptor.FieldDescriptor( 157 name="scores", 158 full_name="pinecone_columnar.ScoredResults.scores", 159 index=1, 160 number=2, 161 type=11, 162 cpp_type=10, 163 label=1, 164 has_default_value=False, 165 default_value=None, 166 message_type=None, 167 enum_type=None, 168 containing_type=None, 169 is_extension=False, 170 extension_scope=None, 171 serialized_options=None, 172 file=DESCRIPTOR, 173 create_key=_descriptor._internal_create_key, 174 ), 175 _descriptor.FieldDescriptor( 176 name="data", 177 full_name="pinecone_columnar.ScoredResults.data", 178 index=2, 179 number=3, 180 type=11, 181 cpp_type=10, 182 label=1, 183 has_default_value=False, 184 default_value=None, 185 message_type=None, 186 enum_type=None, 187 containing_type=None, 188 is_extension=False, 189 extension_scope=None, 190 serialized_options=None, 191 file=DESCRIPTOR, 192 create_key=_descriptor._internal_create_key, 193 ), 194 _descriptor.FieldDescriptor( 195 name="metadata", 196 full_name="pinecone_columnar.ScoredResults.metadata", 197 index=3, 198 number=4, 199 type=11, 200 cpp_type=10, 201 label=1, 202 has_default_value=False, 203 default_value=None, 204 message_type=None, 205 enum_type=None, 206 containing_type=None, 207 is_extension=False, 208 extension_scope=None, 209 serialized_options=None, 210 file=DESCRIPTOR, 211 create_key=_descriptor._internal_create_key, 212 ), 213 ], 214 extensions=[], 215 nested_types=[], 216 enum_types=[], 217 serialized_options=None, 218 is_extendable=False, 219 syntax="proto3", 220 extension_ranges=[], 221 oneofs=[], 222 serialized_start=158, 223 serialized_end=346, 224) 225 226 227_UPSERTREQUEST = _descriptor.Descriptor( 228 name="UpsertRequest", 229 full_name="pinecone_columnar.UpsertRequest", 230 filename=None, 231 file=DESCRIPTOR, 232 containing_type=None, 233 create_key=_descriptor._internal_create_key, 234 fields=[ 235 _descriptor.FieldDescriptor( 236 name="namespace", 237 full_name="pinecone_columnar.UpsertRequest.namespace", 238 index=0, 239 number=1, 240 type=9, 241 cpp_type=9, 242 label=1, 243 has_default_value=False, 244 default_value=b"".decode("utf-8"), 245 message_type=None, 246 enum_type=None, 247 containing_type=None, 248 is_extension=False, 249 extension_scope=None, 250 serialized_options=None, 251 file=DESCRIPTOR, 252 create_key=_descriptor._internal_create_key, 253 ), 254 _descriptor.FieldDescriptor( 255 name="ids", 256 full_name="pinecone_columnar.UpsertRequest.ids", 257 index=1, 258 number=2, 259 type=9, 260 cpp_type=9, 261 label=3, 262 has_default_value=False, 263 default_value=[], 264 message_type=None, 265 enum_type=None, 266 containing_type=None, 267 is_extension=False, 268 extension_scope=None, 269 serialized_options=None, 270 file=DESCRIPTOR, 271 create_key=_descriptor._internal_create_key, 272 ), 273 _descriptor.FieldDescriptor( 274 name="data", 275 full_name="pinecone_columnar.UpsertRequest.data", 276 index=2, 277 number=3, 278 type=11, 279 cpp_type=10, 280 label=1, 281 has_default_value=False, 282 default_value=None, 283 message_type=None, 284 enum_type=None, 285 containing_type=None, 286 is_extension=False, 287 extension_scope=None, 288 serialized_options=None, 289 file=DESCRIPTOR, 290 create_key=_descriptor._internal_create_key, 291 ), 292 _descriptor.FieldDescriptor( 293 name="metadata", 294 full_name="pinecone_columnar.UpsertRequest.metadata", 295 index=3, 296 number=4, 297 type=11, 298 cpp_type=10, 299 label=3, 300 has_default_value=False, 301 default_value=[], 302 message_type=None, 303 enum_type=None, 304 containing_type=None, 305 is_extension=False, 306 extension_scope=None, 307 serialized_options=None, 308 file=DESCRIPTOR, 309 create_key=_descriptor._internal_create_key, 310 ), 311 ], 312 extensions=[], 313 nested_types=[], 314 enum_types=[], 315 serialized_options=None, 316 is_extendable=False, 317 syntax="proto3", 318 extension_ranges=[], 319 oneofs=[], 320 serialized_start=349, 321 serialized_end=481, 322) 323 324 325_UPSERTRESPONSE = _descriptor.Descriptor( 326 name="UpsertResponse", 327 full_name="pinecone_columnar.UpsertResponse", 328 filename=None, 329 file=DESCRIPTOR, 330 containing_type=None, 331 create_key=_descriptor._internal_create_key, 332 fields=[ 333 _descriptor.FieldDescriptor( 334 name="upserted_count", 335 full_name="pinecone_columnar.UpsertResponse.upserted_count", 336 index=0, 337 number=1, 338 type=13, 339 cpp_type=3, 340 label=1, 341 has_default_value=False, 342 default_value=0, 343 message_type=None, 344 enum_type=None, 345 containing_type=None, 346 is_extension=False, 347 extension_scope=None, 348 serialized_options=None, 349 file=DESCRIPTOR, 350 create_key=_descriptor._internal_create_key, 351 ), 352 ], 353 extensions=[], 354 nested_types=[], 355 enum_types=[], 356 serialized_options=None, 357 is_extendable=False, 358 syntax="proto3", 359 extension_ranges=[], 360 oneofs=[], 361 serialized_start=483, 362 serialized_end=523, 363) 364 365 366_DELETEREQUEST = _descriptor.Descriptor( 367 name="DeleteRequest", 368 full_name="pinecone_columnar.DeleteRequest", 369 filename=None, 370 file=DESCRIPTOR, 371 containing_type=None, 372 create_key=_descriptor._internal_create_key, 373 fields=[ 374 _descriptor.FieldDescriptor( 375 name="namespace", 376 full_name="pinecone_columnar.DeleteRequest.namespace", 377 index=0, 378 number=1, 379 type=9, 380 cpp_type=9, 381 label=1, 382 has_default_value=False, 383 default_value=b"".decode("utf-8"), 384 message_type=None, 385 enum_type=None, 386 containing_type=None, 387 is_extension=False, 388 extension_scope=None, 389 serialized_options=None, 390 file=DESCRIPTOR, 391 create_key=_descriptor._internal_create_key, 392 ), 393 _descriptor.FieldDescriptor( 394 name="ids", 395 full_name="pinecone_columnar.DeleteRequest.ids", 396 index=1, 397 number=2, 398 type=9, 399 cpp_type=9, 400 label=3, 401 has_default_value=False, 402 default_value=[], 403 message_type=None, 404 enum_type=None, 405 containing_type=None, 406 is_extension=False, 407 extension_scope=None, 408 serialized_options=None, 409 file=DESCRIPTOR, 410 create_key=_descriptor._internal_create_key, 411 ), 412 _descriptor.FieldDescriptor( 413 name="delete_all", 414 full_name="pinecone_columnar.DeleteRequest.delete_all", 415 index=2, 416 number=3, 417 type=8, 418 cpp_type=7, 419 label=1, 420 has_default_value=False, 421 default_value=False, 422 message_type=None, 423 enum_type=None, 424 containing_type=None, 425 is_extension=False, 426 extension_scope=None, 427 serialized_options=None, 428 file=DESCRIPTOR, 429 create_key=_descriptor._internal_create_key, 430 ), 431 ], 432 extensions=[], 433 nested_types=[], 434 enum_types=[], 435 serialized_options=None, 436 is_extendable=False, 437 syntax="proto3", 438 extension_ranges=[], 439 oneofs=[], 440 serialized_start=525, 441 serialized_end=592, 442) 443 444 445_DELETERESPONSE = _descriptor.Descriptor( 446 name="DeleteResponse", 447 full_name="pinecone_columnar.DeleteResponse", 448 filename=None, 449 file=DESCRIPTOR, 450 containing_type=None, 451 create_key=_descriptor._internal_create_key, 452 fields=[], 453 extensions=[], 454 nested_types=[], 455 enum_types=[], 456 serialized_options=None, 457 is_extendable=False, 458 syntax="proto3", 459 extension_ranges=[], 460 oneofs=[], 461 serialized_start=594, 462 serialized_end=610, 463) 464 465 466_FETCHREQUEST = _descriptor.Descriptor( 467 name="FetchRequest", 468 full_name="pinecone_columnar.FetchRequest", 469 filename=None, 470 file=DESCRIPTOR, 471 containing_type=None, 472 create_key=_descriptor._internal_create_key, 473 fields=[ 474 _descriptor.FieldDescriptor( 475 name="namespace", 476 full_name="pinecone_columnar.FetchRequest.namespace", 477 index=0, 478 number=1, 479 type=9, 480 cpp_type=9, 481 label=1, 482 has_default_value=False, 483 default_value=b"".decode("utf-8"), 484 message_type=None, 485 enum_type=None, 486 containing_type=None, 487 is_extension=False, 488 extension_scope=None, 489 serialized_options=None, 490 file=DESCRIPTOR, 491 create_key=_descriptor._internal_create_key, 492 ), 493 _descriptor.FieldDescriptor( 494 name="ids", 495 full_name="pinecone_columnar.FetchRequest.ids", 496 index=1, 497 number=2, 498 type=9, 499 cpp_type=9, 500 label=3, 501 has_default_value=False, 502 default_value=[], 503 message_type=None, 504 enum_type=None, 505 containing_type=None, 506 is_extension=False, 507 extension_scope=None, 508 serialized_options=None, 509 file=DESCRIPTOR, 510 create_key=_descriptor._internal_create_key, 511 ), 512 ], 513 extensions=[], 514 nested_types=[], 515 enum_types=[], 516 serialized_options=None, 517 is_extendable=False, 518 syntax="proto3", 519 extension_ranges=[], 520 oneofs=[], 521 serialized_start=612, 522 serialized_end=658, 523) 524 525 526_FETCHRESPONSE = _descriptor.Descriptor( 527 name="FetchResponse", 528 full_name="pinecone_columnar.FetchResponse", 529 filename=None, 530 file=DESCRIPTOR, 531 containing_type=None, 532 create_key=_descriptor._internal_create_key, 533 fields=[ 534 _descriptor.FieldDescriptor( 535 name="namespace", 536 full_name="pinecone_columnar.FetchResponse.namespace", 537 index=0, 538 number=1, 539 type=9, 540 cpp_type=9, 541 label=1, 542 has_default_value=False, 543 default_value=b"".decode("utf-8"), 544 message_type=None, 545 enum_type=None, 546 containing_type=None, 547 is_extension=False, 548 extension_scope=None, 549 serialized_options=None, 550 file=DESCRIPTOR, 551 create_key=_descriptor._internal_create_key, 552 ), 553 _descriptor.FieldDescriptor( 554 name="ids", 555 full_name="pinecone_columnar.FetchResponse.ids", 556 index=1, 557 number=2, 558 type=9, 559 cpp_type=9, 560 label=3, 561 has_default_value=False, 562 default_value=[], 563 message_type=None, 564 enum_type=None, 565 containing_type=None, 566 is_extension=False, 567 extension_scope=None, 568 serialized_options=None, 569 file=DESCRIPTOR, 570 create_key=_descriptor._internal_create_key, 571 ), 572 _descriptor.FieldDescriptor( 573 name="vectors", 574 full_name="pinecone_columnar.FetchResponse.vectors", 575 index=2, 576 number=3, 577 type=11, 578 cpp_type=10, 579 label=3, 580 has_default_value=False, 581 default_value=[], 582 message_type=None, 583 enum_type=None, 584 containing_type=None, 585 is_extension=False, 586 extension_scope=None, 587 serialized_options=None, 588 file=DESCRIPTOR, 589 create_key=_descriptor._internal_create_key, 590 ), 591 _descriptor.FieldDescriptor( 592 name="metadata", 593 full_name="pinecone_columnar.FetchResponse.metadata", 594 index=3, 595 number=4, 596 type=11, 597 cpp_type=10, 598 label=3, 599 has_default_value=False, 600 default_value=[], 601 message_type=None, 602 enum_type=None, 603 containing_type=None, 604 is_extension=False, 605 extension_scope=None, 606 serialized_options=None, 607 file=DESCRIPTOR, 608 create_key=_descriptor._internal_create_key, 609 ), 610 ], 611 extensions=[], 612 nested_types=[], 613 enum_types=[], 614 serialized_options=None, 615 is_extendable=False, 616 syntax="proto3", 617 extension_ranges=[], 618 oneofs=[], 619 serialized_start=661, 620 serialized_end=796, 621) 622 623 624_QUERYREQUEST = _descriptor.Descriptor( 625 name="QueryRequest", 626 full_name="pinecone_columnar.QueryRequest", 627 filename=None, 628 file=DESCRIPTOR, 629 containing_type=None, 630 create_key=_descriptor._internal_create_key, 631 fields=[ 632 _descriptor.FieldDescriptor( 633 name="namespace", 634 full_name="pinecone_columnar.QueryRequest.namespace", 635 index=0, 636 number=1, 637 type=9, 638 cpp_type=9, 639 label=1, 640 has_default_value=False, 641 default_value=b"".decode("utf-8"), 642 message_type=None, 643 enum_type=None, 644 containing_type=None, 645 is_extension=False, 646 extension_scope=None, 647 serialized_options=None, 648 file=DESCRIPTOR, 649 create_key=_descriptor._internal_create_key, 650 ), 651 _descriptor.FieldDescriptor( 652 name="namespace_overrides", 653 full_name="pinecone_columnar.QueryRequest.namespace_overrides", 654 index=1, 655 number=6, 656 type=9, 657 cpp_type=9, 658 label=3, 659 has_default_value=False, 660 default_value=[], 661 message_type=None, 662 enum_type=None, 663 containing_type=None, 664 is_extension=False, 665 extension_scope=None, 666 serialized_options=None, 667 file=DESCRIPTOR, 668 create_key=_descriptor._internal_create_key, 669 ), 670 _descriptor.FieldDescriptor( 671 name="top_k", 672 full_name="pinecone_columnar.QueryRequest.top_k", 673 index=2, 674 number=2, 675 type=13, 676 cpp_type=3, 677 label=1, 678 has_default_value=False, 679 default_value=0, 680 message_type=None, 681 enum_type=None, 682 containing_type=None, 683 is_extension=False, 684 extension_scope=None, 685 serialized_options=None, 686 file=DESCRIPTOR, 687 create_key=_descriptor._internal_create_key, 688 ), 689 _descriptor.FieldDescriptor( 690 name="top_k_overrides", 691 full_name="pinecone_columnar.QueryRequest.top_k_overrides", 692 index=3, 693 number=5, 694 type=13, 695 cpp_type=3, 696 label=3, 697 has_default_value=False, 698 default_value=[], 699 message_type=None, 700 enum_type=None, 701 containing_type=None, 702 is_extension=False, 703 extension_scope=None, 704 serialized_options=None, 705 file=DESCRIPTOR, 706 create_key=_descriptor._internal_create_key, 707 ), 708 _descriptor.FieldDescriptor( 709 name="filter", 710 full_name="pinecone_columnar.QueryRequest.filter", 711 index=4, 712 number=7, 713 type=11, 714 cpp_type=10, 715 label=1, 716 has_default_value=False, 717 default_value=None, 718 message_type=None, 719 enum_type=None, 720 containing_type=None, 721 is_extension=False, 722 extension_scope=None, 723 serialized_options=None, 724 file=DESCRIPTOR, 725 create_key=_descriptor._internal_create_key, 726 ), 727 _descriptor.FieldDescriptor( 728 name="filter_overrides", 729 full_name="pinecone_columnar.QueryRequest.filter_overrides", 730 index=5, 731 number=8, 732 type=11, 733 cpp_type=10, 734 label=3, 735 has_default_value=False, 736 default_value=[], 737 message_type=None, 738 enum_type=None, 739 containing_type=None, 740 is_extension=False, 741 extension_scope=None, 742 serialized_options=None, 743 file=DESCRIPTOR, 744 create_key=_descriptor._internal_create_key, 745 ), 746 _descriptor.FieldDescriptor( 747 name="include_values", 748 full_name="pinecone_columnar.QueryRequest.include_values", 749 index=6, 750 number=3, 751 type=8, 752 cpp_type=7, 753 label=1, 754 has_default_value=False, 755 default_value=False, 756 message_type=None, 757 enum_type=None, 758 containing_type=None, 759 is_extension=False, 760 extension_scope=None, 761 serialized_options=None, 762 file=DESCRIPTOR, 763 create_key=_descriptor._internal_create_key, 764 ), 765 _descriptor.FieldDescriptor( 766 name="include_metadata", 767 full_name="pinecone_columnar.QueryRequest.include_metadata", 768 index=7, 769 number=9, 770 type=8, 771 cpp_type=7, 772 label=1, 773 has_default_value=False, 774 default_value=False, 775 message_type=None, 776 enum_type=None, 777 containing_type=None, 778 is_extension=False, 779 extension_scope=None, 780 serialized_options=None, 781 file=DESCRIPTOR, 782 create_key=_descriptor._internal_create_key, 783 ), 784 _descriptor.FieldDescriptor( 785 name="queries", 786 full_name="pinecone_columnar.QueryRequest.queries", 787 index=8, 788 number=4, 789 type=11, 790 cpp_type=10, 791 label=1, 792 has_default_value=False, 793 default_value=None, 794 message_type=None, 795 enum_type=None, 796 containing_type=None, 797 is_extension=False, 798 extension_scope=None, 799 serialized_options=None, 800 file=DESCRIPTOR, 801 create_key=_descriptor._internal_create_key, 802 ), 803 ], 804 extensions=[], 805 nested_types=[], 806 enum_types=[], 807 serialized_options=None, 808 is_extendable=False, 809 syntax="proto3", 810 extension_ranges=[], 811 oneofs=[], 812 serialized_start=799, 813 serialized_end=1088, 814) 815 816 817_QUERYRESPONSE = _descriptor.Descriptor( 818 name="QueryResponse", 819 full_name="pinecone_columnar.QueryResponse", 820 filename=None, 821 file=DESCRIPTOR, 822 containing_type=None, 823 create_key=_descriptor._internal_create_key, 824 fields=[ 825 _descriptor.FieldDescriptor( 826 name="matches", 827 full_name="pinecone_columnar.QueryResponse.matches", 828 index=0, 829 number=1, 830 type=11, 831 cpp_type=10, 832 label=3, 833 has_default_value=False, 834 default_value=[], 835 message_type=None, 836 enum_type=None, 837 containing_type=None, 838 is_extension=False, 839 extension_scope=None, 840 serialized_options=None, 841 file=DESCRIPTOR, 842 create_key=_descriptor._internal_create_key, 843 ), 844 ], 845 extensions=[], 846 nested_types=[], 847 enum_types=[], 848 serialized_options=None, 849 is_extendable=False, 850 syntax="proto3", 851 extension_ranges=[], 852 oneofs=[], 853 serialized_start=1090, 854 serialized_end=1156, 855) 856 857 858_DESCRIBEINDEXSTATSREQUEST = _descriptor.Descriptor( 859 name="DescribeIndexStatsRequest", 860 full_name="pinecone_columnar.DescribeIndexStatsRequest", 861 filename=None, 862 file=DESCRIPTOR, 863 containing_type=None, 864 create_key=_descriptor._internal_create_key, 865 fields=[], 866 extensions=[], 867 nested_types=[], 868 enum_types=[], 869 serialized_options=None, 870 is_extendable=False, 871 syntax="proto3", 872 extension_ranges=[], 873 oneofs=[], 874 serialized_start=1158, 875 serialized_end=1185, 876) 877 878 879_NAMESPACESUMMARY = _descriptor.Descriptor( 880 name="NamespaceSummary", 881 full_name="pinecone_columnar.NamespaceSummary", 882 filename=None, 883 file=DESCRIPTOR, 884 containing_type=None, 885 create_key=_descriptor._internal_create_key, 886 fields=[ 887 _descriptor.FieldDescriptor( 888 name="vector_count", 889 full_name="pinecone_columnar.NamespaceSummary.vector_count", 890 index=0, 891 number=1, 892 type=13, 893 cpp_type=3, 894 label=1, 895 has_default_value=False, 896 default_value=0, 897 message_type=None, 898 enum_type=None, 899 containing_type=None, 900 is_extension=False, 901 extension_scope=None, 902 serialized_options=None, 903 file=DESCRIPTOR, 904 create_key=_descriptor._internal_create_key, 905 ), 906 ], 907 extensions=[], 908 nested_types=[], 909 enum_types=[], 910 serialized_options=None, 911 is_extendable=False, 912 syntax="proto3", 913 extension_ranges=[], 914 oneofs=[], 915 serialized_start=1187, 916 serialized_end=1227, 917) 918 919 920_DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY = _descriptor.Descriptor( 921 name="NamespacesEntry", 922 full_name="pinecone_columnar.DescribeIndexStatsResponse.NamespacesEntry", 923 filename=None, 924 file=DESCRIPTOR, 925 containing_type=None, 926 create_key=_descriptor._internal_create_key, 927 fields=[ 928 _descriptor.FieldDescriptor( 929 name="key", 930 full_name="pinecone_columnar.DescribeIndexStatsResponse.NamespacesEntry.key", 931 index=0, 932 number=1, 933 type=9, 934 cpp_type=9, 935 label=1, 936 has_default_value=False, 937 default_value=b"".decode("utf-8"), 938 message_type=None, 939 enum_type=None, 940 containing_type=None, 941 is_extension=False, 942 extension_scope=None, 943 serialized_options=None, 944 file=DESCRIPTOR, 945 create_key=_descriptor._internal_create_key, 946 ), 947 _descriptor.FieldDescriptor( 948 name="value", 949 full_name="pinecone_columnar.DescribeIndexStatsResponse.NamespacesEntry.value", 950 index=1, 951 number=2, 952 type=11, 953 cpp_type=10, 954 label=1, 955 has_default_value=False, 956 default_value=None, 957 message_type=None, 958 enum_type=None, 959 containing_type=None, 960 is_extension=False, 961 extension_scope=None, 962 serialized_options=None, 963 file=DESCRIPTOR, 964 create_key=_descriptor._internal_create_key, 965 ), 966 ], 967 extensions=[], 968 nested_types=[], 969 enum_types=[], 970 serialized_options=b"8\001", 971 is_extendable=False, 972 syntax="proto3", 973 extension_ranges=[], 974 oneofs=[], 975 serialized_start=1362, 976 serialized_end=1448, 977) 978 979_DESCRIBEINDEXSTATSRESPONSE = _descriptor.Descriptor( 980 name="DescribeIndexStatsResponse", 981 full_name="pinecone_columnar.DescribeIndexStatsResponse", 982 filename=None, 983 file=DESCRIPTOR, 984 containing_type=None, 985 create_key=_descriptor._internal_create_key, 986 fields=[ 987 _descriptor.FieldDescriptor( 988 name="namespaces", 989 full_name="pinecone_columnar.DescribeIndexStatsResponse.namespaces", 990 index=0, 991 number=1, 992 type=11, 993 cpp_type=10, 994 label=3, 995 has_default_value=False, 996 default_value=[], 997 message_type=None, 998 enum_type=None, 999 containing_type=None, 1000 is_extension=False, 1001 extension_scope=None, 1002 serialized_options=None, 1003 file=DESCRIPTOR, 1004 create_key=_descriptor._internal_create_key, 1005 ), 1006 _descriptor.FieldDescriptor( 1007 name="dimension", 1008 full_name="pinecone_columnar.DescribeIndexStatsResponse.dimension", 1009 index=1, 1010 number=2, 1011 type=13, 1012 cpp_type=3, 1013 label=1, 1014 has_default_value=False, 1015 default_value=0, 1016 message_type=None, 1017 enum_type=None, 1018 containing_type=None, 1019 is_extension=False, 1020 extension_scope=None, 1021 serialized_options=None, 1022 file=DESCRIPTOR, 1023 create_key=_descriptor._internal_create_key, 1024 ), 1025 ], 1026 extensions=[], 1027 nested_types=[ 1028 _DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY, 1029 ], 1030 enum_types=[], 1031 serialized_options=None, 1032 is_extendable=False, 1033 syntax="proto3", 1034 extension_ranges=[], 1035 oneofs=[], 1036 serialized_start=1230, 1037 serialized_end=1448, 1038) 1039 1040_SCOREDRESULTS.fields_by_name["ids"].message_type = _NDARRAY 1041_SCOREDRESULTS.fields_by_name["scores"].message_type = _NDARRAY 1042_SCOREDRESULTS.fields_by_name["data"].message_type = _NDARRAY 1043_SCOREDRESULTS.fields_by_name["metadata"].message_type = _NDARRAY 1044_UPSERTREQUEST.fields_by_name["data"].message_type = _NDARRAY 1045_UPSERTREQUEST.fields_by_name["metadata"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1046_FETCHRESPONSE.fields_by_name["vectors"].message_type = _NDARRAY 1047_FETCHRESPONSE.fields_by_name["metadata"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1048_QUERYREQUEST.fields_by_name["filter"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1049_QUERYREQUEST.fields_by_name["filter_overrides"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1050_QUERYREQUEST.fields_by_name["queries"].message_type = _NDARRAY 1051_QUERYRESPONSE.fields_by_name["matches"].message_type = _SCOREDRESULTS 1052_DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY.fields_by_name["value"].message_type = _NAMESPACESUMMARY 1053_DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY.containing_type = _DESCRIBEINDEXSTATSRESPONSE 1054_DESCRIBEINDEXSTATSRESPONSE.fields_by_name["namespaces"].message_type = _DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY 1055DESCRIPTOR.message_types_by_name["NdArray"] = _NDARRAY 1056DESCRIPTOR.message_types_by_name["ScoredResults"] = _SCOREDRESULTS 1057DESCRIPTOR.message_types_by_name["UpsertRequest"] = _UPSERTREQUEST 1058DESCRIPTOR.message_types_by_name["UpsertResponse"] = _UPSERTRESPONSE 1059DESCRIPTOR.message_types_by_name["DeleteRequest"] = _DELETEREQUEST 1060DESCRIPTOR.message_types_by_name["DeleteResponse"] = _DELETERESPONSE 1061DESCRIPTOR.message_types_by_name["FetchRequest"] = _FETCHREQUEST 1062DESCRIPTOR.message_types_by_name["FetchResponse"] = _FETCHRESPONSE 1063DESCRIPTOR.message_types_by_name["QueryRequest"] = _QUERYREQUEST 1064DESCRIPTOR.message_types_by_name["QueryResponse"] = _QUERYRESPONSE 1065DESCRIPTOR.message_types_by_name["DescribeIndexStatsRequest"] = _DESCRIBEINDEXSTATSREQUEST 1066DESCRIPTOR.message_types_by_name["NamespaceSummary"] = _NAMESPACESUMMARY 1067DESCRIPTOR.message_types_by_name["DescribeIndexStatsResponse"] = _DESCRIBEINDEXSTATSRESPONSE 1068_sym_db.RegisterFileDescriptor(DESCRIPTOR) 1069 1070NdArray = _reflection.GeneratedProtocolMessageType( 1071 "NdArray", 1072 (_message.Message,), 1073 { 1074 "DESCRIPTOR": _NDARRAY, 1075 "__module__": "vector_column_service_pb2" 1076 # @@protoc_insertion_point(class_scope:pinecone_columnar.NdArray) 1077 }, 1078) 1079_sym_db.RegisterMessage(NdArray) 1080 1081ScoredResults = _reflection.GeneratedProtocolMessageType( 1082 "ScoredResults", 1083 (_message.Message,), 1084 { 1085 "DESCRIPTOR": _SCOREDRESULTS, 1086 "__module__": "vector_column_service_pb2" 1087 # @@protoc_insertion_point(class_scope:pinecone_columnar.ScoredResults) 1088 }, 1089) 1090_sym_db.RegisterMessage(ScoredResults) 1091 1092UpsertRequest = _reflection.GeneratedProtocolMessageType( 1093 "UpsertRequest", 1094 (_message.Message,), 1095 { 1096 "DESCRIPTOR": _UPSERTREQUEST, 1097 "__module__": "vector_column_service_pb2" 1098 # @@protoc_insertion_point(class_scope:pinecone_columnar.UpsertRequest) 1099 }, 1100) 1101_sym_db.RegisterMessage(UpsertRequest) 1102 1103UpsertResponse = _reflection.GeneratedProtocolMessageType( 1104 "UpsertResponse", 1105 (_message.Message,), 1106 { 1107 "DESCRIPTOR": _UPSERTRESPONSE, 1108 "__module__": "vector_column_service_pb2" 1109 # @@protoc_insertion_point(class_scope:pinecone_columnar.UpsertResponse) 1110 }, 1111) 1112_sym_db.RegisterMessage(UpsertResponse) 1113 1114DeleteRequest = _reflection.GeneratedProtocolMessageType( 1115 "DeleteRequest", 1116 (_message.Message,), 1117 { 1118 "DESCRIPTOR": _DELETEREQUEST, 1119 "__module__": "vector_column_service_pb2" 1120 # @@protoc_insertion_point(class_scope:pinecone_columnar.DeleteRequest) 1121 }, 1122) 1123_sym_db.RegisterMessage(DeleteRequest) 1124 1125DeleteResponse = _reflection.GeneratedProtocolMessageType( 1126 "DeleteResponse", 1127 (_message.Message,), 1128 { 1129 "DESCRIPTOR": _DELETERESPONSE, 1130 "__module__": "vector_column_service_pb2" 1131 # @@protoc_insertion_point(class_scope:pinecone_columnar.DeleteResponse) 1132 }, 1133) 1134_sym_db.RegisterMessage(DeleteResponse) 1135 1136FetchRequest = _reflection.GeneratedProtocolMessageType( 1137 "FetchRequest", 1138 (_message.Message,), 1139 { 1140 "DESCRIPTOR": _FETCHREQUEST, 1141 "__module__": "vector_column_service_pb2" 1142 # @@protoc_insertion_point(class_scope:pinecone_columnar.FetchRequest) 1143 }, 1144) 1145_sym_db.RegisterMessage(FetchRequest) 1146 1147FetchResponse = _reflection.GeneratedProtocolMessageType( 1148 "FetchResponse", 1149 (_message.Message,), 1150 { 1151 "DESCRIPTOR": _FETCHRESPONSE, 1152 "__module__": "vector_column_service_pb2" 1153 # @@protoc_insertion_point(class_scope:pinecone_columnar.FetchResponse) 1154 }, 1155) 1156_sym_db.RegisterMessage(FetchResponse) 1157 1158QueryRequest = _reflection.GeneratedProtocolMessageType( 1159 "QueryRequest", 1160 (_message.Message,), 1161 { 1162 "DESCRIPTOR": _QUERYREQUEST, 1163 "__module__": "vector_column_service_pb2" 1164 # @@protoc_insertion_point(class_scope:pinecone_columnar.QueryRequest) 1165 }, 1166) 1167_sym_db.RegisterMessage(QueryRequest) 1168 1169QueryResponse = _reflection.GeneratedProtocolMessageType( 1170 "QueryResponse", 1171 (_message.Message,), 1172 { 1173 "DESCRIPTOR": _QUERYRESPONSE, 1174 "__module__": "vector_column_service_pb2" 1175 # @@protoc_insertion_point(class_scope:pinecone_columnar.QueryResponse) 1176 }, 1177) 1178_sym_db.RegisterMessage(QueryResponse) 1179 1180DescribeIndexStatsRequest = _reflection.GeneratedProtocolMessageType( 1181 "DescribeIndexStatsRequest", 1182 (_message.Message,), 1183 { 1184 "DESCRIPTOR": _DESCRIBEINDEXSTATSREQUEST, 1185 "__module__": "vector_column_service_pb2" 1186 # @@protoc_insertion_point(class_scope:pinecone_columnar.DescribeIndexStatsRequest) 1187 }, 1188) 1189_sym_db.RegisterMessage(DescribeIndexStatsRequest) 1190 1191NamespaceSummary = _reflection.GeneratedProtocolMessageType( 1192 "NamespaceSummary", 1193 (_message.Message,), 1194 { 1195 "DESCRIPTOR": _NAMESPACESUMMARY, 1196 "__module__": "vector_column_service_pb2" 1197 # @@protoc_insertion_point(class_scope:pinecone_columnar.NamespaceSummary) 1198 }, 1199) 1200_sym_db.RegisterMessage(NamespaceSummary) 1201 1202DescribeIndexStatsResponse = _reflection.GeneratedProtocolMessageType( 1203 "DescribeIndexStatsResponse", 1204 (_message.Message,), 1205 { 1206 "NamespacesEntry": _reflection.GeneratedProtocolMessageType( 1207 "NamespacesEntry", 1208 (_message.Message,), 1209 { 1210 "DESCRIPTOR": _DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY, 1211 "__module__": "vector_column_service_pb2" 1212 # @@protoc_insertion_point(class_scope:pinecone_columnar.DescribeIndexStatsResponse.NamespacesEntry) 1213 }, 1214 ), 1215 "DESCRIPTOR": _DESCRIBEINDEXSTATSRESPONSE, 1216 "__module__": "vector_column_service_pb2" 1217 # @@protoc_insertion_point(class_scope:pinecone_columnar.DescribeIndexStatsResponse) 1218 }, 1219) 1220_sym_db.RegisterMessage(DescribeIndexStatsResponse) 1221_sym_db.RegisterMessage(DescribeIndexStatsResponse.NamespacesEntry) 1222 1223 1224DESCRIPTOR._options = None 1225_DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY._options = None 1226 1227_VECTORCOLUMNSERVICE = _descriptor.ServiceDescriptor( 1228 name="VectorColumnService", 1229 full_name="pinecone_columnar.VectorColumnService", 1230 file=DESCRIPTOR, 1231 index=0, 1232 serialized_options=None, 1233 create_key=_descriptor._internal_create_key, 1234 serialized_start=1451, 1235 serialized_end=1907, 1236 methods=[ 1237 _descriptor.MethodDescriptor( 1238 name="Upsert", 1239 full_name="pinecone_columnar.VectorColumnService.Upsert", 1240 index=0, 1241 containing_service=None, 1242 input_type=_UPSERTREQUEST, 1243 output_type=_UPSERTRESPONSE, 1244 serialized_options=None, 1245 create_key=_descriptor._internal_create_key, 1246 ), 1247 _descriptor.MethodDescriptor( 1248 name="Delete", 1249 full_name="pinecone_columnar.VectorColumnService.Delete", 1250 index=1, 1251 containing_service=None, 1252 input_type=_DELETEREQUEST, 1253 output_type=_DELETERESPONSE, 1254 serialized_options=None, 1255 create_key=_descriptor._internal_create_key, 1256 ), 1257 _descriptor.MethodDescriptor( 1258 name="Fetch", 1259 full_name="pinecone_columnar.VectorColumnService.Fetch", 1260 index=2, 1261 containing_service=None, 1262 input_type=_FETCHREQUEST, 1263 output_type=_FETCHRESPONSE, 1264 serialized_options=None, 1265 create_key=_descriptor._internal_create_key, 1266 ), 1267 _descriptor.MethodDescriptor( 1268 name="Query", 1269 full_name="pinecone_columnar.VectorColumnService.Query", 1270 index=3, 1271 containing_service=None, 1272 input_type=_QUERYREQUEST, 1273 output_type=_QUERYRESPONSE, 1274 serialized_options=None, 1275 create_key=_descriptor._internal_create_key, 1276 ), 1277 _descriptor.MethodDescriptor( 1278 name="DescribeIndexStats", 1279 full_name="pinecone_columnar.VectorColumnService.DescribeIndexStats", 1280 index=4, 1281 containing_service=None, 1282 input_type=_DESCRIBEINDEXSTATSREQUEST, 1283 output_type=_DESCRIBEINDEXSTATSRESPONSE, 1284 serialized_options=None, 1285 create_key=_descriptor._internal_create_key, 1286 ), 1287 ], 1288) 1289_sym_db.RegisterServiceDescriptor(_VECTORCOLUMNSERVICE) 1290 1291DESCRIPTOR.services_by_name["VectorColumnService"] = _VECTORCOLUMNSERVICE 1292 1293# @@protoc_insertion_point(module_scope)
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for metadata.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for vectors.
Getter for metadata.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for namespace_overrides.
Getter for top_k_overrides.
Getter for filter_overrides.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for matches.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for namespaces.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
Inherited Members
- vector_column_service_pb2.NamespacesEntry
- NamespacesEntry
- DESCRIPTOR
- MergeFrom
- Clear
- SetInParent
- IsInitialized
- MergeFromString
- SerializeToString
- SerializePartialToString
- ListFields
- HasField
- ClearField
- WhichOneof
- UnknownFields
- DiscardUnknownFields
- ByteSize
- FromString
- RegisterExtension
- KEY_FIELD_NUMBER
- key
- VALUE_FIELD_NUMBER
- value
- FindInitializationErrors
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension