Fix linter to allow subscripted annotations (e.g. list[int]
, dict[str, int]
)
#114
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes an issue where our linter rejects subscripted annotations (e.g.
list[int]
) with an S16- Illegal argument annotation used error, even though the base type (list
,dict
, etc.) is in our allowed list of annotation types.Problem
ALLOWED_ANNOTATION_TYPES
. However, we only include strings like"list"
,"dict"
,"int"
, etc.list[int]
,dict[str, int]
), we store it as a string like"list[int]"
. Since"list[int]"
is not strictly inALLOWED_ANNOTATION_TYPES
, it triggers the S16 violation.Example code that triggers the error:
Result: The linter reports
Line X : S16- Illegal argument annotation used : list[int]
.Proposed Changes
list[int]
,dict[str,int]
, etc.) to recognize the base type (list
,dict
) during linting.annotation_types
method, if the string contains[
, extract the portion before it (e.g.,"list"
from"list[int]"
or"list[list[int]]"
). If that base is inALLOWED_ANNOTATION_TYPES
, allow the annotation.Minimal Example of the Annotation Check
With this tweak, the linter will permit subscripted forms of otherwise allowed types like
list
ordict
.Testing
Rationale and Benefits
list[int]
,dict[str, T]
, etc.) without artificially blocking them in the linter.list
,dict
, etc.) are allowed, but extends it gracefully to subscripted types.Additional Notes
ALLOWED_ANNOTATION_TYPES
. It merely makes the existing ones more flexible for subscript usage._get_annotation_name
or any similar logic) remains as is—this change focuses on the final check to see if a subscript is allowed.Type of change
Checklist