@@ -74,18 +74,39 @@ def decorator_like(*args, **kwargs):
7474 return f (* args , ** kwargs )
7575 return decorator_like
7676
77+ _LINT_PATTERN = re .compile (r"^#\s*amaranth:\s*((?:\w+=\w+\s*)(?:,\s*\w+=\w+\s*)*)\n$" )
78+
7779
7880def get_linter_options (filename ):
7981 first_line = linecache .getline (filename , 1 )
8082 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 )
8284 if match :
8385 return dict (map (lambda s : s .strip ().split ("=" , 2 ), match .group (1 ).split ("," )))
8486 return dict ()
8587
8688
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+
89110 if name not in options :
90111 return default
91112
0 commit comments