Skip to content

Commit

Permalink
Fixed bug where deprecation warnings weren't being logged
Browse files Browse the repository at this point in the history
fixes #8499
  • Loading branch information
David Davis authored and bmbouter committed Apr 2, 2021
1 parent d55f1c2 commit 3543838
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGES/8499.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Deprecation warnings are now being logged by default if the log level includes WARNING. This can be
disabled by adjusting the log level of ``pulpcore.deprecation``. See the deprecation docs for more
information.
20 changes: 16 additions & 4 deletions docs/plugins/plugin-writer/concepts/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ In 3.8 the following changes happen:
2. The existing method signature ``def foo(a, b)`` is left in-tact.
3. The ``foo`` method would have the a Python ``DeprecationWarning`` added to it such as::

warnings.warn("foo() is deprecated and will be removed in pulpcore==3.9; use the_new_foo().", DeprecationWarning)
from pulpcore.app.logging import deprecation_logger
deprecation_logger.warn("foo() is deprecated and will be removed in pulpcore==3.9; use the_new_foo().")

4. A ``CHANGES/plugin_api/XXXX.deprecation`` changelog entry is created explaining how to port
plugin code onto the new call interface.
Expand All @@ -413,14 +414,25 @@ Then in 3.9 the following happens:

.. note::

Python ``DeprecationWarning`` log statements are shown to users of your plugin when using a
deprecated call interface. This is by design to raise general awareness that the code in-use
will eventually be removed.
Deprecation log statements are shown to users of your plugin when using a deprecated call
interface. This is by design to raise general awareness that the code in-use will eventually be
removed.

This also applies to models importable from ``pulpcore.plugin.models``. For example, an attribute
that is being renamed or removed would follow a similar deprecation process described above to allow
plugin code one release cycle to update their code compatibility.

Logging of deprecation warnings can be disabled by raising the log level for the
``pulpcore.deprecation`` logger in the pulpcore settings file::

LOGGING = {
# ...
"loggers": {
"pulpcore.deprecation": {
"level": "ERROR",
}
}


.. _plugin_installation:

Expand Down
4 changes: 4 additions & 0 deletions pulpcore/app/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import logging


deprecation_logger = logging.getLogger("pulpcore.deprecation")
7 changes: 3 additions & 4 deletions pulpcore/app/models/publication.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from gettext import gettext as _
import warnings

from django.db import IntegrityError, models, transaction

Expand All @@ -8,6 +7,7 @@
from .repository import Remote, Repository, RepositoryVersion
from .task import CreatedResource
from pulpcore.app.files import PulpTemporaryUploadedFile
from pulpcore.app.logging import deprecation_logger


class Publication(MasterModel):
Expand Down Expand Up @@ -291,12 +291,11 @@ class BaseDistribution(MasterModel):

def __init__(self, *args, **kwargs):
""" Initialize a BaseDistribution and emit DeprecationWarnings"""
warnings.warn(
deprecation_logger.warn(
_(
"BaseDistribution is deprecated and could be removed as early as pulpcore==3.13; "
"use pulpcore.plugin.models.Distribution instead."
),
DeprecationWarning,
)
)
return super().__init__(*args, **kwargs)

Expand Down
17 changes: 7 additions & 10 deletions pulpcore/app/serializers/publication.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from gettext import gettext as _
import warnings

from django.db.models import Q
from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from pulpcore.app import models
from pulpcore.app.logging import deprecation_logger
from pulpcore.app.serializers import (
BaseURLField,
DetailIdentityField,
Expand Down Expand Up @@ -156,12 +156,11 @@ class BaseDistributionSerializer(ModelSerializer, BasePathOverlapMixin):

def __init__(self, *args, **kwargs):
""" Initialize a BaseDistributionSerializer and emit DeprecationWarnings"""
warnings.warn(
deprecation_logger.warn(
_(
"BaseDistributionSerializer is deprecated and could be removed as early as "
"pulpcore==3.13; use pulpcore.plugin.serializers.DistributionSerializer instead."
),
DeprecationWarning,
)
)
return super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -299,13 +298,12 @@ class PublicationDistributionSerializer(BaseDistributionSerializer):

def __init__(self, *args, **kwargs):
""" Initialize a PublicationDistributionSerializer and emit DeprecationWarnings"""
warnings.warn(
deprecation_logger.warn(
_(
"PublicationDistributionSerializer is deprecated and could be removed as early as "
"pulpcore==3.13; use pulpcore.plugin.serializers.DistributionSerializer instead. "
"See its docstring for more details."
),
DeprecationWarning,
)
)
return super().__init__(*args, **kwargs)

Expand All @@ -328,13 +326,12 @@ class RepositoryVersionDistributionSerializer(BaseDistributionSerializer):

def __init__(self, *args, **kwargs):
""" Initialize a RepositoryVersionDistributionSerializer and emit DeprecationWarnings"""
warnings.warn(
deprecation_logger.warn(
_(
"PublicationDistributionSerializer is deprecated and could be removed as early as "
"pulpcore==3.13; use pulpcore.plugin.serializers.DistributionSerializer instead. "
"See its docstring for more details."
),
DeprecationWarning,
)
)
return super().__init__(*args, **kwargs)

Expand Down
12 changes: 5 additions & 7 deletions pulpcore/app/viewsets/publication.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from gettext import gettext as _
import warnings

from django_filters.rest_framework import DjangoFilterBackend, filters
from rest_framework import mixins
Expand All @@ -25,6 +24,7 @@
LabelSelectFilter,
RepositoryVersionFilter,
)
from pulpcore.app.logging import deprecation_logger


class PublicationFilter(BaseFilterSet):
Expand Down Expand Up @@ -104,12 +104,11 @@ class DistributionFilter(BaseFilterSet):

def __init__(self, *args, **kwargs):
""" Initialize a DistributionFilter and emit DeprecationWarnings"""
warnings.warn(
deprecation_logger.warn(
_(
"DistributionFilter is deprecated and could be removed as early as "
"pulpcore==3.13; use pulpcore.plugin.serializers.NewDistributionFilter instead."
),
DeprecationWarning,
)
)
return super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -160,12 +159,11 @@ class BaseDistributionViewSet(

def __init__(self, *args, **kwargs):
""" Initialize a BaseDistributionViewSet and emit DeprecationWarnings"""
warnings.warn(
deprecation_logger.warn(
_(
"BaseDistributionViewSet is deprecated and could be removed as early as "
"pulpcore==3.13; use pulpcore.plugin.viewsets.DistributionViewset instead."
),
DeprecationWarning,
)
)
return super().__init__(*args, **kwargs)

Expand Down
8 changes: 4 additions & 4 deletions pulpcore/tasking/storage.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import random
import shutil
import warnings
from gettext import gettext as _

from django.conf import settings
from rq.job import get_current_job

from pulpcore.app.logging import deprecation_logger


class _WorkingDir:
"""
Expand Down Expand Up @@ -187,12 +188,11 @@ def __init__(self):
Raises:
RuntimeError: When used outside of an RQ task.
"""
warnings.warn(
deprecation_logger.warn(
_(
"WorkingDirectory is deprecated and will be removed in pulpcore==3.12; "
'use tempfile.TemporaryDirectory(dir=".") instead.'
),
DeprecationWarning,
)
)
try:
job = get_current_job()
Expand Down

0 comments on commit 3543838

Please sign in to comment.