Skip to content
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

TypeError in URL.with_query #24

Closed
dyus opened this issue Nov 14, 2016 · 5 comments
Closed

TypeError in URL.with_query #24

dyus opened this issue Nov 14, 2016 · 5 comments

Comments

@dyus
Copy link

dyus commented Nov 14, 2016

Hello, I have a problem with generating query_params. I try to make query

session = aiohttp.ClientSession()
session.request('get', 'some_url', {'p1': 1, 'p1': 2, 'p2': 3})

and got TypeError

  File "/lib/python3.5/site-packages/aiohttp/client.py", line 553, in __await__
    resp = yield from self._coro
  File "/lib/python3.5/site-packages/aiohttp/client.py", line 198, in _request
    proxy=proxy, proxy_auth=proxy_auth, timeout=timeout)
  File "lib/python3.5/site-packages/aiohttp/client_reqrep.py", line 79, in __init__
    url2 = url.with_query(params)
  File "/lib/python3.5/site-packages/yarl/__init__.py", line 675, in with_query
    raise TypeError("Invalid variable type")
TypeError: Invalid variable type

After some research I understand that Mapping with key: Sequence works with MultiDict. But this is not obvious. Maybe it should cast dict into MultiDict or return more descriptive warning or add logic for parsing Sequences in Mapping section

@asvetlov
Copy link
Member

I prefer first option: casting dict into MultiDict.
Would you provide a patch?

@dyus
Copy link
Author

dyus commented Nov 15, 2016

Yes, I'll send pr today or tomorrow

@asvetlov
Copy link
Member

After some research I understand that Mapping with key: Sequence works with MultiDict.

@dyus please elaborate.

>>> MultiDict(a=[1, 2, 3])
<MultiDict('a': [1, 2, 3])>

Result is a dict with single key and list as value.
It's not the same as three items with the same key:

>>> MultiDict([('a', 1), ('a', 2), ('a', 3)])
<MultiDict('a': 1, 'a': 2, 'a': 3)>

@dyus
Copy link
Author

dyus commented Nov 24, 2016

You mean dict_to_multidict should convert {'a': [1, 2, 3} into <MultiDict('a': [1, 2, 3])>'? This result is not a valid structure for URL.with_query` method.

n [5]: u = yarl.URL('http://123/')
In [6]: u.with_query(multidict.MultiDict({'a': [1,2,3]}))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-6675a96330d2> in <module>()
----> 1 u.with_query(multidict.MultiDict({'a': [1,2,3]}))

/Users/a.zdanchuk/Envs/mobile_api/lib/python3.5/site-packages/yarl/__init__.py in with_query(self, *args, **kwargs)
    673                 #     v = '&'.join([str(i) for i in v])
    674                 else:
--> 675                     raise TypeError("Invalid variable type")
    676                 lst.append(quoter(k)+'='+quoter(v))
    677             query = '&'.join(lst)

TypeError: Invalid variable type

@asvetlov
Copy link
Member

asvetlov commented Dec 2, 2016

I don't want to support list values but added more verbose error messages: 4b9a94a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants