Skip to content

gh-151128: Improve SyntaxError message for cross language keywords#151129

Open
zang-langyan wants to merge 7 commits into
python:mainfrom
zang-langyan:151128
Open

gh-151128: Improve SyntaxError message for cross language keywords#151129
zang-langyan wants to merge 7 commits into
python:mainfrom
zang-langyan:151128

Conversation

@zang-langyan

Copy link
Copy Markdown
Contributor
image

@serhiy-storchaka

Copy link
Copy Markdown
Member

I do not think that it is needed to add 'switch' to keywords.

Even if we only add one special case, it is worth to write more general code which will allow to add more keywords in future. See #146407 as example.

ByteFlowing1337

This comment was marked as outdated.

@zang-langyan

Copy link
Copy Markdown
Contributor Author

I do not think that it is needed to add 'switch' to keywords.

Even if we only add one special case, it is worth to write more general code which will allow to add more keywords in future. See #146407 as example.

Thank you for the reference, it is really a good example. I will look into it.

@zang-langyan zang-langyan changed the title gh-151128: Improve SyntaxError message for match gh-151128: Improve SyntaxError message for cross language keywords Jun 9, 2026
Comment thread Lib/traceback.py Outdated
Comment on lines +1890 to +1891
hint = _CROSS_LANGUAGE_KEYWORD_HINTS.get(wrong_name)
return hint

This comment was marked as resolved.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can split this on two features -- for keywords and for operators.

Anyway, it needs a NEWS entry, a What's New entry, and tests.

Comment thread Grammar/python.gram Outdated
| a=expression ':=' expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
| a=expression '&''&' b=expression {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. This is actually a different feature. You can even open a separate issue and PR for this (if there is no already open). Anyway, this is a nice improvement.

I suggest to add also cases for <> ("not equal" in Pascal and Python 2), "=<", "=>" and "=!" (typo in "<=", ">=" and "!="), "===" ("is" in JavaScript and PHP).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there is already a PR for improving SyntaxError message of && and ||: #150906

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for review, great suggestion, I will add those~

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then let's focus on keywords in this PR.

Comment thread Lib/traceback.py Outdated
suggestion = _suggestions._generate_suggestions(keyword.kwlist + keyword.softkwlist, wrong_name)
if suggestion:
matches.append(suggestion)
matches.extend(difflib.get_close_matches(wrong_name, keyword.kwlist, n=max_matches, cutoff=0.5))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
matches.extend(difflib.get_close_matches(wrong_name, keyword.kwlist, n=max_matches, cutoff=0.5))
matches.extend(difflib.get_close_matches(wrong_name, keyword.kwlist+keyword.softkwlist, n=max_matches, cutoff=0.5))

Maybe we should add softkwlist here too?

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add also tests for soft keywords? And document this change in the NEWS entry.

Why aliases for None were removed? Were there issues with them?

@zang-langyan

zang-langyan commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

Could you please add also tests for soft keywords? And document this change in the NEWS entry.

Why aliases for None were removed? Were there issues with them?

Sure, I will add the tests for soft keywords.

I removed the aliases for None because I realized that names like Null, nil, etc. don't work well for keyword syntax detection. I can't think of a situation where these tokens would be correctly identified as keywords. Any suggestions?

@serhiy-storchaka

Copy link
Copy Markdown
Member

You are right. Maybe they can be handled where NameError is handed.

Comment on lines +1821 to +1822
("typ x = int", "type"),
("typed x = int", "type"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two tests for match and type, no tests for lazy.

Comment thread Lib/traceback.py
'delete': 'del',
# function define equivalents
'function': 'def',
'func': 'def',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'func': 'def',
'func': 'def',
'void': 'def'

Long shot. But maybe?

Comment thread Lib/traceback.py
max_matches = 3
matches = []

hint = _get_cross_language_keyword_hint(wrong_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hint = _get_cross_language_keyword_hint(wrong_name)
hint = _CROSS_LANGUAGE_KEYWORD_HINTS.get(wrong_name)

If it is only used once, and is marked as a "private" method, I would suggest calling the body directly

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants