def test_constructor_proto2(): # type: () -> None x = Simple2() # It's OK to omit a required field from the constructor. assert not x.HasField("a_string") x = Simple2(a_string=None) # It's OK to pass None for a required field. assert not x.HasField("a_string")
def test_extensions_proto2(): # type: () -> None s1 = Simple1() s2 = Simple2() assert isinstance(Extensions1.ext, FieldDescriptor) assert isinstance(Extensions2.foo, FieldDescriptor) assert isinstance(SeparateFileExtension.ext, FieldDescriptor) e1 = s1.Extensions[Extensions1.ext] e1.ext1_string = "first extension" assert isinstance(e1, Extensions1) e2 = s1.Extensions[Extensions2.foo] e2.flag = True assert isinstance(e2, Extensions2) e3 = s2.Extensions[SeparateFileExtension.ext] e3.flag = True assert isinstance(e3, SeparateFileExtension) del s1.Extensions[Extensions2.foo] # Using __iter__, x is a FieldDescriptor but the type of the message that # s1.Extensions[x] yields is unknown (it could be any of the extension messages). # Hence, s1.Extensions[x] is typed as Any. for x in s1.Extensions: assert isinstance(x, FieldDescriptor) assert x.is_extension y = s1.Extensions[x] assert y.ext1_string == "first extension" assert Extensions1.ext in s1.Extensions assert len(s2.Extensions) == 1
s6.HasField(u"outer_enum") # E:2.7 E:3.8 s6.HasField(u"a_repeated_string") # E:2.7 E:3.8 # Proto2 s.ClearField("garbage") # E:2.7 E:3.8 # Proto3 s6.ClearField("garbage") # E:2.7 E:3.8 # Proto2 WhichOneof s.WhichOneof("garbage") # E:2.7 E:3.8 a = 5 a = s.WhichOneof("a_oneof") # E:2.7 E:3.8 b = s.WhichOneof("a_oneof") s.HasField(b) # allowed simple2 = Simple2(a_string="abc") simple2.HasField(b) # E:2.7 E:3.8 # Proto3 WhichOneof s6.WhichOneof("garbage") # E:2.7 E:3.8 a3 = 5 a3 = s6.WhichOneof("a_oneof") # E:2.7 E:3.8 b3 = s6.WhichOneof("a_oneof") s6.HasField(b3) # allowed simple2.HasField(b3) # E:2.7 E:3.8 - it's a text but not one of the literals # Proto2 Extensions an_int = 5 an_int = Extensions1.ext # E:2.7 E:3.8 _ = s.Extensions[Extensions1.bad] # E:2.7 E:3.8 e1 = s.Extensions[Extensions1.ext]
s6.HasField(u"outer_enum") # E:2.7 E:3.5 s6.HasField(u"a_repeated_string") # E:2.7 E:3.5 # Proto2 s.ClearField("garbage") # E:2.7 E:3.5 # Proto3 s6.ClearField("garbage") # E:2.7 E:3.5 # Proto2 WhichOneof s.WhichOneof("garbage") # E:2.7 E:3.5 a = 5 a = s.WhichOneof("a_oneof") # E:2.7 E:3.5 b = s.WhichOneof("a_oneof") s.HasField(b) # allowed simple2 = Simple2(a_string="abc") simple2.HasField(b) # E:2.7 E:3.5 # Proto3 WhichOneof s6.WhichOneof("garbage") # E:2.7 E:3.5 a3 = 5 a3 = s6.WhichOneof("a_oneof") # E:2.7 E:3.5 b3 = s6.WhichOneof("a_oneof") s6.HasField(b3) # allowed simple2.HasField(b3) # E:2.7 E:3.5 - it's a text but not one of the literals # Proto2 Extensions an_int = 5 an_int = Extensions1.ext # E:2.7 E:3.5 _ = s.Extensions[Extensions1.bad] # E:2.7 E:3.5 e1 = s.Extensions[Extensions1.ext]