Skip to content

Commit

Permalink
Drop deprecated route.url() (#2501)
Browse files Browse the repository at this point in the history
* Drop deprecated route.url()

* Add changelog
  • Loading branch information
asvetlov authored Nov 10, 2017
1 parent 3c1746d commit c7e51cb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 133 deletions.
1 change: 1 addition & 0 deletions CHANGES/2501.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Drop deprecated `resource.url()`.
49 changes: 0 additions & 49 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ def __init__(self, *, name=None):
def name(self):
return self._name

@abc.abstractmethod # pragma: no branch
def url(self, **kwargs):
"""Construct url for resource with additional params.
Deprecated, use url_for() instead.
"""
warnings.warn(".url(...) is deprecated, use .url_for instead",
DeprecationWarning,
stacklevel=3)

@abc.abstractmethod # pragma: no branch
def url_for(self, **kwargs):
"""Construct url for resource with additional params."""
Expand Down Expand Up @@ -171,17 +160,6 @@ def get_info(self):
def url_for(self, *args, **kwargs):
"""Construct url for route with additional params."""

@abc.abstractmethod # pragma: no branch
def url(self, **kwargs):
"""Construct url for resource with additional params.
Deprecated, use url_for() instead.
"""
warnings.warn(".url(...) is deprecated, use .url_for instead",
DeprecationWarning,
stacklevel=3)

async def handle_expect_header(self, request):
return await self._expect_handler(request)

Expand Down Expand Up @@ -334,10 +312,6 @@ def _match(self, path):
def get_info(self):
return {'path': self._path}

def url(self, *, query=None):
super().url()
return str(self.url_for().with_query(query))

def url_for(self):
return URL(self._path)

Expand Down Expand Up @@ -411,10 +385,6 @@ def url_for(self, **parts):
url = self._formatter.format_map(parts)
return URL(url)

def url(self, *, parts, query=None):
super().url(**parts)
return str(self.url_for(**parts).with_query(query))

def __repr__(self):
name = "'" + self.name + "' " if self.name is not None else ""
return ("<DynamicResource {name} {formatter}"
Expand Down Expand Up @@ -467,12 +437,6 @@ def __init__(self, prefix, directory, *, name=None,
'HEAD': ResourceRoute('HEAD', self._handle, self,
expect_handler=expect_handler)}

def url(self, *, filename, append_version=None, query=None):
url = self.url_for(filename=filename, append_version=append_version)
if query is not None:
return str(url.update_query(query))
return str(url)

def url_for(self, *, filename, append_version=None):
if append_version is None:
append_version = self._append_version
Expand Down Expand Up @@ -629,11 +593,6 @@ def url_for(self, *args, **kwargs):
raise RuntimeError(".url_for() is not supported "
"by sub-application root")

def url(self, **kwargs):
"""Construct url for route with additional params."""
raise RuntimeError(".url() is not supported "
"by sub-application root")

def get_info(self):
return {'app': self._app,
'prefix': self._prefix}
Expand Down Expand Up @@ -681,11 +640,6 @@ def url_for(self, *args, **kwargs):
"""Construct url for route with additional params."""
return self._resource.url_for(*args, **kwargs)

def url(self, **kwargs):
"""Construct url for route with additional params."""
super().url(**kwargs)
return self._resource.url(**kwargs)

def get_info(self):
return self._resource.get_info()

Expand All @@ -699,9 +653,6 @@ def __init__(self, http_exception):
def url_for(self, *args, **kwargs):
raise RuntimeError(".url_for() is not allowed for SystemRoute")

def url(self, *args, **kwargs):
raise RuntimeError(".url() is not allowed for SystemRoute")

@property
def name(self):
return None
Expand Down
13 changes: 0 additions & 13 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1792,19 +1792,6 @@ Resource classes hierarchy::

:return: :class:`~yarl.URL` -- resulting URL instance.

.. method:: url(**kwargs)

Construct an URL for route with additional params.

**kwargs** depends on a list accepted by inherited resource
class parameters.

:return: :class:`str` -- resulting URL string.

.. deprecated:: 1.1

Use :meth:`url_for` instead.


.. class:: Resource

Expand Down
90 changes: 21 additions & 69 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,99 +322,80 @@ def test_route_dynamic(router):
route = router.add_route('GET', '/get/{name}', handler,
name='name')

route2 = next(iter(router['name']))
with pytest.warns(DeprecationWarning):
url = route2.url(parts={'name': 'John'})
assert '/get/John' == url
assert route is route2


def test_route_dynamic2(router):
handler = make_handler()
route = router.add_route('GET', '/get/{name}', handler,
name='name')

route2 = next(iter(router['name']))
url = route2.url_for(name='John')
assert '/get/John' == str(url)
assert route is route2


def test_route_with_qs(router):
handler = make_handler()
router.add_route('GET', '/get', handler, name='name')

with pytest.warns(DeprecationWarning):
url = router['name'].url(query=[('a', 'b'), ('c', '1')])
assert '/get?a=b&c=1' == url


def test_add_static(router):
resource = router.add_static('/st',
os.path.dirname(aiohttp.__file__),
name='static')
assert router['static'] is resource
url = resource.url(filename='/dir/a.txt')
assert '/st/dir/a.txt' == url
url = resource.url_for(filename='/dir/a.txt')
assert '/st/dir/a.txt' == str(url)
assert len(resource) == 2


def test_add_static_append_version(router):
resource = router.add_static('/st',
os.path.dirname(__file__),
name='static')
url = resource.url(filename='/data.unknown_mime_type', append_version=True)
url = resource.url_for(filename='/data.unknown_mime_type',
append_version=True)
expect_url = '/st/data.unknown_mime_type?' \
'v=aUsn8CHEhhszc81d28QmlcBW0KQpfS2F4trgQKhOYd8%3D'
assert expect_url == url
assert expect_url == str(url)


def test_add_static_append_version_set_from_constructor(router):
resource = router.add_static('/st',
os.path.dirname(__file__),
append_version=True,
name='static')
url = resource.url(filename='/data.unknown_mime_type')
url = resource.url_for(filename='/data.unknown_mime_type')
expect_url = '/st/data.unknown_mime_type?' \
'v=aUsn8CHEhhszc81d28QmlcBW0KQpfS2F4trgQKhOYd8%3D'
assert expect_url == url
assert expect_url == str(url)


def test_add_static_append_version_override_constructor(router):
resource = router.add_static('/st',
os.path.dirname(__file__),
append_version=True,
name='static')
url = resource.url(filename='/data.unknown_mime_type',
append_version=False)
url = resource.url_for(filename='/data.unknown_mime_type',
append_version=False)
expect_url = '/st/data.unknown_mime_type'
assert expect_url == url
assert expect_url == str(url)


def test_add_static_append_version_filename_without_slash(router):
resource = router.add_static('/st',
os.path.dirname(__file__),
name='static')
url = resource.url(filename='data.unknown_mime_type', append_version=True)
url = resource.url_for(filename='data.unknown_mime_type',
append_version=True)
expect_url = '/st/data.unknown_mime_type?' \
'v=aUsn8CHEhhszc81d28QmlcBW0KQpfS2F4trgQKhOYd8%3D'
assert expect_url == url
assert expect_url == str(url)


def test_add_static_append_version_non_exists_file(router):
resource = router.add_static('/st',
os.path.dirname(__file__),
name='static')
url = resource.url(filename='/non_exists_file', append_version=True)
assert '/st/non_exists_file' == url
url = resource.url_for(filename='/non_exists_file', append_version=True)
assert '/st/non_exists_file' == str(url)


def test_add_static_append_version_non_exists_file_without_slash(router):
resource = router.add_static('/st',
os.path.dirname(__file__),
name='static')
url = resource.url(filename='non_exists_file', append_version=True)
assert '/st/non_exists_file' == url
url = resource.url_for(filename='non_exists_file', append_version=True)
assert '/st/non_exists_file' == str(url)


def test_add_static_append_version_follow_symlink(router, tmpdir):
Expand All @@ -430,12 +411,12 @@ def test_add_static_append_version_follow_symlink(router, tmpdir):
resource = router.add_static('/st', tmp_dir_path, follow_symlinks=True,
append_version=True)

url = resource.url(
url = resource.url_for(
filename='/append_version_symlink/data.unknown_mime_type')

expect_url = '/st/append_version_symlink/data.unknown_mime_type?' \
'v=aUsn8CHEhhszc81d28QmlcBW0KQpfS2F4trgQKhOYd8%3D'
assert expect_url == url
assert expect_url == str(url)


def test_add_static_append_version_not_follow_symlink(router, tmpdir):
Expand All @@ -452,30 +433,8 @@ def test_add_static_append_version_not_follow_symlink(router, tmpdir):
append_version=True)

filename = '/append_version_symlink/data.unknown_mime_type'
url = resource.url(filename=filename)
assert '/st/append_version_symlink/data.unknown_mime_type' == url


def test_add_static_append_version_with_query(router):
resource = router.add_static('/st',
os.path.dirname(__file__),
name='static')
url = resource.url(filename='/data.unknown_mime_type',
append_version=True,
query={'key': 'val'})
expect_url = '/st/data.unknown_mime_type?' \
'v=aUsn8CHEhhszc81d28QmlcBW0KQpfS2F4trgQKhOYd8%3D&key=val'
assert expect_url == url


def test_add_static_append_version_non_exists_file_with_query(router):
resource = router.add_static('/st',
os.path.dirname(__file__),
name='static')
url = resource.url(filename='/non_exists_file',
append_version=True,
query={'key': 'val'})
assert '/st/non_exists_file?key=val' == url
url = resource.url_for(filename=filename)
assert '/st/append_version_symlink/data.unknown_mime_type' == str(url)


def test_plain_not_match(router):
Expand Down Expand Up @@ -1021,13 +980,6 @@ def test_subapp_get_info(app, loop):
assert resource.get_info() == {'prefix': '/pre', 'app': subapp}


def test_subapp_url(app, loop):
subapp = web.Application()
resource = app.add_subapp('/pre', subapp)
with pytest.raises(RuntimeError):
resource.url()


def test_subapp_url_for(app, loop):
subapp = web.Application()
resource = app.add_subapp('/pre', subapp)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ async def handler(data, request):

def test_system_route():
route = SystemRoute(web.HTTPCreated(reason='test'))
with pytest.raises(RuntimeError):
route.url()
with pytest.raises(RuntimeError):
route.url_for()
assert route.name is None
Expand Down

0 comments on commit c7e51cb

Please sign in to comment.