Skip to content

bug: generate a valid class name when the project name starts with a digit - #674

Open
Amadeus-22 wants to merge 2 commits into
QuantConnect:masterfrom
Amadeus-22:bug-673-class-name-leading-digit
Open

Amadeus-22 wants to merge 2 commits into
QuantConnect:masterfrom
Amadeus-22:bug-673-class-name-leading-digit

Conversation

@Amadeus-22

Copy link
Copy Markdown

Closes #673.

  • lean project-create --language python "2024 Momentum" wrote class 2024Momentum(QCAlgorithm):, which is a syntax error; the same happens for C#. The command still exited with code 0.
  • convert_to_class_name now prefixes the name with an underscore when it is empty or starts with a digit (_2024Momentum). Other names are unchanged. The same helper is used by lean research.
  • Added a parametrized test that creates the project and compiles the generated main.py. It fails on master and passes with the fix.
  • pytest tests/commands/test_create_project.py tests/commands/test_research.py passes (30 tests, Python 3.12) and python static_analysis.py is clean.

🤖 Generated with Claude Code

Amadeus-22 and others added 2 commits October 6, 2026 15:01
…digit

convert_to_class_name only removed non-alphanumeric characters, so a
project called '2024 Momentum' got 'class 2024Momentum', which does not
compile. Prefix the name with an underscore when it is empty or starts
with a digit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Project names such as `none`, `true` or `_class` produced `class None`,
`class True` or `class class`, which are syntax errors in Python and C#.
Prefix the class name with an underscore when it starts with a lowercase
letter or is a Python keyword.
@jhonabreul

Copy link
Copy Markdown
Collaborator

Thanks for the fix. I pushed a follow-up commit (e57b0ef) on top of it.

Names that still failed with the previous commit

The leading-digit case was fixed, but convert_to_class_name could still return a hard keyword. The command exited with code 0 and the generated main.py was a syntax error:

Project name Class name Why
none, true, false None, True, False capitalizing the word produces the keyword
_class, __import__, _lambda class, import, lambda the leading _ is capitalized as a no-op and then stripped, leaving the bare keyword

The second shape also produces C# keywords for C# projects (public class class : QCAlgorithm, which fails with CS1001). A fuzz over the characters the project-name validator allows, plus every entry of keyword.kwlist with each stripped prefix, found 187 breaking names.

What changed

  • convert_to_class_name also prefixes _ when the stripped name starts with a lowercase letter or is a Python keyword. Keywords are lowercase in both languages except for None, True and False, so the lowercase check covers C# keywords without a separate list. _myproject now yields _myproject instead of Myproject; other names are unchanged.
  • The parametrized test is now test_create_project_creates_valid_class_name_when_name_is_not_an_identifier and adds none, true, _class and __import__ to the existing digit cases; each one creates the project and compiles the generated main.py. The new cases fail on the previous commit. tests/commands/test_create_project.py and tests/commands/test_research.py pass (34 tests) and static_analysis.py is clean. The fuzz now reports zero breaking names.

A hyphen-prefixed name such as -def takes the same path as _def, but click rejects it as an unknown option before the command runs, so it is not part of the test.

@Amadeus-22

Copy link
Copy Markdown
Author

Thanks, @jhonabreul. I read e57b0ef. I had only covered the leading digit and missed that capitalizing or stripping the prefix could still land on a keyword (none, _class). Using the lowercase check to cover the C# keywords without keeping a separate list is a neat way to handle both languages, and compiling the generated main.py in the test is a stronger check than the one I wrote. Nothing to add from my side.

@jhonabreul jhonabreul left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for your contribution!

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.

project-create generates an invalid class name when the project name starts with a digit

2 participants