-
Notifications
You must be signed in to change notification settings - Fork 179
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
End column/lineno #454
Comments
This would only be useful / easy in python3.8+ where |
I hadn't even considered whether the ast itself had this built in. I was just thinking of computing it manually. For nodes that represent names or keywords, there is a natural length.I guess for other nodes it could be hard. Just glossing over messages.py, I'd say over half the messages relate to either a name or a keyword, so for those at least it would be quite easy to get this. I forgot to mention that my use case here is that I'm writing a tool that shows errors in an interactive program, and I want to highlight the exact chunk of code that has a warning associated with it. I don't think the command line API should use this, unless there is an already accepted way to do so. Another question is whether (end_col, end_lineno) should point to the last character of a word or the character after the last character of a word (for "fake" 0-indexing). What does the Python 3.8 AST do? |
here's some output from py38, I haven't had time to look at the indexes and what they mean
|
(I happen to know |
It looks like the end column is after the last character, so that end_col - col gives the length of the item (if it's on the same line). For instance: I would suggest just using this. For < 3.8 or things where offsets don't make sense, we can set the end column/lineno to be the same as the start. I'm curious where this work in CPython is going? Will there someday be a standard roundtrippable AST? It would also be nice if this sort of thing made its way to the actual parser, so that syntax errors could point to ranges instead of just positions in the source. |
I've been ~loosely following it, it seems there's two directions right now:
the latter I don't think has a concrete (:laughing:) plan yet |
would be nice if python-language-server would show pyflakes unused import errors so that it ends in the right place for example, if I currently do |
Would it be a good idea to expose the original Lines 6 to 13 in 4cf2189
Alternatively, would you accept a PR that adds self.end_col_offset = getattr(loc, 'end_col_offset', None)
self.end_lineno = getattr(loc, 'end_lineno', None) I would really like to improve reporting from pyflakes in editors using revived python-lsp-server such as Spyder and JupyterLab-LSP. |
My understanding from the discussions here is that that the pyflakes team is in support of this change. I think adding |
Thank you for the quick reply.
I don't have a strong feeling, but a slight preference to |
I would disagree with @asmeurer. I see no statement of support. I see statements of fact (this is possible in 3.8+ but not before it). It would be more possible if we replaced I think there's going to be a problem here where projects supporting multiple versions with multiple developers across those versions will see different information about the project. I don't think changing the metadata about reports across versions of Python is going to be a good user experience and I'm not confident we should add this until we can drop Python versions prior to 3.8 |
:(
The proposed change does not influence default reporters at all, it only enables custom reporters to make use of it. As a researcher and as an instructor I would like to get better highlighting of errors and warnings. We are currently looking at PEP 657 in Python 3.11 which makes wonders for quick understanding of tracebacks. I would have thought this PEP is a statement of direction towards making user life easier (and it is directly related to this requests as it pertains narrowing down where a flagged issue arises in the code).
What is you policy on dropping support? I was a bit confused by seeing that you still support versions 2.7, 3.4 and 3.5 although those no longer receive security fixes. I am genuinely asking because if the pre-condition of dropping versions prior to Python 3.8 will only become true in 4 years or 5 years, I will just monkey-patch it downstream. |
Why can't you draw a zero width line? Most code that you would write to draw a line would just do the right thing (draw nothing) if you give it an equal start and stop. On the other hand, it would raise a TypeError if you pass it None.
This comment very much reads like a statement of support to me ("useful") #454 (comment).
As @krassowski has pointed out, this doesn't change any metadata, only adds new attributes. Existing code that doesn't know about these attributes will do nothing (just to be clear, the suggestion here is only to add new attributes to the Python classes, not to add end column information to the command line output, which I agree would be a backwards incompatible change). As for different versions of Python doing different things, this is already how pyflakes works. If you run pyflakes in Python 3.7 on code that uses assignment expressions, it will fail (and as you well know, this used to be a huge problem back when Python 2 was more commonly used). The general consensus has always been that that's just the way things are. The only way to avoid that would be to manually backport all new Python syntax features, which would entail basically using a custom parser (I've suggested using parso do to this in the past, which does in fact support parsing multiple versions of Python from a single version). Incidentally, for messages that involve a variable name (like unused variable warnings), it's pretty easy to manually create the end column. Just add |
It would be useful to have
end_col
andend_lineno
attributes on Message objects. The way it would work is that (col, lineno) to (end_col, end_lineno) would represent the range of characters for the error. For instance, for undefined names, the col, lineno already points to the first character, and the end versions would point to the last character in the word.I haven't looked yet how easy this is to implement. I wanted to post it here to see if anyone had any thoughts. I might make a pull request for it later (time permitting).
The text was updated successfully, but these errors were encountered: