A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/protocolbuffers/protobuf/commit/0e3326bbbbf42641a6c8392bde3ebff5d2b53b22 below:

Implement typing for proto Timestamp/Duration assignments. · protocolbuffers/protobuf@0e3326b · GitHub

File tree Expand file treeCollapse file tree 4 files changed

+31

-5

lines changed

Filter options

Expand file treeCollapse file tree 4 files changed

+31

-5

lines changed Original file line number Diff line number Diff line change

@@ -12,6 +12,7 @@

12 12

import unittest

13 13 14 14

from google.protobuf import duration

15 +

from google.protobuf.internal import well_known_types_test_pb2

15 16 16 17

from google.protobuf import duration_pb2

17 18

@@ -57,6 +58,13 @@ def test_duration_timedelta(self):

57 58

converted_message = duration.from_timedelta(td)

58 59

self.assertEqual(message, converted_message)

59 60 61 +

def test_duration_construction(self):

62 +

expected_td = datetime.timedelta(microseconds=123)

63 +

message = well_known_types_test_pb2.WKTMessage(

64 +

optional_duration=expected_td

65 +

)

66 +

self.assertEqual(expected_td, message.optional_duration.ToTimedelta())

67 + 60 68 61 69

if __name__ == '__main__':

62 70

unittest.main()

Original file line number Diff line number Diff line change

@@ -59,6 +59,11 @@ def test_timestamp_datetime(self):

59 59

naive_utc_epoch, timestamp.to_datetime(message.optional_timestamp) # pytype: disable=wrong-arg-types

60 60

)

61 61 62 +

def test_timstamp_construction(self):

63 +

message = well_known_types_test_pb2.WKTMessage(

64 +

optional_timestamp=datetime.datetime.today()

65 +

)

66 + 62 67 63 68

if __name__ == '__main__':

64 69

unittest.main()

Original file line number Diff line number Diff line change

@@ -396,8 +396,18 @@ std::string PyiGenerator::GetFieldType(

396 396

return "";

397 397

}

398 398 399 -

void PyiGenerator::PrintMessage(

400 -

const Descriptor& message_descriptor, bool is_nested) const {

399 +

std::string PyiGenerator::ExtraInitTypes(const Descriptor& msg_des) const {

400 +

if (msg_des.full_name() == "google.protobuf.Timestamp") {

401 +

return "datetime.datetime, ";

402 +

} else if (msg_des.full_name() == "google.protobuf.Duration") {

403 +

return "datetime.timedelta, ";

404 +

} else {

405 +

return "";

406 +

}

407 +

}

408 + 409 +

void PyiGenerator::PrintMessage(const Descriptor& message_descriptor,

410 +

bool is_nested) const {

401 411

if (!is_nested) {

402 412

printer_->Print("\n");

403 413

}

@@ -528,9 +538,11 @@ void PyiGenerator::PrintMessage(

528 538

printer_->Print("_Iterable[");

529 539

}

530 540

if (field_des->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {

531 -

printer_->Print(

532 -

"_Union[$type_name$, _Mapping]", "type_name",

533 -

GetFieldType(*field_des, message_descriptor));

541 +

const auto& extra_init_types =

542 +

ExtraInitTypes(*field_des->message_type());

543 +

printer_->Print("_Union[$extra_init_types$$type_name$, _Mapping]",

544 +

"extra_init_types", extra_init_types, "type_name",

545 +

GetFieldType(*field_des, message_descriptor));

534 546

} else {

535 547

if (field_des->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {

536 548

printer_->Print("_Union[$type_name$, str]", "type_name",

Original file line number Diff line number Diff line change

@@ -83,6 +83,7 @@ class PROTOC_EXPORT PyiGenerator : public google::protobuf::compiler::CodeGenera

83 83

std::string ModuleLevelName(const DescriptorT& descriptor) const;

84 84

std::string PublicPackage() const;

85 85

std::string InternalPackage() const;

86 +

std::string ExtraInitTypes(const Descriptor& msg_des) const;

86 87 87 88

bool opensource_runtime_ = true;

88 89

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4