Skip to content

python.lsh: Avoid capturing underscores as numeric literals - #981

Merged
Leonard Hecker (lhecker) merged 3 commits into
microsoft:mainfrom
jagourq:main
Oct 12, 2026
Merged

Leonard Hecker (lhecker) merged 3 commits into
microsoft:mainfrom
jagourq:main

Conversation

@jagourq

Copy link
Copy Markdown
Contributor

The definition of numeric literals in python.lsh currently captures underscores, so identifiers like __init__ and ._foo are considered to be numeric literals immediately followed by identifiers and are discarded as invalid.

I've changed the regex for numeric literals by replacing [\d_]+ with \d[\d_]*.

lhecker

This comment was marked as outdated.

@lhecker

This comment was marked as resolved.

@lhecker Leonard Hecker (lhecker) 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.

Actually I realize now that your explanation doesn't make sense. How would __init__ match this clause? It doesn't even contain any digits!

Can you explain what this PR solves?

@jagourq

James (jagourq) commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

This PR fixes the highlighting of methods beginning with _ and variables consisting solely of _ in Python source files.
The regex for digits is designed to allow digits to contain _, but the pattern used also captures _, __ and so on.

The __ in __main__ is currently captured by [\d_]+ in the regex for digits on line 55, main__ is then captured by \w+ on line 56, and everything is discarded as an "Invalid numeric literal" (see the comment on line 57), preventing __main__ from being parsed as a method afterwards.

Currently, the code

_ = 3.14
def __main__(): pass
def main(): pass
f._foo()
f.foo()

renders as
image
_ is incorrectly highlighted as a digit, and __main__() and ._foo() are not highlighted as methods.
After these commits, it renders as
image
with _ highlighted similarly to other variables and __main__() and ._foo() highlighted as methods, fixing the previous issues.

@lhecker Leonard Hecker (lhecker) 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.

Well, time to fix all the others too then!

@lhecker

Copy link
Copy Markdown
Member

James (@jagourq) We can merge this after you agree to the contributor's agreement above.

@jagourq

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@lhecker
Leonard Hecker (lhecker) merged commit cb6de0a into microsoft:main Oct 12, 2026
6 checks passed
@lhecker

Copy link
Copy Markdown
Member

Thanks! :)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants