Skip to content

Commit

Permalink
Use f-strings instead of str.format() (#1793)
Browse files Browse the repository at this point in the history
  • Loading branch information
myusko authored Feb 25, 2020
1 parent 861e873 commit 16961fa
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 103 deletions.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _setup(route_details: tuple) -> (Router, tuple):
for method, route in route_details:
try:
router._add(
uri="/{}".format(route),
uri=f"/{route}",
methods=frozenset({method}),
host="localhost",
handler=_handler,
Expand Down
4 changes: 1 addition & 3 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ def handler(request):
request, response = app.test_client.get("/", debug=True)
assert response.status == 500
assert response.text.startswith(
"Error while handling error: {}\nStack: Traceback (most recent call last):\n".format(
err_msg
)
f"Error while handling error: {err_msg}\nStack: Traceback (most recent call last):\n"
)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_blueprint_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def blueprint_1_v2_method_with_put_and_post(request: Request):
)
def blueprint_2_named_method(request: Request, param):
if request.method == "DELETE":
return text("DELETE_{}".format(param))
return text(f"DELETE_{param}")
elif request.method == "PATCH":
return text("PATCH_{}".format(param))
return text(f"PATCH_{param}")

blueprint_group = Blueprint.group(
blueprint_1, blueprint_2, url_prefix="/api"
Expand Down
12 changes: 6 additions & 6 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ def test_versioned_routes_get(app, method):
func = getattr(bp, method)
if callable(func):

@func("/{}".format(method), version=1)
@func(f"/{method}", version=1)
def handler(request):
return text("OK")

else:
print(func)
raise Exception("{} is not callable".format(func))
raise Exception(f"{func} is not callable")

app.blueprint(bp)

client_method = getattr(app.test_client, method)

request, response = client_method("/v1/{}".format(method))
request, response = client_method(f"/v1/{method}")
assert response.status == 200


Expand Down Expand Up @@ -554,7 +554,7 @@ def api_v1_info(request):

resource_id = str(uuid4())
request, response = app.test_client.get(
"/api/v1/resources/{0}".format(resource_id)
f"/api/v1/resources/{resource_id}"
)
assert response.json == {"resource_id": resource_id}

Expand Down Expand Up @@ -669,9 +669,9 @@ def test_duplicate_blueprint(app):
app.blueprint(bp1)

assert str(excinfo.value) == (
'A blueprint with the name "{}" is already registered. '
f'A blueprint with the name "{bp_name}" is already registered. '
"Blueprint names must be unique."
).format(bp_name)
)


@pytest.mark.parametrize("debug", [True, False, None])
Expand Down
9 changes: 6 additions & 3 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
def test_cookies(app):
@app.route("/")
def handler(request):
response = text("Cookies are: {}".format(request.cookies["test"]))
cookie_value = request.cookies["test"]
response = text(f"Cookies are: {cookie_value}")
response.cookies["right_back"] = "at you"
return response

Expand All @@ -31,7 +32,8 @@ def handler(request):
async def test_cookies_asgi(app):
@app.route("/")
def handler(request):
response = text("Cookies are: {}".format(request.cookies["test"]))
cookie_value = request.cookies["test"]
response = text(f"Cookies are: {cookie_value}")
response.cookies["right_back"] = "at you"
return response

Expand Down Expand Up @@ -78,7 +80,8 @@ def handler(request):
def test_http2_cookies(app):
@app.route("/")
async def handler(request):
response = text("Cookies are: {}".format(request.cookies["test"]))
cookie_value = request.cookies["test"]
response = text(f"Cookies are: {cookie_value}")
return response

headers = {"cookie": "test=working!"}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_exceptions_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def handler_6(request, arg):
try:
foo = 1 / arg
except Exception as e:
raise e from ValueError("{}".format(arg))
raise e from ValueError(f"{arg}")
return text(foo)


Expand Down
14 changes: 5 additions & 9 deletions tests/test_keep_alive_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ def _collect_request(request):
):
url = uri
else:
uri = uri if uri.startswith("/") else "/{uri}".format(uri=uri)
uri = uri if uri.startswith("/") else f"/{uri}"
scheme = "http"
url = "{scheme}://{host}:{port}{uri}".format(
scheme=scheme, host=HOST, port=PORT, uri=uri
)
url = f"{scheme}://{HOST}:{PORT}{uri}"

@self.app.listener("after_server_start")
async def _collect_response(loop):
Expand Down Expand Up @@ -134,7 +132,7 @@ async def _collect_response(loop):
self.app.listeners["after_server_start"].pop()

if exceptions:
raise ValueError("Exception during request: {}".format(exceptions))
raise ValueError(f"Exception during request: {exceptions}")

if gather_request:
self.app.request_middleware.pop()
Expand All @@ -143,16 +141,14 @@ async def _collect_response(loop):
return request, response
except Exception:
raise ValueError(
"Request and response object expected, got ({})".format(
results
)
f"Request and response object expected, got ({results})"
)
else:
try:
return results[-1]
except Exception:
raise ValueError(
"Request object expected, got ({})".format(results)
f"Request object expected, got ({results})"
)

def kill_server(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_logo_false(app, caplog):
assert caplog.record_tuples[ROW][1] == logging.INFO
assert caplog.record_tuples[ROW][
2
] == "Goin' Fast @ http://127.0.0.1:{}".format(PORT)
] == f"Goin' Fast @ http://127.0.0.1:{PORT}"


def test_logo_true(app, caplog):
Expand Down
20 changes: 10 additions & 10 deletions tests/test_named_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def test_versioned_named_routes_get(app, method):
bp = Blueprint("test_bp", url_prefix="/bp")

method = method.lower()
route_name = "route_{}".format(method)
route_name2 = "route2_{}".format(method)
route_name = f"route_{method}"
route_name2 = f"route2_{method}"

func = getattr(app, method)
if callable(func):

@func("/{}".format(method), version=1, name=route_name)
@func(f"/{method}", version=1, name=route_name)
def handler(request):
return text("OK")

Expand All @@ -38,7 +38,7 @@ def handler(request):
func = getattr(bp, method)
if callable(func):

@func("/{}".format(method), version=1, name=route_name2)
@func(f"/{method}", version=1, name=route_name2)
def handler2(request):
return text("OK")

Expand All @@ -48,14 +48,14 @@ def handler2(request):

app.blueprint(bp)

assert app.router.routes_all["/v1/{}".format(method)].name == route_name
assert app.router.routes_all[f"/v1/{method}"].name == route_name

route = app.router.routes_all["/v1/bp/{}".format(method)]
assert route.name == "test_bp.{}".format(route_name2)
route = app.router.routes_all[f"/v1/bp/{method}"]
assert route.name == f"test_bp.{route_name2}"

assert app.url_for(route_name) == "/v1/{}".format(method)
url = app.url_for("test_bp.{}".format(route_name2))
assert url == "/v1/bp/{}".format(method)
assert app.url_for(route_name) == f"/v1/{method}"
url = app.url_for(f"test_bp.{route_name2}")
assert url == f"/v1/bp/{method}"
with pytest.raises(URLBuildError):
app.url_for("handler")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ def test_redirect_with_params(app, test_str):

@app.route("/api/v1/test/<test>/")
async def init_handler(request, test):
return redirect("/api/v2/test/{}/".format(use_in_uri))
return redirect(f"/api/v2/test/{use_in_uri}/")

@app.route("/api/v2/test/<test>/")
async def target_handler(request, test):
assert test == test_str
return text("OK")

_, response = app.test_client.get("/api/v1/test/{}/".format(use_in_uri))
_, response = app.test_client.get(f"/api/v1/test/{use_in_uri}/")
assert response.status == 200

assert response.content == b"OK"
24 changes: 12 additions & 12 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def handler(request):
def test_ip(app):
@app.route("/")
def handler(request):
return text("{}".format(request.ip))
return text(f"{request.ip}")

request, response = app.test_client.get("/")

Expand All @@ -55,7 +55,7 @@ def handler(request):
async def test_ip_asgi(app):
@app.route("/")
def handler(request):
return text("{}".format(request.url))
return text(f"{request.url}")

request, response = await app.asgi_client.get("/")

Expand Down Expand Up @@ -325,7 +325,7 @@ async def handler(request):
token = "a1d895e0-553a-421a-8e22-5ff8ecb48cbf"
headers = {
"content-type": "application/json",
"Authorization": "{}".format(token),
"Authorization": f"{token}",
}

request, response = app.test_client.get("/", headers=headers)
Expand All @@ -335,7 +335,7 @@ async def handler(request):
token = "a1d895e0-553a-421a-8e22-5ff8ecb48cbf"
headers = {
"content-type": "application/json",
"Authorization": "Token {}".format(token),
"Authorization": f"Token {token}",
}

request, response = app.test_client.get("/", headers=headers)
Expand All @@ -345,7 +345,7 @@ async def handler(request):
token = "a1d895e0-553a-421a-8e22-5ff8ecb48cbf"
headers = {
"content-type": "application/json",
"Authorization": "Bearer {}".format(token),
"Authorization": f"Bearer {token}",
}

request, response = app.test_client.get("/", headers=headers)
Expand All @@ -370,7 +370,7 @@ async def handler(request):
token = "a1d895e0-553a-421a-8e22-5ff8ecb48cbf"
headers = {
"content-type": "application/json",
"Authorization": "{}".format(token),
"Authorization": f"{token}",
}

request, response = await app.asgi_client.get("/", headers=headers)
Expand All @@ -380,7 +380,7 @@ async def handler(request):
token = "a1d895e0-553a-421a-8e22-5ff8ecb48cbf"
headers = {
"content-type": "application/json",
"Authorization": "Token {}".format(token),
"Authorization": f"Token {token}",
}

request, response = await app.asgi_client.get("/", headers=headers)
Expand All @@ -390,7 +390,7 @@ async def handler(request):
token = "a1d895e0-553a-421a-8e22-5ff8ecb48cbf"
headers = {
"content-type": "application/json",
"Authorization": "Bearer {}".format(token),
"Authorization": f"Bearer {token}",
}

request, response = await app.asgi_client.get("/", headers=headers)
Expand Down Expand Up @@ -1028,7 +1028,7 @@ async def handler(request):

app.add_route(handler, path)

request, response = app.test_client.get(path + "?{}".format(query))
request, response = app.test_client.get(path + f"?{query}")
assert request.url == expected_url.format(HOST, PORT)

parsed = urlparse(request.url)
Expand All @@ -1054,7 +1054,7 @@ async def handler(request):

app.add_route(handler, path)

request, response = await app.asgi_client.get(path + "?{}".format(query))
request, response = await app.asgi_client.get(path + f"?{query}")
assert request.url == expected_url.format(ASGI_HOST)

parsed = urlparse(request.url)
Expand Down Expand Up @@ -1087,7 +1087,7 @@ async def handler(request):
app.add_route(handler, path)

request, response = app.test_client.get(
"https://{}:{}".format(HOST, PORT) + path + "?{}".format(query),
f"https://{HOST}:{PORT}" + path + f"?{query}",
server_kwargs={"ssl": context},
)
assert request.url == expected_url.format(HOST, PORT)
Expand Down Expand Up @@ -1122,7 +1122,7 @@ async def handler(request):
app.add_route(handler, path)

request, response = app.test_client.get(
"https://{}:{}".format(HOST, PORT) + path + "?{}".format(query),
f"https://{HOST}:{PORT}" + path + f"?{query}",
server_kwargs={"ssl": ssl_dict},
)
assert request.url == expected_url.format(HOST, PORT)
Expand Down
Loading

0 comments on commit 16961fa

Please sign in to comment.