Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FloatConvertor regex #1973

Merged
merged 4 commits into from Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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