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
2 people authored and aminalaee committed Feb 13, 2023
1 parent 1115ff3 commit f2ccd05
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
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
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 f2ccd05

Please sign in to comment.