@@ -74,18 +74,39 @@ def decorator_like(*args, **kwargs):
74
74
return f (* args , ** kwargs )
75
75
return decorator_like
76
76
77
+ _LINT_PATTERN = re .compile (r"^#\s*amaranth:\s*((?:\w+=\w+\s*)(?:,\s*\w+=\w+\s*)*)\n$" )
78
+
77
79
78
80
def get_linter_options (filename ):
79
81
first_line = linecache .getline (filename , 1 )
80
82
if first_line :
81
- match = re .match (r"^#\s*amaranth:\s*((?:\w+=\w+\s*)(?:,\s*\w+=\w+\s*)*)\n$" , first_line )
83
+ match = _LINT_PATTERN .match (first_line )
82
84
if match :
83
85
return dict (map (lambda s : s .strip ().split ("=" , 2 ), match .group (1 ).split ("," )))
84
86
return dict ()
85
87
86
88
87
- def get_linter_option (filename , name , type , default ):
88
- options = get_linter_options (filename )
89
+ def get_linter_option (frame , name , type , default ):
90
+ """
91
+ Get given linter option for a given stack frame. This iterates down the frames, collating options of the form:
92
+ ```
93
+ # amaranth: {name} = value
94
+ ```
95
+ The earliest option value in the stack takes precedence
96
+
97
+ Returns:
98
+ option value: bool | int
99
+ """
100
+
101
+ options = {}
102
+ while frame :
103
+ f_opts = get_linter_options (frame .f_code .co_filename )
104
+ options = f_opts | options
105
+ if frame .f_back is None :
106
+ break
107
+ else :
108
+ frame = frame .f_back
109
+
89
110
if name not in options :
90
111
return default
91
112
0 commit comments