Skip to content

add utility functions - #29

Open
CristianAmbrosini wants to merge 4 commits into
mainfrom
test-custom-instructions
Open

add utility functions#29
CristianAmbrosini wants to merge 4 commits into
mainfrom
test-custom-instructions

Conversation

@CristianAmbrosini

Copy link
Copy Markdown
Owner

Adds utility functions for discount calculation, email validation, name formatting, and value clamping.

@sonar-review-dev18

sonar-review-dev18 Bot commented Apr 23, 2026

Copy link
Copy Markdown

Summary

Adds a new utils.py module with 8 general-purpose utility functions: discount calculation, email validation, name formatting, value clamping, string truncation, safe integer parsing, text slugification, and word pluralization.

Note: The PR description mentions only 4 functions but the implementation includes 8. All functions are simple, synchronous utilities with no dependencies.

What reviewers should know

Start here: The entire changeset is in a single new file utils.py — straightforward to review.

Points for reviewers to consider:

  • Email validation (is_valid_email): Uses a basic heuristic (@-sign and dot in domain). If stricter RFC-compliant validation is needed, this won't handle edge cases like quoted strings or IP addresses.

  • Pluralization (pluralize): Hard-coded "add s" rule. Won't handle irregular plurals (e.g., "child" → "children").

  • Slugification (slugify): Minimal implementation. Doesn't handle special characters, accents, or consecutive spaces.

  • Missing tests: No test coverage is included in this PR. Consider whether test fixtures should be added.

  • No docstrings: Functions lack documentation. Adding docstrings would clarify intent and usage, especially for clamp and truncate_string edge cases.

These utilities appear ready for straightforward use cases. Assess whether the simplistic implementations match your project's requirements or if more robust alternatives are needed.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

@sonar-review-dev18 sonar-review-dev18 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The PR introduces four utility functions, two of which have correctness issues that should be fixed before merge.

🗣️ Give feedback

Comment thread utils.py


def is_valid_email(email):
return "@" in email and "." in email.split("@")[-1]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: is_valid_email accepts addresses with multiple @ signs as valid. "a@b@c.com".split("@")[-1] yields "c.com", which passes the . check — so "attacker@target@domain.com" returns True.

A minimal fix is to also assert there is exactly one @:

Suggested change
return "@" in email and "." in email.split("@")[-1]
return email.count("@") == 1 and "." in email.split("@")[-1]
  • Mark as noise

Comment thread utils.py
@@ -0,0 +1,18 @@
def calculate_discount(price, percentage):
return price - (price * percentage / 100)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing test coverage: No test covers the case where percentage > 100, which causes the function to return a negative price. If callers can pass arbitrary percentages, this silently produces an invalid result (e.g. calculate_discount(50, 150)-25). A unit test asserting the return value is non-negative, or a guard clamping percentage to [0, 100], would prevent this from reaching production.

  • Mark as noise

Comment thread utils.py


def is_valid_email(email):
return "@" in email and "." in email.split("@")[-1]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing test coverage: The email validation logic is non-trivial and entirely uncovered. At minimum, tests should assert:

  • Valid address returns True

  • Missing @ returns False

  • Multiple @ signs return False

  • Domain with no . (e.g. "user@localhost") returns False

  • Trailing dot in domain (e.g. "user@domain.") returns False

  • Mark as noise

@sonar-review-dev18 sonar-review-dev18 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's a bug in the newly added truncate_string that needs fixing. The previously flagged issues (is_valid_email accepting multiple @ signs, calculate_discount producing negative prices, and missing email validation tests) are still open.

SonarQube Cloud status: Coverage on new code is 0% against an 80% threshold — see the reviewer guide above for details.

🗣️ Give feedback

Comment thread utils.py


def truncate_string(text, max_length):
if len(text) <= max_length:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: When truncation occurs, the returned string is max_length + 3 characters long (the "..." suffix is appended after slicing to max_length), so the result silently exceeds the stated limit.

For example: truncate_string("hello world", 5)"hello..." (8 chars, not 5).

Callers that rely on the result fitting within max_length (e.g. for a database column or UI label) will get data that's longer than expected.

Suggested change
if len(text) <= max_length:
return text[:max(0, max_length - 3)] + "..."
  • Mark as noise

@sonar-review-dev18 sonar-review-dev18 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The four issues flagged in earlier review rounds (multiple-@ email bug, negative-price from calculate_discount, off-by-3 in truncate_string, and the email validation coverage gap) are all still present in the current code — none have been addressed.

SonarQube Cloud quality gate is failing — coverage on new code is 0% against an 80% threshold. See the reviewer guide above for details.

🗣️ Give feedback

Comment thread utils.py


def slugify(text):
return text.lower().strip().replace(" ", "-")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: slugify only replaces spaces — it leaves punctuation, symbols, and non-ASCII characters in the output. For example:

  • slugify("Hello, World!")"hello,-world!" (comma and ! retained)
  • slugify("café au lait")"café-au-lait" (accented character retained)
  • slugify("hello world")"hello--world" (double space → double hyphen)

Any caller using the result in a URL will silently get an invalid slug. At minimum, non-word characters should be stripped and runs of hyphens collapsed:

import re

def slugify(text):
    text = text.lower().strip()
    text = re.sub(r'[^\w\s-]', '', text)
    text = re.sub(r'[\s_]+', '-', text)
    return re.sub(r'-+', '-', text).strip('-')
  • Mark as noise

@sonarqube-cloud-dev18

Copy link
Copy Markdown

SonarQube reviewer guide

Review in SonarQube

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@sonar-review-dev18 sonar-review-dev18 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This commit only adds pluralize — the five issues flagged in prior review rounds (invalid email with multiple @ signs, negative price from calculate_discount, email validation coverage, truncate_string exceeding max_length, and slugify leaving punctuation/non-ASCII characters) are all still open and unaddressed.

SonarQube Cloud quality gate is failing — coverage on new code is 0%, against an 80% threshold. See the reviewer guide above for details.

🗣️ Give feedback

Comment thread utils.py


def pluralize(word, count):
return word if count == 1 else word + "s"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing test coverage: pluralize has no tests. Two cases that could regress silently:

  • count == 0 (e.g. pluralize("item", 0)) should return the plural form — confirm the intent matches the call sites.

  • Non-English words that don't pluralise with a plain +"s" (e.g. "bus""buss" instead of "buses") — if the project only uses simple English nouns this is fine, but it's worth a test to document the expected scope.

  • Mark as noise

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.

1 participant