Open NeilGirdhar opened 1 year ago
It would be nice to also support associating comment directives from preceding otherwise empty lines, e.g., like this.
Personally, I like the suggestion by @jhance in this comment better: Add a flag to "type: ignore" to indicate which type checker(s) it applies to. This would need no ast changes, only an amendment of the typing spec. The latter chapter need updating anyway, since it doesn't reflect the current reality of "# type: ignore" comments.
@srittau Right, the downside of that approach is that some other linter or tool may want to parse something else. Also, pyright and Ruff will probably never change what they respond to (and while they don't use AST, some tool may want to parse their directives?).
I guess I'm an idealist, and I want a perfect solution, but I agree that your suggestion is easier to get to from where we are. Also, your solution doesn't preclude one day implementing the "perfect solution", which is nice.
The latter chapter need updating anyway, since it doesn't reflect the current reality of "# type: ignore" comments.
Great point.
Feature or enhancement
Add a flag to
ast.parse
that signals it to parse comments for directives given to various static analysis tools like PyRight, MyPy, Pylint, etc.Pitch
Currently,
ast.parse
produces directives fortype: ignore
only, which all type checkers respond to. PyRight reacts to its own specialpyright: ignore
flag to disambiguate type errors that are specific to it. MyPy cannot easily implement a similar flag since it depends onast.parse
. Yet amypy: ignore
directive is needed.A concrete interface is proposed here by @jab.
Previous discussion
Discussion here. Guido suggests a passing a list of namespaces the caller is interested in. This would make the function output easier to parse, and possibly more efficient.