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

issue a warning if a static view is referencing a package that doesn't exist #3752

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ Backward Incompatibilities
These have been deprecated in Python's gettext module since 3.8, and
removed in Python 3.11.

Deprecations
------------

- Deprecated the ability to use a non-existent package with
``pyramid.config.Configurator.add_static_view`` and
``pyramid.static.static_view``. This can be fixed by choosing a path
located within a real package as the ``root_dir`` for your static files.
This is almost always either a misconfig or an attempt to define an alias
location for use with ``pyramid.config.Configurator.override_asset``.
See https://github.com/Pylons/pyramid/pull/3752

Documentation Changes
---------------------

Expand Down
14 changes: 14 additions & 0 deletions src/pyramid/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from os.path import exists, getmtime, getsize, isdir, join, normcase, normpath
from pkg_resources import resource_exists, resource_filename, resource_isdir
import warnings

from pyramid.asset import abspath_from_asset_spec, resolve_asset_spec
from pyramid.httpexceptions import HTTPMovedPermanently, HTTPNotFound
Expand Down Expand Up @@ -92,6 +93,19 @@ def __init__(
if package_name is None:
package_name = caller_package().__name__
package_name, docroot = resolve_asset_spec(root_dir, package_name)
if package_name:
try:
__import__(package_name)
except ImportError:
warnings.warn(
f'A "pyramid.static.static_view" is being created with an'
f' asset spec referencing a package "{package_name}" that'
f' does not exist. This will break in the future.'
f' If this is done to override an asset, you must adjust'
f' this to override a location inside a real package.',
DeprecationWarning,
stacklevel=2,
)
self.use_subpath = use_subpath
self.package_name = package_name
self.docroot = docroot
Expand Down