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

re.error: bad character in group name when using custom formatter in a tag #225

Closed
asparagusbeef opened this issue May 11, 2024 · 3 comments
Assignees
Labels
bug Something isn't working wait for release

Comments

@asparagusbeef
Copy link

asparagusbeef commented May 11, 2024

This is my code:

@cache(
	ttl=cache_control_ttl(default="7d"), 
	key="user:me:user_id:{token.credentials:auth_jwt()}", 
	tags=["user", "me", "user_id:{token.credentials:auth_jwt()}"]
)
@userRouter.get("/me", tags=['user'], response_model=User_Response)
async def me(client: AsyncClient = Depends(get_http_client), token: HTTPAuthorizationCredentials = Depends(bearer)) -> User_Response:
	response = await client.get(f"{DB_API_URL}/user/me", headers={"Authorization": f"Bearer {token.credentials}"})
	return await handle_web_response(response)

I also tried

@cache(
	ttl=cache_control_ttl(default="7d"), 
	key="user:me:user_id:{token.credentials:auth_jwt()}", 
	tags=["user", "me", "user_id:{auth_jwt(token.credentials)}"]
)

auth_jwt() is defined like so:

@default_formatter.register("auth_jwt")
def _auth_jwt(jwt: str) -> str:
    return authenticate_token(jwt)

Full Traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/src/venv/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 78, in subprocess_started
    target(sockets=sockets)
  File "/src/venv/lib/python3.11/site-packages/uvicorn/server.py", line 65, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/src/venv/lib/python3.11/site-packages/uvicorn/server.py", line 69, in serve
    await self._serve(sockets)
  File "/src/venv/lib/python3.11/site-packages/uvicorn/server.py", line 76, in _serve
    config.load()
  File "/src/venv/lib/python3.11/site-packages/uvicorn/config.py", line 433, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/venv/lib/python3.11/site-packages/uvicorn/importer.py", line 19, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/src/app/main.py", line 33, in <module>
    from .api.v1.routers.user_router import userRouter, userAnonRouter
  File "/src/app/api/v1/routers/user_router.py", line 105, in <module>
    @cache(
     ^^^^^^
  File "/src/venv/lib/python3.11/site-packages/cashews/wrapper/decorators.py", line 50, in _decorator
    decorator = decorator_fabric(self, **decor_kwargs)(func)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/venv/lib/python3.11/site-packages/cashews/decorators/cache/simple.py", line 43, in _decor
    backend.register_tag(tag, _key_template)
  File "/src/venv/lib/python3.11/site-packages/cashews/wrapper/tags.py", line 72, in register_tag
    self._tags_registry.register_tag(tag, key_template)
  File "/src/venv/lib/python3.11/site-packages/cashews/wrapper/tags.py", line 19, in register_tag
    self._registry_template[tag].append(template_to_re_pattern(key_template))
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/venv/lib/python3.11/site-packages/cashews/formatter.py", line 175, in template_to_re_pattern
    return re.compile("^" + pattern + "$", flags=re.MULTILINE)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/__init__.py", line 227, in compile
    return _compile(pattern, flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/__init__.py", line 294, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/_compiler.py", line 745, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/_parser.py", line 989, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/_parser.py", line 464, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/_parser.py", line 730, in _parse
    source.checkgroupname(name, 1, nested)
  File "/usr/local/lib/python3.11/re/_parser.py", line 307, in checkgroupname
    raise self.error(msg, len(name) + offset)
re.error: bad character in group name 'token\\_credentials' at position 21

Perhaps I am doing something incorrectly but I couldn't find in the docs a reference to using a custom formatter (registered with cashews.default_formatter.register) in a key + tagging it. Assistance will be much appreciated!

@Krukov Krukov self-assigned this May 11, 2024
@Krukov Krukov added the bug Something isn't working label May 11, 2024
@Krukov
Copy link
Owner

Krukov commented May 11, 2024

Hello !

Your code looks correct, that is just a bug in library. Unfortunately I ( and probably others ) do not use tags in this way, with advanced templates. That is why it is not stable and have a bugs. So thank you for reporting this issue, it leads to improvements in the library.

The fix is on the way. I will let you know when the fix is released

@Krukov
Copy link
Owner

Krukov commented May 12, 2024

Should work with 7.1.0 ( I've just released). Please reopen it the issue still exist

@Krukov Krukov closed this as completed May 12, 2024
@asparagusbeef
Copy link
Author

Thank you so much for the responsiveness and for making this library! It's really appreciated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wait for release
Projects
None yet
Development

No branches or pull requests

2 participants