You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by GitBox <gi...@apache.org> on 2022/04/02 11:44:49 UTC

[GitHub] [thrift] jeking3 commented on a change in pull request #2523: THRIFT-5139: THRIFT-4181: Python3 type hints

jeking3 commented on a change in pull request #2523:
URL: https://github.com/apache/thrift/pull/2523#discussion_r841066871



##########
File path: compiler/cpp/src/thrift/generate/t_py_generator.cc
##########
@@ -2742,8 +2765,71 @@ string t_py_generator::type_name(t_type* ttype) {
   return ttype->get_name();
 }
 
+string t_py_generator::arg_hint(t_type* type) {
+  if(gen_type_hints_) {
+      return ": " + type_to_py_hint(type);
+  }
+
+  return "";
+}
+
+string t_py_generator::func_hint(t_type* type) {
+  if(gen_type_hints_) {
+      return " -> " + type_to_py_hint(type);
+  }
+
+  return "";
+}
+
+/**
+ * Converts the parse type to a Python type hint
+ */
+string t_py_generator::type_to_py_hint(t_type* type) {
+  return "typing.Optional[" + type_to_py_type(type) + "]";
+}
+
+/**
+ * Converts the parse type to a Python type
+ */
+string t_py_generator::type_to_py_type(t_type* type) {
+  type = get_true_type(type);
+
+  if (type->is_binary()) {
+    return  "bytes";
+  }
+  if (type->is_base_type()) {
+    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+    switch (tbase) {
+    case t_base_type::TYPE_VOID:
+      return "None";
+    case t_base_type::TYPE_STRING:
+      return "str";
+    case t_base_type::TYPE_BOOL:
+      return "bool";
+    case t_base_type::TYPE_I8:
+    case t_base_type::TYPE_I16:
+    case t_base_type::TYPE_I32:
+    case t_base_type::TYPE_I64:
+      return "int";
+    case t_base_type::TYPE_DOUBLE:
+      return "float";
+    }
+  } else if (type->is_enum() || type->is_struct() || type->is_xception()) {
+    return type_name(type);
+  } else if (type->is_map()) {
+    return "dict[" + type_to_py_type(((t_map*)type)->get_key_type()) + ", " + type_to_py_type(((t_map*)type)->get_val_type()) + "]";

Review comment:
       This should be using `Dict[...]` and `from typing import Dict` ?
   
   Same below for `Set` and `List` ?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org