Skip to content

Commit

Permalink
Don't create a new URL if fragment unchanged (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 27, 2019
1 parent 089eeac commit d0752ca
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ CHANGES

* Fix quoting of plus in path by pure python version (#339)

* Don't create a new URL if fragment is unchanged (#292)

1.3.0 (2018-12-11)
------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ junit_suite_name = yarl_test_suite


[flake8]
ignore = E301,E302,E704,W503,W504
ignore = E301,E302,E704,W503,W504,F811
max-line-length = 88

[mypy]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,18 @@ def test_with_fragment_None():
assert str(url2) == "http://example.com/path"


def test_with_fragment_None_matching():
url = URL("http://example.com/path")
url2 = url.with_fragment(None)
assert url is url2


def test_with_fragment_matching():
url = URL("http://example.com/path#frag")
url2 = url.with_fragment("frag")
assert url is url2


def test_with_fragment_bad_type():
url = URL("http://example.com")
with pytest.raises(TypeError):
Expand Down
10 changes: 6 additions & 4 deletions yarl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,12 +913,14 @@ def with_fragment(self, fragment):
"""
# N.B. doesn't cleanup query/fragment
if fragment is None:
fragment = ""
raw_fragment = ""
elif not isinstance(fragment, str):
raise TypeError("Invalid fragment type")
return URL(
self._val._replace(fragment=self._FRAGMENT_QUOTER(fragment)), encoded=True
)
else:
raw_fragment = self._FRAGMENT_QUOTER(fragment)
if self.raw_fragment == raw_fragment:
return self
return URL(self._val._replace(fragment=raw_fragment), encoded=True)

def with_name(self, name):
"""Return a new URL with name (last part of path) replaced.
Expand Down
4 changes: 2 additions & 2 deletions yarl/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ class URL:
def with_path(self, path: str, *, encoded: bool = ...) -> URL: ...
@overload
def with_query(self, query: Query) -> URL: ...
@overload # noqa: F811
@overload
def with_query(self, **kwargs: QueryVariable) -> URL: ...
@overload
def update_query(self, query: Query) -> URL: ...
@overload # noqa: F811
@overload
def update_query(self, **kwargs: QueryVariable) -> URL: ...
def with_fragment(self, fragment: Optional[str]) -> URL: ...
def with_name(self, name: str) -> URL: ...
Expand Down

0 comments on commit d0752ca

Please sign in to comment.