Skip to content
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

Add comments parsing flag to ast.parse #101494

Open
NeilGirdhar opened this issue Feb 1, 2023 · 4 comments
Open

Add comments parsing flag to ast.parse #101494

NeilGirdhar opened this issue Feb 1, 2023 · 4 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@NeilGirdhar
Copy link

NeilGirdhar commented Feb 1, 2023

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 for type: ignore only, which all type checkers respond to. PyRight reacts to its own special pyright: ignore flag to disambiguate type errors that are specific to it. MyPy cannot easily implement a similar flag since it depends on ast.parse. Yet a mypy: 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.

@NeilGirdhar
Copy link
Author

It would be nice to also support associating comment directives from preceding otherwise empty lines, e.g., like this.

@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 27, 2023
@srittau
Copy link
Contributor

srittau commented Jul 18, 2024

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.

@NeilGirdhar
Copy link
Author

NeilGirdhar commented Jul 18, 2024

@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.

@wyattscarpenter
Copy link

I think it would be nice to have this feature.

I also think it would probably be easier and more extensible if ast.parse exposed all comments in its results. Then all libraries hoping to read their directives in comments could do so on their end. This would allow flexibility; for instance mypy could choose to do its directives in either way. This would also match the behavior of https://docs.python.org/3/library/tokenize.html, which is intended to match the behavior of ast.parser except it also exposes COMMENT tokens.

All three of these mechanisms (including the current type: ignore mechanism, which already exists) could co-exist. Of them, I would say exposing all comments is the most important. Maybe there's some reason not to do that which I don't know of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants