Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion th_cli/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def validate_test_ids(test_ids: str) -> list[str]:
raise CLIError("No valid test IDs provided")

# Validate each test ID format
valid_pattern = re.compile(r"^TC[-_][A-Z_]{2,20}([-_\.]\d+){2,3}(-custom)?$")
valid_pattern = re.compile(r"^TC[-_][A-Z_]{1,20}([-_\.]\d+){2,3}(-custom)?$")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For improved performance, it's recommended to compile this regex at the module level, outside of this function. Compiling a regex can be a relatively expensive operation, and doing it inside a function that might be called in a loop can lead to unnecessary overhead. By defining the compiled pattern as a module-level constant, it's compiled only once when the module is imported.

For example:

# At module level, outside any function
_VALID_TEST_ID_PATTERN = re.compile(r"^TC[-_][A-Z_]{1,20}([-_.]\d+){2,3}(-custom)?$")

def validate_test_ids(test_ids: str) -> list[str]:
    # ...
    # inside the function, just use it
    for test_id in ids:
        if not _VALID_TEST_ID_PATTERN.match(test_id):
            # ...

As a minor point, the backslash before the dot in [-_\.] is not necessary as . is not a special character inside a character set []. You can simplify it to [-_.].

References
  1. For performance, regular expressions should be compiled once at the module level rather than inside a function that may be called multiple times.


invalid_ids = []
for test_id in ids:
Expand Down