Skip to content

Modify MustUse to iterate the stack frame and collate options from all frames. #1621

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 5 additions & 3 deletions amaranth/_unused.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import warnings

from ._utils import get_linter_option
from ._utils import get_linter_option, collate_linter_options


__all__ = ["UnusedMustUse", "MustUse"]
Expand All @@ -18,7 +18,9 @@ class MustUse:
def __new__(cls, *args, src_loc_at=0, **kwargs):
frame = sys._getframe(1 + src_loc_at)
self = super().__new__(cls)
self._MustUse__used = False

self._MustUse__used = False
self._MustUse__options = collate_linter_options(frame)
self._MustUse__context = dict(
filename=frame.f_code.co_filename,
lineno=frame.f_lineno,
Expand All @@ -31,7 +33,7 @@ def __del__(self):
if getattr(self._MustUse__warning, "_MustUse__silence", False):
return
if hasattr(self, "_MustUse__used") and not self._MustUse__used:
if get_linter_option(self._MustUse__context["filename"],
if get_linter_option(self._MustUse__options,
self._MustUse__warning.__qualname__, bool, True):
warnings.warn_explicit(
f"{self!r} created but never used", self._MustUse__warning,
Expand Down
30 changes: 28 additions & 2 deletions amaranth/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def decorator_like(*args, **kwargs):
return decorator_like



def get_linter_options(filename):
first_line = linecache.getline(filename, 1)
if first_line:
Expand All @@ -84,8 +85,33 @@ def get_linter_options(filename):
return dict()


def get_linter_option(filename, name, type, default):
options = get_linter_options(filename)
def collate_linter_options(frame):
"""
Get all linter options for a given stack frame. This iterates down the frames, collating options of the form:

.. code::
# amaranth: {name}=value

The earliest option value in the stack takes precedence

Returns
-------
:class:`bool` or :class:`int`
Option value
"""

options = {}
while frame:
f_opts = get_linter_options(frame.f_code.co_filename)
options = f_opts | options
if frame.f_back is None:
break
else:
frame = frame.f_back
return options


def get_linter_option(options, name, type, default):
if name not in options:
return default

Expand Down
Loading