Skip to content

Commit

Permalink
feat: reduce dev-time warnings with inheritance (#16984)
Browse files Browse the repository at this point in the history
  • Loading branch information
miketheman authored Oct 28, 2024
1 parent 7b95714 commit 4ae96f9
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 20 deletions.
2 changes: 2 additions & 0 deletions docs/dev/development/patterns.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Adding a new service
3. (Optional) Create other implementations of the interface. For instance, many services in ``warehouse``
also provide a ``NullService`` version used for development. These Null implementations only
provide basic functionalities without verifications and reduce the need for stubs in tests.
When implementing a warning for using the service, subclass the exception from
``warehouse.utils.exceptions.DevelopmentModeWarning`` to minimize warnings in the test logs.

Any new implementation must implement the complete interface, including all its methods and attributes.

Expand Down
13 changes: 5 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ cache_dir = "dev/.mypy_cache"
[[tool.mypy.overrides]]
# These modules do not yet have types available.
module = [
"automat.*",
"bpython.*", # https://github.com/bpython/bpython/issues/892
"b2sdk.*", # https://github.com/Backblaze/b2-sdk-python/issues/148
"celery.app.backends.*",
"celery.backends.redis.*",
"github_reserved_names.*",
"github_reserved_names.*", # https://github.com/Julian/github-reserved-names/pull/10
"google.cloud.*",
"forcediphttpsadapter.*",
"IPython.*", # has types, but only installed in dev
"packaging_legacy.*",
"packaging_legacy.*", # https://github.com/di/packaging_legacy/pull/5
"paginate.*",
"paginate_sqlalchemy.*",
"premailer.*",
Expand All @@ -71,10 +70,11 @@ module = [
"pyramid_rpc.*",
"pyqrcode.*",
"requests_aws4auth.*", # https://github.com/tedder/requests-aws4auth/issues/53
"rfc3986.*",
"rfc3986.*", # https://github.com/python-hyper/rfc3986/issues/122
"transaction.*",
"ua_parser.*", # https://github.com/ua-parser/uap-python/issues/110
"venusian.*",
"whitenoise.*", # https://github.com/evansd/whitenoise/pull/410
"zope.sqlalchemy.*",
]
ignore_missing_imports = true
Expand All @@ -93,10 +93,7 @@ markers = [
'functional: Slower running tests which test the entire system is functioning.',
]
filterwarnings = [
'ignore::warehouse.admin.services.InsecureStorageWarning',
'ignore::warehouse.utils.exceptions.InsecureIntegrityServiceWarning',
'ignore::warehouse.utils.exceptions.InsecureOIDCPublisherWarning',
'ignore::warehouse.packaging.services.InsecureStorageWarning',
'ignore::warehouse.utils.exceptions.DevelopmentModeWarning',
'error:SELECT statement has a cartesian product:sqlalchemy.exc.SAWarning',
]

Expand Down
6 changes: 3 additions & 3 deletions requirements/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,9 @@ limits==3.13.0 \
--hash=sha256:6571b0c567bfa175a35fed9f8a954c0c92f1c3200804282f1b8f1de4ad98a953 \
--hash=sha256:9767f7233da4255e9904b79908a728e8ec0984c0b086058b4cbbd309aea553f6
# via -r requirements/main.in
linehaul==1.0.1 \
--hash=sha256:09d71b1f6a9ab92dd8c763b3d099e4ae05c2845ee783a02d5fe731e6e2a6a997 \
--hash=sha256:d19ca669008dad910868dfae7f904dfc5362583729bda344799cf7ea2ad5ef12
linehaul==1.0.2 \
--hash=sha256:4545fee3b54df22c697d204da8668e8fd975afcb28cbb5654865e43491c70ff0 \
--hash=sha256:e2f91cda162ed05d3053426ac21e1546313513d4fbaeda74d085b7b54a2cb914
# via -r requirements/main.in
logfury==1.0.1 \
--hash=sha256:130a5daceab9ad534924252ddf70482aa2c96662b3a3825a7d30981d03b76a26 \
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

import warehouse

from warehouse import admin, config, email, static
from warehouse import admin, config, static
from warehouse.accounts import services as account_services
from warehouse.accounts.interfaces import ITokenService, IUserService
from warehouse.admin.flags import AdminFlag, AdminFlagValue
Expand Down Expand Up @@ -679,7 +679,7 @@ def send_email(pyramid_request, monkeypatch):
lambda *args, **kwargs: send_email_stub
)
pyramid_request.registry.settings = {"mail.sender": "noreply@example.com"}
monkeypatch.setattr(email, "send_email", send_email_stub)
monkeypatch.setattr(warehouse.email, "send_email", send_email_stub)
return send_email_stub


Expand Down
3 changes: 2 additions & 1 deletion warehouse/admin/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
from zope.interface import implementer

from warehouse.admin.interfaces import ISponsorLogoStorage
from warehouse.utils.exceptions import DevelopmentModeWarning


class InsecureStorageWarning(UserWarning):
class InsecureStorageWarning(DevelopmentModeWarning):
pass


Expand Down
7 changes: 4 additions & 3 deletions warehouse/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ def record_event(self, *, tag, request: Request, additional=None):
additional = additional or {}
additional["user_agent_info"] = {
"installer": "Browser",
"device": parsed_user_agent["device"]["family"],
"os": parsed_user_agent["os"]["family"],
"user_agent": parsed_user_agent["user_agent"]["family"],
# See https://github.com/pypi/linehaul-cloud-function/issues/203
"device": parsed_user_agent["device"]["family"], # type: ignore[index] # noqa: E501
"os": parsed_user_agent["os"]["family"], # type: ignore[index]
"user_agent": parsed_user_agent["user_agent"]["family"], # type: ignore[index] # noqa: E501
}
else:
additional = additional or {}
Expand Down
3 changes: 2 additions & 1 deletion warehouse/packaging/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
Role,
)
from warehouse.rate_limiting import DummyRateLimiter, IRateLimiter
from warehouse.utils.exceptions import DevelopmentModeWarning
from warehouse.utils.project import PROJECT_NAME_RE

logger = logging.getLogger(__name__)
Expand All @@ -74,7 +75,7 @@ def _namespace_stdlib_list(module_list):
}


class InsecureStorageWarning(UserWarning):
class InsecureStorageWarning(DevelopmentModeWarning):
pass


Expand Down
8 changes: 6 additions & 2 deletions warehouse/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
# limitations under the License.


class InsecureOIDCPublisherWarning(UserWarning):
class DevelopmentModeWarning(UserWarning):
pass


class InsecureIntegrityServiceWarning(UserWarning):
class InsecureOIDCPublisherWarning(DevelopmentModeWarning):
pass


class InsecureIntegrityServiceWarning(DevelopmentModeWarning):
pass

0 comments on commit 4ae96f9

Please sign in to comment.