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

Pybrake raises depecration warnings with the FastAPI middleware #269

Open
pouellet opened this issue Aug 17, 2023 · 2 comments
Open

Pybrake raises depecration warnings with the FastAPI middleware #269

pouellet opened this issue Aug 17, 2023 · 2 comments

Comments

@pouellet
Copy link

Hi team,

Pybrake raises deprecation warnings when used with the FastAPI middleware:

pybrake/middleware/fastapi.py:1: DeprecationWarning: ExceptionMiddleware is deprecated on `starlette.exceptions`. Import it from `starlette.middleware.exceptions` instead.
    from .starlette import init_pybrake

pybrake/notifier.py:394: DeprecationWarning: Accessing attr.__version__ is deprecated and will be removed in a future release. Use importlib.metadata directly to query for attrs's packaging metadata.
    if hasattr(mod, "__version__"):

pybrake/notifier.py:395: DeprecationWarning: Accessing attr.__version__ is deprecated and will be removed in a future release. Use importlib.metadata directly to query for attrs's packaging metadata.
    versions[name] = mod.__version__

I am using the following versions:

Python 3.11.4

pybrake = "==1.10.1"
fastapi = "==0.100.0"
starlette = "==0.27.0"
@Maybe14
Copy link

Maybe14 commented Sep 26, 2023

I'm getting the same thing. Any updates?

@alokRamteke
Copy link

We can apply the patch like below to address this warning until pybrake provides a fix in future updates.

import importlib.metadata
import sys
from typing import Any, Dict

import pybrake.notifier


def get_version(name: str) -> str:
    """Get version using importlib.metadata."""
    return importlib.metadata.version(name)


def patched_build_context(self: Any) -> Dict[str, Any]:
    """Patched version of pybrake.notifier.Notifier._build_context."""
    ctx = self._context.copy()  # pylint: disable=W0212
    versions = ctx["versions"]

    for name, _ in sys.modules.items():
        if name.startswith("_"):
            continue
        try:
            # Use importlib.metadata.version to get the version of the module
            versions[name] = get_version(name)
        except importlib.metadata.PackageNotFoundError as exc:
            print(f"Failed to get version for {name}. Error: {str(exc)}")
            versions[name] = None

    return ctx


def apply_pybrake_patch() -> None:
    """Apply the monkey patch to the pybrake.notifier module."""
    pybrake.notifier.Notifier._build_context = patched_build_context  # pylint: disable=W0212

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants