You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
aseembits93
changed the title
Adding type hints to the dummy code for verify-setup
Adding type hints to the dummy code for verify-setup (CF-647)
May 20, 2025
The use of string literals in cast calls (e.g., cast("str", ...)) passes a string instead of a type object, which will raise a TypeError at runtime. cast expects actual type objects, not string names.
tests_root=Path(curdir) /Path(cast("str", tests_root_answer))
tests_root=tests_root.relative_to(curdir)
ph("cli-tests-root-provided")
# Autodiscover test frameworkautodetected_test_framework=detect_test_framework(curdir, tests_root)
autodetected_suffix= (
f" (seems to me you're using {autodetected_test_framework})"ifautodetected_test_frameworkelse""
)
test_framework=inquirer_wrapper(
inquirer.list_input,
message="Which test framework do you use?"+autodetected_suffix,
choices=["pytest", "unittest"],
default=autodetected_test_frameworkor"pytest",
carousel=True,
)
ph("cli-test-framework-provided", {"test_framework": test_framework})
# Get benchmarks root directorydefault_benchmarks_subdir="benchmarks"create_benchmarks_option=f"okay, create a {default_benchmarks_subdir}{os.path.sep} directory for me!"no_benchmarks_option="I don't need benchmarks"# Check if benchmarks directory exists inside tests directorytests_subdirs= []
iftests_root.exists():
tests_subdirs= [d.namefordintests_root.iterdir() ifd.is_dir() andnotd.name.startswith(".")]
benchmarks_options= []
benchmarks_options.append(no_benchmarks_option)
ifdefault_benchmarks_subdirintests_subdirs:
benchmarks_options.append(default_benchmarks_subdir)
benchmarks_options.extend([dfordintests_subdirsifd!=default_benchmarks_subdiranddnotinignore_subdirs])
benchmarks_options.append(create_benchmarks_option)
benchmarks_options.append(custom_dir_option)
benchmarks_answer=inquirer_wrapper(
inquirer.list_input,
message="Where are your performance benchmarks located? (benchmarks must be a sub directory of your tests root directory)",
choices=benchmarks_options,
default=(
default_benchmarks_subdirifdefault_benchmarks_subdirinbenchmarks_optionselsebenchmarks_options[0]
),
)
ifbenchmarks_answer==create_benchmarks_option:
benchmarks_root=tests_root/default_benchmarks_subdirbenchmarks_root.mkdir(exist_ok=True)
click.echo(f"✅ Created directory {benchmarks_root}{os.path.sep}{LF}")
elifbenchmarks_answer==custom_dir_option:
custom_benchmarks_answer=inquirer_wrapper_path(
"path",
message=f"Enter the path to your benchmarks directory inside {tests_root}{os.path.sep} ",
path_type=inquirer.Path.DIRECTORY,
)
ifcustom_benchmarks_answer:
benchmarks_root=tests_root/Path(custom_benchmarks_answer["path"])
else:
apologize_and_exit()
elifbenchmarks_answer==no_benchmarks_option:
benchmarks_root=Noneelse:
benchmarks_root=tests_root/Path(cast("str", benchmarks_answer))
# TODO: Implement other benchmark framework options# if benchmarks_root:# benchmarks_root = benchmarks_root.relative_to(curdir)## # Ask about benchmark framework# benchmark_framework_options = ["pytest-benchmark", "asv (Airspeed Velocity)", "custom/other"]# benchmark_framework = inquirer_wrapper(# inquirer.list_input,# message="Which benchmark framework do you use?",# choices=benchmark_framework_options,# default=benchmark_framework_options[0],# carousel=True,# )formatter=inquirer_wrapper(
inquirer.list_input,
message="Which code formatter do you use?",
choices=["black", "ruff", "other", "don't use a formatter"],
default="black",
carousel=True,
)
git_remote=""try:
repo=Repo(str(module_root), search_parent_directories=True)
git_remotes=get_git_remotes(repo)
ifgit_remotes: # Only proceed if there are remotesiflen(git_remotes) >1:
git_remote=inquirer_wrapper(
inquirer.list_input,
message="What git remote do you want Codeflash to use for new Pull Requests? ",
choices=git_remotes,
default="origin",
carousel=True,
)
else:
git_remote=git_remotes[0]
else:
click.echo(
"No git remotes found. You can still use Codeflash locally, but you'll need to set up a remote ""repository to use GitHub features."
)
exceptInvalidGitRepositoryError:
git_remote=""ignore_paths: list[str] = []
returnSetupInfo(
module_root=str(module_root),
tests_root=str(tests_root),
benchmarks_root=str(benchmarks_root) ifbenchmarks_rootelseNone,
test_framework=cast("str", test_framework),
ignore_paths=ignore_paths,
formatter=cast("str", formatter),
git_remote=str(git_remote),
The union type syntax list[int] | list[float] in the generated bubble_sort_content requires Python 3.10+. Verify that generated code targets a compatible Python version or adjust annotations for older versions.
The first argument to cast should be a type, not a string literal. Replace the quoted "str" with the actual str type to restore correct runtime behavior.
Why: The cast call uses a string literal instead of the str type, which breaks static type checking; changing it to cast(str, ...) restores correct type hints.
Low
Correct cast usage for benchmarks
cast expects a type object, not a string. Change "str" to str to correctly hint the type for benchmarks_answer.
Why: Both test_framework and formatter casts use "str" instead of str, which is incorrect for type hinting; updating them ensures proper static type checking.
Low
Fix cast on project_name
Change the quoted "str" to the str type in cast to ensure the return value is correctly interpreted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Adding type hints tends to help with guiding the test generation process, only homogeneous test inputs are generated.
PR Type
Enhancement
Description
Replace cast(str, ...) with cast("str", ...) calls.
Add list[int]|list[float] hints to bubble_sort function.
Update project_name cast return annotation.
Standardize string-literal casting usage.
Changes walkthrough 📝
cmd_init.py
Standardize cast args and add type hintscodeflash/cli_cmds/cmd_init.py
"str"in multiple statementsbubble_sort_contentsignatureproject_namereturn annotation