Skip to content

Commit

Permalink
Fix FloatConvertor regex (#1973)
Browse files Browse the repository at this point in the history
* Bugfix of regex at FloatConvertor (version 2)

For passing your checks of #1942
A correct statement is: regex = r"[0-9]+(\.[0-9]+)?"
Reference: https://www.flake8rules.com/rules/W605.html

I have no problem to corrected without 'r' prefix directly at /site-packages/starlette/convertors.py of my computer.
Having submitted last pull-request, I realized to add a 'r' prefix to pass your tests.

* Add test

Co-authored-by: Ching <thus.kindly@gmail.com>
  • Loading branch information
Kludex and i-Ching authored Dec 3, 2022
1 parent fd1e91a commit 865efeb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion starlette/convertors.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def to_string(self, value: int) -> str:


class FloatConvertor(Convertor):
regex = "[0-9]+(.[0-9]+)?"
regex = r"[0-9]+(\.[0-9]+)?"

def convert(self, value: str) -> float:
return float(value)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_convertors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ def test_datetime_convertor(test_client_factory, app: Router):
app.url_path_for("datetime-convertor", param=datetime(1996, 1, 22, 23, 0, 0))
== "/datetime/1996-01-22T23:00:00"
)


@pytest.mark.parametrize("param, status_code", [("1.0", 200), ("1-0", 404)])
def test_default_float_convertor(test_client_factory, param: str, status_code: int):
def float_convertor(request):
param = request.path_params["param"]
assert isinstance(param, float)
return JSONResponse({"float": param})

app = Router(routes=[Route("/{param:float}", endpoint=float_convertor)])

client = test_client_factory(app)
response = client.get(f"/{param}")
assert response.status_code == status_code

0 comments on commit 865efeb

Please sign in to comment.