From d0eedd7e191ac36d503b627cdff83cd106378890 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Tue, 30 Nov 2021 19:14:29 -0500 Subject: [PATCH] Handle some Deprecations (#10410) * Fix PytestCollectionWarning When collecting tests, this class raises a warning since it's named `Test*`: PytestCollectionWarning: cannot collect test class 'TestAdminFlagValues' because it has a __new__ constructor Set an attribute that informs pytest to ignore this class, and thus remove the warning. Refs: https://docs.pytest.org/en/6.2.x/example/pythoncollection.html#customizing-test-collection (last lines) Signed-off-by: Mike Fiedler * Fix PytestDeprecationWarning Current test warns: PytestDeprecationWarning: The --strict option is deprecated, use --strict-markers instead. Marked as deprecated in https://github.com/pytest-dev/pytest/pull/7985 Released in pytest 6.2.0 Signed-off-by: Mike Fiedler * Fix DeprecationWarning pyramid import The import paths for these modules has changed in Pyramid 2.0, and raises warnings. There are other Pyramid 2.0 warnings, but are more involved than changing an import path, and should be addressed as another commit. Refs: https://github.com/Pylons/pyramid/pull/3563 Signed-off-by: Mike Fiedler * Fix DeprecationWarning for jinja.contextfilter A few instances of this warning are raised: DeprecationWarning: 'contextfilter' is renamed to 'pass_context', the old name will be removed in Jinja 3.1. Replace the usages accordingly. Refs: https://github.com/pallets/jinja/pull/1389 Signed-off-by: Mike Fiedler * Fix DeprecationWarning for jinja.Markup The pass-through import of markup.Markup() has been deprecated in Jinja 3.0, and will be removed in Jinja 3.1. Warnings raised: DeprecationWarning: 'jinja2.Markup' is deprecated and will be removed in Jinja 3.1. Import 'markupsafe.Markup' instead. Replace the import paths. TODO: Determine if the requirements/main.in needs to be changed as well. Refs: https://github.com/pallets/jinja/pull/1391 Signed-off-by: Mike Fiedler * Fix DeprecationWarning for babel.numbers.from_number Warnings raised: DeprecationWarning: Use babel.numbers.format_decimal() instead. Deprecated since 2.6.0 Refs: https://github.com/python-babel/babel/pull/538 Signed-off-by: Mike Fiedler Co-authored-by: Dustin Ingram --- bin/tests | 2 +- tests/unit/admin/test_flags.py | 1 + tests/unit/i18n/test_filters.py | 2 +- tests/unit/packaging/test_models.py | 2 +- tests/unit/test_config.py | 2 +- warehouse/accounts/forms.py | 8 ++++---- warehouse/config.py | 2 +- warehouse/filters.py | 2 +- warehouse/i18n/filters.py | 10 +++++----- warehouse/packaging/models.py | 2 +- warehouse/policy.py | 4 ++-- 11 files changed, 19 insertions(+), 18 deletions(-) diff --git a/bin/tests b/bin/tests index 1363c9b5cfd2..c6449742c4a5 100755 --- a/bin/tests +++ b/bin/tests @@ -33,6 +33,6 @@ fi set -x # Actually run our tests. -python -m coverage run -m pytest --strict $COMMAND_ARGS +python -m coverage run -m pytest --strict-markers $COMMAND_ARGS python -m coverage html python -m coverage report -m --fail-under 100 diff --git a/tests/unit/admin/test_flags.py b/tests/unit/admin/test_flags.py index e03f64f72c31..24cba047b4ca 100644 --- a/tests/unit/admin/test_flags.py +++ b/tests/unit/admin/test_flags.py @@ -16,6 +16,7 @@ class TestAdminFlagValues(enum.Enum): + __test__ = False NOT_A_REAL_FLAG = "not-a-real-flag" THIS_FLAG_IS_ENABLED = "this-flag-is-enabled" diff --git a/tests/unit/i18n/test_filters.py b/tests/unit/i18n/test_filters.py index 908e4c01bc95..1344f1e9fbdd 100644 --- a/tests/unit/i18n/test_filters.py +++ b/tests/unit/i18n/test_filters.py @@ -70,7 +70,7 @@ def test_format_rfc822_datetime(monkeypatch): def test_format_number(monkeypatch): formatted = pretend.stub() format_number = pretend.call_recorder(lambda *a, **kw: formatted) - monkeypatch.setattr(babel.numbers, "format_number", format_number) + monkeypatch.setattr(babel.numbers, "format_decimal", format_number) request = pretend.stub(locale=pretend.stub()) ctx = pretend.stub(get=pretend.call_recorder(lambda k: request)) diff --git a/tests/unit/packaging/test_models.py b/tests/unit/packaging/test_models.py index caf764d6fab0..39a30a49f93b 100644 --- a/tests/unit/packaging/test_models.py +++ b/tests/unit/packaging/test_models.py @@ -15,8 +15,8 @@ import pretend import pytest +from pyramid.authorization import Allow from pyramid.location import lineage -from pyramid.security import Allow from warehouse.packaging.models import Dependency, DependencyKind, File, ProjectFactory diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index d53ae201a723..4a6c360003ef 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -16,8 +16,8 @@ import pytest from pyramid import renderers +from pyramid.authorization import Allow, Authenticated from pyramid.httpexceptions import HTTPForbidden, HTTPUnauthorized -from pyramid.security import Allow, Authenticated from pyramid.tweens import EXCVIEW from warehouse import config diff --git a/warehouse/accounts/forms.py b/warehouse/accounts/forms.py index a9230a5534f2..649e3560333f 100644 --- a/warehouse/accounts/forms.py +++ b/warehouse/accounts/forms.py @@ -16,7 +16,7 @@ from email.headerregistry import Address import disposable_email_domains -import jinja2 +import markupsafe import wtforms import wtforms.fields import wtforms.fields.html5 @@ -181,7 +181,7 @@ def validate_new_password(self, field): field.data, tags=["method:new_password"] ): raise wtforms.validators.ValidationError( - jinja2.Markup(self._breach_service.failure_message) + markupsafe.Markup(self._breach_service.failure_message) ) @@ -278,7 +278,7 @@ def validate_password(self, field): is_disabled, disabled_for = self.user_service.is_disabled(userid) if is_disabled and disabled_for == DisableReason.CompromisedPassword: raise wtforms.validators.ValidationError( - jinja2.Markup(self.breach_service.failure_message) + markupsafe.Markup(self.breach_service.failure_message) ) # Do our typical validation of the password. @@ -297,7 +297,7 @@ def validate_password(self, field): user.id, reason=DisableReason.CompromisedPassword ) raise wtforms.validators.ValidationError( - jinja2.Markup(self.breach_service.failure_message) + markupsafe.Markup(self.breach_service.failure_message) ) diff --git a/warehouse/config.py b/warehouse/config.py index 1b0403d1ae6f..79368e98837b 100644 --- a/warehouse/config.py +++ b/warehouse/config.py @@ -18,9 +18,9 @@ import transaction from pyramid import renderers +from pyramid.authorization import Allow, Authenticated from pyramid.config import Configurator as _Configurator from pyramid.response import Response -from pyramid.security import Allow, Authenticated from pyramid.tweens import EXCVIEW from pyramid_rpc.xmlrpc import XMLRPCRenderer diff --git a/warehouse/filters.py b/warehouse/filters.py index 037d3157835f..8265c723e11b 100644 --- a/warehouse/filters.py +++ b/warehouse/filters.py @@ -63,7 +63,7 @@ def _camo_url(request, url): return urllib.parse.urljoin(camo_url, path) -@jinja2.contextfilter +@jinja2.pass_context def camoify(ctx, value): request = ctx.get("request") or get_current_request() diff --git a/warehouse/i18n/filters.py b/warehouse/i18n/filters.py index 3b57998204e9..1aa407a50e9b 100644 --- a/warehouse/i18n/filters.py +++ b/warehouse/i18n/filters.py @@ -19,28 +19,28 @@ from pyramid.threadlocal import get_current_request -@jinja2.contextfilter +@jinja2.pass_context def format_date(ctx, *args, **kwargs): request = ctx.get("request") or get_current_request() kwargs.setdefault("locale", request.locale) return babel.dates.format_date(*args, **kwargs) -@jinja2.contextfilter +@jinja2.pass_context def format_datetime(ctx, *args, **kwargs): request = ctx.get("request") or get_current_request() kwargs.setdefault("locale", request.locale) return babel.dates.format_datetime(*args, **kwargs) -@jinja2.contextfilter +@jinja2.pass_context def format_rfc822_datetime(ctx, dt, *args, **kwargs): return email.utils.formatdate(dt.timestamp(), usegmt=True) -@jinja2.contextfilter +@jinja2.pass_context def format_number(ctx, number, locale=None): request = ctx.get("request") or get_current_request() if locale is None: locale = request.locale - return babel.numbers.format_number(number, locale=locale) + return babel.numbers.format_decimal(number, locale=locale) diff --git a/warehouse/packaging/models.py b/warehouse/packaging/models.py index 1e40732f54d7..95cc89d6c474 100644 --- a/warehouse/packaging/models.py +++ b/warehouse/packaging/models.py @@ -18,7 +18,7 @@ import packaging.utils from citext import CIText -from pyramid.security import Allow +from pyramid.authorization import Allow from pyramid.threadlocal import get_current_request from sqlalchemy import ( BigInteger, diff --git a/warehouse/policy.py b/warehouse/policy.py index 69ce173dfdd0..51c0f5c23045 100644 --- a/warehouse/policy.py +++ b/warehouse/policy.py @@ -13,7 +13,7 @@ import os.path import html5lib -import jinja2 +import markupsafe import mistune import warehouse @@ -39,7 +39,7 @@ def markdown_view(request): title = html.find("//h1[1]").text - return {"title": title, "html": jinja2.Markup(rendered)} + return {"title": title, "html": markupsafe.Markup(rendered)} return markdown_view