ShaneEverittM commented on code in PR #2523:
URL: https://github.com/apache/thrift/pull/2523#discussion_r1034311607
##########
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) + "]";
Review Comment:
Here's my setup.
Thrift file:
```thrift
service Increment {
i32 add(1: i32 value);
}
```
Python Client:
```python
from serial import Serial
from thrift.protocol.TCompactProtocol import TCompactProtocol
from thrift.transport.TTransport import TFramedTransport,
TFileObjectTransport
from commands.Increment import Client
def main():
client =
Client(TCompactProtocol(TFramedTransport(TFileObjectTransport(Serial("COM2",
115200)))))
assert client.add(None) == 1, "Expect default value on server to be 0,
returning 1"
if __name__ == '__main__':
main()
```
Generated from
[this](https://github.com/cspwizard/thrift/tree/feature/python3) branch,
running:
```shell
./thrift --gen py:type_hints -out pyclient/ idl/commands.thrift
```
Rust Server:
```rust
use anyhow::Result;
use thrift::{
protocol::{TCompactInputProtocol, TCompactOutputProtocol},
server::TProcessor,
transport::{TFramedReadTransport, TFramedWriteTransport}
};
use commands::{IncrementSyncHandler, IncrementSyncProcessor};
use rpc::VirtualCOMPort;
use thrift_import::import;
import!(commands);
fn main() -> Result<()> {
let port = serialport::new("COM4", 115200).open_native()?;
let read_port = port.try_clone_native().expect("Failed to duplicate
handle");
let write_port = port;
let mut i_prot =
TCompactInputProtocol::new(TFramedReadTransport::new(VirtualCOMPort(read_port)));
let mut o_prot =
TCompactOutputProtocol::new(TFramedWriteTransport::new(VirtualCOMPort(write_port)));
let processor = IncrementSyncProcessor::new(Increment);
loop {
processor.process(&mut i_prot, &mut o_prot)?;
}
}
struct Increment;
impl IncrementSyncHandler for Increment {
fn handle_add(&self, value: i32) -> thrift::Result<i32> {
Ok(value + 1)
}
}
```
Where `VirtualCOMPort` is a wrapper that stubs `flush` with noop because my
virtual COM port software doesn't support that operation for some reason. The
wrapper looks like this:
```rust
use std::io::{self, Read, Write};
use serialport::COMPort;
pub struct VirtualCOMPort(pub COMPort);
impl Write for VirtualCOMPort {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
// Flushing does not work with virtual COM ports.
Ok(())
}
}
impl Read for VirtualCOMPort {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
```
The rust is generated using a build script and an importing macro that I
wrote, but it boils down to this call to the thrift compiler:
```shell
./thrift --gen rs -out $OUT_DIR ./idl/commands.thrift
```
When I run the client as shown above, the server returns this exception:
```shell
thrift.Thrift.TApplicationException: ProtocolError { kind: Unknown, message:
"missing required field IncrementAddArgs.value" }
```
This is in conflict with the generated signature of `Client.add`:
```python
def add(self, value: typing.Optional[int]) -> typing.Optional[int]:
```
Let me know if you need any more information.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]