Not planned
Description
Feature or enhancement
Proposal:
Sometimes another programmer may make the mistake that using "/" or "/**/" to comment, especially cross-language projects.
In my option, showing the message need to satisfy the following comditions:
If the Exception raised by "/":
1: must at the beginning of the line.
2: the last line of the code cannot be end with ""
#SyntaxError(need)
def one_or_two():
/ 1 or 2
a = random.randrange(1,3)
return a
#SyntaxError(don't need)
def one_or_two(): / 1 or 2
a = random.randrange(1,3)
return a
#TypeError(don't need)
def one_or_two():
a = random.randrange(1,2)
print(a) / 1 or 2
#Valid
def one_or_two():
a = random.randrange(1,3)
return a / 1 or 2
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Activity
skirpichev commentedon May 14, 2025
What to do with comments like
(* bla-bla *)
(e.g. Mathematica)? There are a lot of languages, not just C++.Why?
I wan't this:
I don't get it. Could you please explain what exactly this means?
Locked-chess-official commentedon May 14, 2025
Ok. The message is "Did you mean '#'" if is "/" or "Did you mean ' ''' ' or ' """ '" if "/*", like others.
Locked-chess-official commentedon May 14, 2025
Why should be at the first of the line? Because if at the centre, it is complex.
skirpichev commentedon May 14, 2025
I don't find it helpful.
Last chance. I entered "1 / 1j" in CPython repl. How will look traceback with your proposal?
Currently we have (repeat):
Locked-chess-official commentedon May 14, 2025
What does the conditions means is that if the SyntaxError raised by "/", it needs to check, if the "/" is not the beginning of the line of the code, the message shouldn't be show.
Locked-chess-official commentedon May 14, 2025
The "/" is not at the beginning of the code, so it doesn't satify with the condition. I have explained.
skirpichev commentedon May 14, 2025
Sorry, you don't. One of you examples in the description looks:
Do you mean we need to replace TypeError here?
Could you please be more precise and provide exact suggestions to all your examples? How tracebacks will look?
If your proposal is raising a SyntaxError with a different message iff the
/
was at the beginning of the string - maybe this does make sense.Locked-chess-official commentedon May 14, 2025
It just show the complex of the conditions that "/" at the centre of the line. So what I mean is that to escape from those conditions, only when "/" at the beginning of the line.
hjhicks commentedon May 14, 2025
I understand the intent, but I don't find this to be overly useful. The code you included in your examples already raises a SyntaxError, I think a competent developer should be able to recognize that they messed up the comment syntax based solely on the current SyntaxError. This is just my opinion, though.
[-]Add SyntaxError message for incorrectly using "/" or "/**/" to comment[/-][+]Add SyntaxError hint for incorrectly using C "/" or "/*...*/" to comment[/+]terryjreedy commentedon May 15, 2025
There is a communication problem here; let's be gentile with that. The proposal (formulated to be possible) is that if a line beginning with
/
or/* raises a syntax error, the hinter should assume that the user is a C programmer who meant to start a comment and add a hint proposing
*or
"""as an alternative. OP proposes to ignore
/?` errors elsewhere because it would be too hard to determine if it was meant to be a comment starter.Problems with the proposal:
0. OP added an end-of-file condition that the rest of us seem not to understand. It does not seem necessary for the proposal and I ignored it. In any case, the hinter likely does not have access to the file itself.
/
(usually) and '/*' (always) are tokenized as two tokens. The initial/
is usually invalid by itself. If not, a following*
is invalid by itself, and it would not be at the beginning of the line./*
should be matched with*/
(also 2 tokens) later if the file. I believe 'later' will not have been parsed.long_new_name = long_old_number_name + garulous * engaging\n/ (cringeness + slopiness)
where the error is omitting\
on the previous line or not wrapping the 2-line expression in(...)
. The hint would be a mistake./?
language. For such users, the hint would be a mistake.2 remaining items