Skip to content

Commit 0e3326b

Browse files
anandoleecopybara-github
authored andcommitted
Implement typing for proto Timestamp/Duration assignments.
PiperOrigin-RevId: 730336339
1 parent e390402 commit 0e3326b

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

python/google/protobuf/internal/duration_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import unittest
1313

1414
from google.protobuf import duration
15+
from google.protobuf.internal import well_known_types_test_pb2
1516

1617
from google.protobuf import duration_pb2
1718

@@ -57,6 +58,13 @@ def test_duration_timedelta(self):
5758
converted_message = duration.from_timedelta(td)
5859
self.assertEqual(message, converted_message)
5960

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+
6068

6169
if __name__ == '__main__':
6270
unittest.main()

python/google/protobuf/internal/timestamp_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def test_timestamp_datetime(self):
5959
naive_utc_epoch, timestamp.to_datetime(message.optional_timestamp) # pytype: disable=wrong-arg-types
6060
)
6161

62+
def test_timstamp_construction(self):
63+
message = well_known_types_test_pb2.WKTMessage(
64+
optional_timestamp=datetime.datetime.today()
65+
)
66+
6267

6368
if __name__ == '__main__':
6469
unittest.main()

src/google/protobuf/compiler/python/pyi_generator.cc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,18 @@ std::string PyiGenerator::GetFieldType(
396396
return "";
397397
}
398398

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 {
401411
if (!is_nested) {
402412
printer_->Print("\n");
403413
}
@@ -528,9 +538,11 @@ void PyiGenerator::PrintMessage(
528538
printer_->Print("_Iterable[");
529539
}
530540
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));
534546
} else {
535547
if (field_des->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
536548
printer_->Print("_Union[$type_name$, str]", "type_name",

src/google/protobuf/compiler/python/pyi_generator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class PROTOC_EXPORT PyiGenerator : public google::protobuf::compiler::CodeGenera
8383
std::string ModuleLevelName(const DescriptorT& descriptor) const;
8484
std::string PublicPackage() const;
8585
std::string InternalPackage() const;
86+
std::string ExtraInitTypes(const Descriptor& msg_des) const;
8687

8788
bool opensource_runtime_ = true;
8889

0 commit comments

Comments
 (0)