EventHeartbeatPayload payload data structure error.
Closed this issue · 13 comments
When wechaty puppet recieved a heartbeat from hostie, an error occured: TypeError: __init__() got an unexpected keyword argument 'timeout'
python-wechaty-puppet/src/wechaty_puppet/schemas/event.py
Lines 132 to 134 in 984e173
Here class
EventHeartbeatPayload()
only defined one of the arguments data
, left timeout
behind, which may cause this error.@zavierxu it's a greate issue about heartbeat
event payload. This is translated from wechaty-puppet. This is a really data structure bug for python-wechaty-puppet, Thank your for finding it.
@huan I wonder if this is also a bug for wechaty-puppet ?
What kind of bug so you think it is, could you please describe it in details?
python-wechaty-puppet-hostie
receive EventHeartBeatPayload
from hostie-server
with two fields: data
, timeout
. I translate from puppet-wechaty
, so EventHeartBeatPayload
in python-wechaty
have only one field: data
. wechaty-puppet also have one field: data
I wonder if this is also a bug for wechaty-puppet ?
According to TypeScript handbook : https://www.typescriptlang.org/docs/handbook/interfaces.html#our-first-interface
Notice we didn’t have to explicitly say that the object we pass to printLabel implements this interface like we might have to in other languages. Here, it’s only the shape that matters. If the object we pass to the function meets the requirements listed, then it’s allowed.
So, it's valid in TypeScript, but not in Python
Thanks for the explanation and the fix.
It seems that the timeout
property is unexpected data received from the server.
I believe we should always align with our golden truth from: wechaty-puppet
. So the https://github.com/wechaty/wechaty-puppet/blob/ac9c87017234432d716fe55aa88874d22dd03412/src/schemas/event.ts#L80-L82, which you have used before should be the right data structure:
export interface EventHeartbeatPayload {
data: string,
}
However, when we receive a payload that contains unexpected property, it seems that the Python dataclass will raise an exception?
Could we make the Python dataclass just ignore the unexpected property? If we can, I believe we can do that.
Or if it can't, then I agree with you that the #7 will be a good workaround.
At last, if we can find the reason that the puppet server sends the timeout
, we can fix this problem at the server-side. a PR will be welcome if anyone can figure it out.
Ok,I will try to fix unexpected field exception at dataclass.
There are several workgrounds,for example this one, but it seems there is not an official way to do this.
I have proposed a PR at wechaty/python-wechaty-puppet-service#7, it's very concise and clean, I think it should be able to solve this problem?
It seems that this issue has been solved. I will close this issue.