Issue with encode_request
and encode_urlencoded_data
#3115
Unanswered
hekhuisk
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I found a couple of issues while using this library.
AsyncClient.post()
takes indata
which is supposed to beRequestData
orNone
. Looking at theRequestData
type it saysMapping[str, Any]
. Then insideencode_request
it also takes indata
which is supposed to beRequestData
orNone
. However, the first line of that method is checkingif data is not None and not isinstance(data, Mapping)
which implies it can be a type besidesRequestData
orNone
. That means a typechecker does not like you passingbytes
fordata
, even though it is a valid type to pass (yes I know it says it is deprecated, but having something deprecated should not invalidate type hinting).If you do pass a
Mapping
, in my case adict
, toAsyncClient.post()
for thedata
field, and thatdict
happens to contain a key/value pair where the value is anotherdict
containing a value ofNone
, then you end up with a string that says"None"
in it. The reason is because of this line inencode_urlencoded_data
.The fix that is needed is to check
if isinstance(value, Mapping)
and handle properly converting the values in it to a string. You also need to recursively go through all values to check for more nestedMapping
s. This applies to the check forlist
andtuple
as well where it doesplain_data.extend([(key, primitive_value_to_str(item)) for item in value])
. Ifitem
is aMapping
you run into the same issue.I found Form-encoded data with
None
as the value gets passed as string "None" #1500 and see that the choice was made to always convertNone
to""
. I've had many use cases whereNone
is supposed to representnull
and not empty string. So if I make a call out to something that can beNone
or astr
and it receives""
it will becomestr
instead of properly becomingNone
. PerhapsRequest
should take a flag callednone_as_null
that ifTrue
would putnull
in the serialized JSON instead of""
.?Beta Was this translation helpful? Give feedback.
All reactions