Skip to content

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

Not planned
@Locked-chess-official

Description

@Locked-chess-official

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

skirpichev commented on May 14, 2025

@skirpichev
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?

added
pendingThe issue will be closed if no feedback is provided
on May 14, 2025
Locked-chess-official

Locked-chess-official commented on May 14, 2025

@Locked-chess-official
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

Locked-chess-official commented on May 14, 2025

@Locked-chess-official
Author

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

skirpichev

skirpichev commented on May 14, 2025

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

Locked-chess-official commented on May 14, 2025

@Locked-chess-official
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

Locked-chess-official commented on May 14, 2025

@Locked-chess-official
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

skirpichev commented on May 14, 2025

@skirpichev
Contributor

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

Locked-chess-official commented on May 14, 2025

@Locked-chess-official
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

hjhicks commented on May 14, 2025

@hjhicks

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.

changed the title [-]Add SyntaxError message for incorrectly using "/" or "/**/" to comment[/-] [+]Add SyntaxError hint for incorrectly using C "/" or "/*...*/" to comment[/+] on May 15, 2025
terryjreedy

terryjreedy commented on May 15, 2025

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

2 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

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

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant