question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"ValueError: Invalid declaration" with routes containing file extensions

See original GitHub issue

Describe the bug

Starting in Sanic 21.6.1, routes with periods in them (e.g. "/<name>.txt") are no longer accepted:

$ python app/main.py

Traceback (most recent call last):
  File ".../python3.9/site-packages/sanic_routing/utils.py", line 73, in parts_to_path
    if match.group(2):
AttributeError: 'NoneType' object has no attribute 'group'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../app/main.py", line 7, in <module>
    def example(request, name):
  File ".../python3.9/site-packages/sanic/mixins/routes.py", line 161, in decorator
    self._apply_route(route)
  File ".../python3.9/site-packages/sanic/app.py", line 345, in _apply_route
    routes = self.router.add(**params)
  File ".../python3.9/site-packages/sanic/router.py", line 129, in add
    route = super().add(**params)  # type: ignore
  File ".../python3.9/site-packages/sanic_routing/router.py", line 194, in add
    path = parts_to_path(
  File ".../python3.9/site-packages/sanic_routing/utils.py", line 77, in parts_to_path
    raise ValueError(f"Invalid declaration: {part}")
ValueError: Invalid declaration: <name>.txt

Code snippet

from sanic import Sanic, response

app = Sanic("app")

@app.get("/<name>.txt")
def example(request, name):
    return response.text(f"Hello, {name}!")

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

Expected behavior

With sanic==21.6.0 and sanic-routing==0.7.0, the app is able to start but /foobar.txt is not routed:

$ python app/main.py
[INFO] Goin' Fast @ http://127.0.0.1:8000

$ curl http://localhost:8000/foobar.txt
<!DOCTYPE html><html lang=en><meta charset=UTF-8><title>⚠️ 404 — Not Found</title>

With sanic==20.12.3, the app both starts and correctly routes /foobar.txt to the function:

$ python app/main.py
[INFO] Goin' Fast @ http://127.0.0.1:8000

$ curl http://localhost:8000/foobar.txt
Hello, foobar!

Environment

  • OS: macOS 11.4
  • Python: 3.9.1
  • Version: sanic==21.6.1 and sanic-routing==0.7.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ahopkinscommented, Aug 1, 2021

See: https://github.com/sanic-org/sanic-routing/pull/47

New path parameter type:

@app.get(r"/<name:ext:txt>")
def example(request, name, ext):
    return response.text(f"Hello, {name}!")
1reaction
ahopkinscommented, Jul 31, 2021

I’m definitely a fan of your proposed change to sanic-routing over the alternatives:

I am hoping to get it in for next release if possible. There definitely are some challenges to allow for extracting multiple variables per segment. Not necessarily in the actual destructuring and matching, but creating a simple abstraction layer for defining the path parameter definitions without creating too much complexity or coupling.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid declaration" with routes containing file extensions
"ValueError: Invalid declaration" with routes containing file extensions. This issue has been tracked since 2021-07-30. Describe the bug.
Read more >
Handling input file extensions other than `.ts`, `.js`, `.tsx` ...
A .d.ts file meaning can not be changed, and a declaration file can ... (both TS and TSX) and regular TS extension files...
Read more >
Python glob multiple filetypes
This solution allows you to have a custom list of exts that can be changed without having to update your code.
Read more >
Handling File Uploads With Flask - miguelgrinberg.com
An attacker can upload files with viruses or other types of malware in a place where the application, for example, expects images. Limiting...
Read more >
doctest — Test interactive Python examples
To perform regression testing by verifying that interactive examples from a test file or a test object work as expected. To write tutorial...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found