-
-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encoding None Values #118
Comments
Yes. It appears to be a bug. if iterable_vs is not None:
for v in iterable_vs:
if v is not None:
result.append(
(
k.encode("utf-8") if isinstance(k, str) else k,
v.encode("utf-8") if isinstance(v, str) else v,
)
) Adding regards, |
I'd opt to integrate the more general solution that is in requests, which just throws the object into a list if it's not iterable. if isinstance(vs, (str, bytes, int, float, bool)) or not hasattr(vs, "__iter__"):
# not officially supported, but some people maybe passing ints, float or bool.
if isinstance(vs, (str, bytes)) is False:
iterable_vs = [str(vs)]
else:
iterable_vs = [vs]
else:
iterable_vs = vs
for v in iterable_vs:
if v is not None:
result.append(
(
k.encode("utf-8") if isinstance(k, str) else k,
v.encode("utf-8") if isinstance(v, str) else v,
)
) ) or we could do this to avoid another if statement: if isinstance(vs, (str, bytes, int, float, bool, type(None)):
# not officially supported, but some people maybe passing ints, float or bool.
if isinstance(vs, (str, bytes)) is False:
iterable_vs = [str(vs)]
else:
iterable_vs = [vs]
else:
iterable_vs = vs
for v in iterable_vs:
if v is not None:
result.append(
(
k.encode("utf-8") if isinstance(k, str) else k,
v.encode("utf-8") if isinstance(v, str) else v,
)
) ) |
the thing is[...] requests isn't typed and allowed things that made mypy unhappy (as far as I remember). I am open to suggestions as long as:
regards, |
Thanks @Ousret, I have a fix with mypy passing. I can also implement some unit tests for this bit too. Are you interested in setting mypy to run in CI? Or are the pre-commit hooks sufficient for you? |
The pre-commit includes mypy. So yes, it is sufficient. |
This addresses #118, inspired by the code in [requests](https://github.com/psf/requests/blob/2d5f54779ad174035c5437b3b3c1146b0eaf60fe/src/requests/models.py#L122). Essentially this PR will allow encoding any non-iterable object as string. This is a general solution to the specific problem in #118. Since `None` is not iterable, it is encoded "None". Similarly, any non-iterable object will be encoded as `str(obj)`. --------- Co-authored-by: TAHRI Ahmed R <Ousret@users.noreply.github.com>
It is live.
|
I was testing niquests to see if it is a drop-in replacement for requests for my project and found an issue. My project sometimes ends up posting data that has
None
values. Requests does not have an issue with this, but niquests throws aTypeError: 'NoneType' object is not iterable
error.The difference comes in how requests and niquests have their `_encode_params method. See the requests implementation and the niquests implementation
Expected Result
Expect the post complete when posting data with
None
values.Actual Result
niquests throws a
TypeError: 'NoneType' object is not iterable
error.Reproduction Steps
System Information
The text was updated successfully, but these errors were encountered: