Skip to content

Add SyntaxError hint for incorrectly using C "//" or "/*...*/" to comment #134002

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

Closed
Locked-chess-official opened this issue May 14, 2025 · 11 comments
Labels
type-feature A feature request or enhancement

Comments

@Locked-chess-official
Copy link

Locked-chess-official commented May 14, 2025

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

@Locked-chess-official Locked-chess-official added the type-feature A feature request or enhancement label May 14, 2025
@skirpichev
Copy link
Contributor

What to do with comments like (* bla-bla *) (e.g. Mathematica)? There are a lot of languages, not just C++.

1: must at the beginning of the line.

Why?

I wan't this:

>>> 1 // 1j
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    1 // 1j
    ~~^^~~~
TypeError: unsupported operand type(s) for //: 'int' and 'complex'

2: the last line of the code cannot be end with ""

I don't get it. Could you please explain what exactly this means?

@skirpichev skirpichev added the pending The issue will be closed if no feedback is provided label May 14, 2025
@Locked-chess-official
Copy link
Author

What to do with comments like (* bla-bla *) (e.g. Mathematica)? There are a lot of languages, not just C++.

1: must at the beginning of the line.

Why?

I wan't this:

>>> 1 // 1j
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    1 // 1j
    ~~^^~~~
TypeError: unsupported operand type(s) for //: 'int' and 'complex'

2: the last line of the code cannot be end with ""

I don't get it. Could you please explain what exactly this means?

Ok. The message is "Did you mean '#'" if is "//" or "Did you mean ' ''' ' or ' """ '" if "/*", like others.

@Locked-chess-official
Copy link
Author

Why should be at the first of the line? Because if at the centre, it is complex.

@skirpichev
Copy link
Contributor

The message is "Did you mean '#'" if is "//"

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):

Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    1 // 1j
    ~~^^~~~
TypeError: unsupported operand type(s) for //: 'int' and 'complex'

@Locked-chess-official
Copy link
Author

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
Copy link
Author

The message is "Did you mean '#'" if is "//"

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):

Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    1 // 1j
    ~~^^~~~
TypeError: unsupported operand type(s) for //: 'int' and 'complex'

The "//" is not at the beginning of the code, so it doesn't satify with the condition. I have explained.

@skirpichev
Copy link
Contributor

skirpichev commented May 14, 2025

I have explained.

Sorry, you don't. One of you examples in the description looks:

#TypeError(don't need)
def one_or_two():
    a = random.randrange(1,2)
    print(a) // 1 or 2

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
Copy link
Author

I have explained.

Sorry, you don't. One of you examples in the description looks:

#TypeError(don't need)
def one_or_two():
    a = random.randrange(1,2)
    print(a) // 1 or 2

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.

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
Copy link

hjhicks commented 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.

@terryjreedy terryjreedy changed the title Add SyntaxError message for incorrectly using "//" or "/**/" to comment Add SyntaxError hint for incorrectly using C "//" or "/*...*/" to comment May 15, 2025
@terryjreedy
Copy link
Member

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.

  1. CPython's tokenizer, exposed in the tokenizer module, is an iterator. I presume that the parser reads one token at a time and then processes it. Both // (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.
  2. OP suggested that /* should be matched with */ (also 2 tokens) later if the file. I believe 'later' will not have been parsed.
  3. Consider 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.
  4. Today, many Python users may either not know or use a /? language. For such users, the hint would be a mistake.
  5. (skirpichev) There are other language with other comment syntaxes. And there are other syntax errors that are legal in some other languages. Interpreting Python code errors as legal code and suggesting a Python equivalent is a road I think the core team should not start down. It would be better, if at all, as an independent project on pypi.org.

@skirpichev skirpichev closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2025
@skirpichev skirpichev removed the pending The issue will be closed if no feedback is provided label May 15, 2025
@skirpichev
Copy link
Contributor

I presume that the parser reads one token at a time and then processes it. Both // (usually) and '/*' (always) are tokenized as two tokens.

This is invalid for //:

$ echo "   // is this a comment?" | ./python -m tokenize
1,0-1,3:            INDENT         '   '          
1,3-1,5:            OP             '//'           
1,6-1,8:            NAME           'is'           
1,9-1,13:           NAME           'this'         
1,14-1,15:          NAME           'a'            
1,16-1,23:          NAME           'comment'      
1,23-1,24:          OP             '?'            
1,24-1,25:          NEWLINE        '\n'           
2,0-2,0:            DEDENT         ''             
2,0-2,0:            ENDMARKER      ''           

Though, point 3) holds in this case.

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

No branches or pull requests

4 participants