From 1f3d7da2b7668f9968bbcaeb411916367a5f5a4c Mon Sep 17 00:00:00 2001 From: Sohail Date: Sun, 22 Nov 2026 13:48:06 +0530 Subject: [PATCH] R23-T028: Strict Ruff audit, code fixes, and review documentation --- conversions/pressure_conversions.py | 14 +++-- work/R23-T028/PR_BODY.md | 33 +++++++++++ work/R23-T028/REVIEW.md | 87 +++++++++++++++++++++++++++++ work/R23-T028/__init__.py | 1 + work/R23-T028/test_output.txt | 83 +++++++++++++++++++++++++++ 5 files changed, 212 insertions(+), 6 deletions(-) create mode 100644 work/R23-T028/PR_BODY.md create mode 100644 work/R23-T028/REVIEW.md create mode 100644 work/R23-T028/__init__.py create mode 100644 work/R23-T028/test_output.txt diff --git a/conversions/pressure_conversions.py b/conversions/pressure_conversions.py index fe78b1382677..61be8387d759 100644 --- a/conversions/pressure_conversions.py +++ b/conversions/pressure_conversions.py @@ -1,5 +1,5 @@ -""" -Conversion of pressure units. +"""Conversion of pressure units. + Available Units:- Pascal,Bar,Kilopascal,Megapascal,psi(pound per square inch), inHg(in mercury column),torr,atm USAGE : @@ -23,6 +23,8 @@ class FromTo(NamedTuple): + """Conversion factors from and to standard atmosphere (atm).""" + from_factor: float to_factor: float @@ -40,8 +42,8 @@ class FromTo(NamedTuple): def pressure_conversion(value: float, from_type: str, to_type: str) -> float: - """ - Conversion between pressure units. + """Conversion between pressure units. + >>> pressure_conversion(4, "atm", "pascal") 405300 >>> pressure_conversion(1, "pascal", "psi") @@ -67,12 +69,12 @@ def pressure_conversion(value: float, from_type: str, to_type: str) -> float: if from_type not in PRESSURE_CONVERSION: raise ValueError( f"Invalid 'from_type' value: {from_type!r} Supported values are:\n" - + ", ".join(PRESSURE_CONVERSION) + + ", ".join(PRESSURE_CONVERSION), ) if to_type not in PRESSURE_CONVERSION: raise ValueError( f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n" - + ", ".join(PRESSURE_CONVERSION) + + ", ".join(PRESSURE_CONVERSION), ) return ( value diff --git a/work/R23-T028/PR_BODY.md b/work/R23-T028/PR_BODY.md new file mode 100644 index 000000000000..d51d81268c12 --- /dev/null +++ b/work/R23-T028/PR_BODY.md @@ -0,0 +1,33 @@ +## Summary of Changes + +### Task R23-T028: Strict Ruff audit, code fixes, and review documentation + +This pull request completes Task R23-T028 (8 of 10) on `conversions/pressure_conversions.py`. + +### 1. Goal and Decisions +- Evaluated `conversions/pressure_conversions.py` under Ruff's strictest rule set (`--select ALL`). +- Resolved **6 rule codes** (exceeding the requirement of at least 3): + - **`D101`** (`missing-docstring-public-class`): Added clear docstring to `class FromTo(NamedTuple)`. + - **`D205`** (`missing-blank-line-after-summary`): Added blank line between docstring summary and body. + - **`D212`** (`multi-line-summary-first-line`): Aligned multi-line docstring summaries to the opening line. + - **`D400`** (`ends-in-period`): Ensured summary sentences end with proper period punctuation. + - **`D415`** (`ends-in-punctuation`): Ensured docstring summary punctuation compliance. + - **`COM812`** (`missing-trailing-comma`): Added trailing commas to multi-line `ValueError` raise calls. +- Justified and kept non-applicable or domain-specific rules in `work/R23-T028/REVIEW.md`: + - **`CPY001`** (Missing copyright notice): Kept as TheAlgorithms-Python does not use per-file copyright headers. + - **`E501`** (Line too long / URLs): Kept reference URLs intact for documentation utility. + - **`PLR2004`** (Magic numbers): Kept numeric literals in `PRESSURE_CONVERSION` as physical constants. + - **`pep8-naming` (`inHg`)**: Kept scientific meteorological unit abbreviation intact. + +### 2. Verification & Test Evidence +- **Repo-Config Ruff Check**: `uvx ruff check conversions/pressure_conversions.py` passes (0 errors). +- **Ruff Format Check**: `uvx ruff format --check conversions/pressure_conversions.py` passes. +- **Doctests**: All 9 existing doctests pass (`9 passed. Test passed.`). +- **Pytest**: `uv run --with pytest pytest --doctest-modules conversions/pressure_conversions.py` passes (1 passed in 0.02s). + +### 3. Submission Checklist +- [x] Strict Ruff audit performed and documented in `work/R23-T028/REVIEW.md`. +- [x] At least 3 rule codes fixed in `conversions/pressure_conversions.py` (6 fixed). +- [x] Repo-config Ruff passes with 0 warnings. +- [x] All 9 doctests pass. +- [x] Clean upstream branch `student/E26B07F0850/R23-T028-upstream` prepared with only code changes. diff --git a/work/R23-T028/REVIEW.md b/work/R23-T028/REVIEW.md new file mode 100644 index 000000000000..f3b4714230da --- /dev/null +++ b/work/R23-T028/REVIEW.md @@ -0,0 +1,87 @@ +# Strict Ruff Audit & Review for `conversions/pressure_conversions.py` +**Task ID**: R23-T028 +**Repository**: [S-AI-Projects/TheAlgorithms-Python](https://github.com/S-AI-Projects/TheAlgorithms-Python) +**Target File**: `conversions/pressure_conversions.py` +**Working Branch**: `student/E26B07F0850/R23-T028` (alias `R23-T028`) + +--- + +## 1. Executive Summary + +Under default repository configuration, `conversions/pressure_conversions.py` passes all linter checks (`uvx ruff check conversions/pressure_conversions.py` reports 0 findings). + +Running Ruff with strict rules enabled (`uvx ruff check --select ALL conversions/pressure_conversions.py`) revealed several style, formatting, and documentation findings across `pydocstyle` (`D`), `flake8-commas` (`COM`), and `flake8-copyright` (`CPY`). In addition, we performed in-depth analyses on how Ruff treats line lengths with URLs (`E501`), magic numbers (`PLR2004`), and scientific unit naming conventions (`pep8-naming` / `N815`). + +We resolved **6 rule codes** (`D101`, `D205`, `D212`, `D400`, `D415`, `COM812`) in `conversions/pressure_conversions.py`, exceeding the task requirement of at least 3 fixed codes. The remaining findings were either kept or evaluated as non-applicable according to `CONTRIBUTING.md` standards and scientific domain conventions. + +--- + +## 2. Ruff `--select ALL` Rule Evaluation Matrix + +| Rule Code | Category | Rule Name | Decision | Justification & Action Taken | +| :--- | :--- | :--- | :---: | :--- | +| **`D101`** | pydocstyle | `missing-docstring-public-class` | **FIXED** | Added concise, descriptive docstring to `class FromTo(NamedTuple)` explaining its purpose as conversion factors from and to standard atmosphere (`atm`). | +| **`D205`** | pydocstyle | `missing-blank-line-after-summary` | **FIXED** | Formatted the module docstring and `pressure_conversion` docstring with exactly one blank line between the summary sentence and subsequent details/examples. | +| **`D212`** | pydocstyle | `multi-line-summary-first-line` | **FIXED** | Formatted multi-line docstrings so the summary sentence begins immediately on the first line after `"""`, aligning with PEP 257 and project guidelines. | +| **`D400`** | pydocstyle | `ends-in-period` | **FIXED** | Ensured docstring first-line summaries end with a period (`.`): `"""Conversion of pressure units.` and `"""Conversion between pressure units.`. | +| **`D415`** | pydocstyle | `ends-in-punctuation` | **FIXED** | Verified summary statements terminate cleanly with punctuation (`.`), satisfying both D400 and D415 simultaneously. | +| **`COM812`**| flake8-commas | `missing-trailing-comma` | **FIXED** | Added missing trailing commas to multi-line `ValueError` raise expressions in `pressure_conversion` for consistent formatting. | +| **`CPY001`**| flake8-copyright| `missing-copyright-notice` | **KEPT** | `TheAlgorithms-Python` is an open-source MIT-licensed repository that does not enforce per-file copyright headers. Injecting custom copyright comments violates repo-wide conventions. | + +--- + +## 3. Analysis of Special Inquiries: URLs, Magic Numbers & Naming + +The task prompt directed a focused investigation on how Ruff evaluates long lines, magic numbers, and unit naming within `conversions/pressure_conversions.py`: + +### A. Long Lines and URLs (`E501`) +- **Observation**: `conversions/pressure_conversions.py` contains numerous reference URLs pointing to Wikipedia, MSE Student, and UnitConverters (lines 13–19). +- **Ruff Behavior**: Under `E501` (`line-too-long`), Ruff defaults to a maximum line length of 88 characters. Furthermore, Ruff's `E501` rule contains built-in exceptions for lines that consist predominantly of URLs or links that cannot be split without breaking link usability. +- **Decision**: **KEPT**. In `conversions/pressure_conversions.py`, all reference lines remain well within reasonable bounds (longest URL line is 67 characters), and wrapping or truncating URLs would diminish their utility as documentation links. + +### B. Magic Numbers (`PLR2004`) +- **Observation**: `PRESSURE_CONVERSION` contains raw float and integer conversion factors: `101325`, `0.986923`, `101.325`, `0.101325`, `14.6959`, `29.9213`, `760`, etc. +- **Ruff Behavior**: Ruff's `PLR2004` checks for magic numbers *used in comparison expressions* (e.g., `if val == 101325:`). It does not flag numeric literals defined in data dictionaries, namedtuples, or lookup tables. +- **Decision**: **KEPT**. Extracting these physical constants into separate global variables (e.g., `PASCAL_PER_ATM = 101325`) would introduce unnecessary indirection without improving readability. The values directly reflect standard SI and imperial physical conversion constants relative to 1 standard atmosphere (`atm`). + +### C. Unit Naming Conventions (`pep8-naming` / `N815`) +- **Observation**: The unit dictionary includes `"inHg"` (inches of mercury column), which combines lowercase (`in`) and mixed-case chemical symbol (`Hg`). +- **Ruff Behavior**: `pep8-naming` rules (such as `N815` for mixed-case class attributes, `N802` for functions, `N806` for variables) analyze identifier symbols, not string dictionary keys. +- **Decision**: **KEPT**. The string key `"inHg"` is the standard scientific, meteorological, and aviation symbol for inches of mercury. Changing `"inHg"` to lowercase `"inhg"` or uppercase `"IN_HG"` would break domain accuracy and violate backwards compatibility for callers passing unit names into `pressure_conversion()`. + +--- + +## 4. Verification and Validation Results + +All checks and tests pass with zero errors: + +1. **Repo-Config Ruff Check**: + ```bash + uvx ruff check conversions/pressure_conversions.py + # All checks passed! + ``` + +2. **Ruff Format Check**: + ```bash + uvx ruff format --check conversions/pressure_conversions.py + # 1 file already formatted + ``` + +3. **Strict Ruff (`--select ALL`) Check**: + ```bash + uvx ruff check --select ALL conversions/pressure_conversions.py + # Only 1 finding reported: CPY001 (Missing copyright notice - intentionally kept) + ``` + +4. **Doctests**: + ```bash + uv run python3 -m doctest -v conversions/pressure_conversions.py + # 9 tests in 3 items. + # 9 passed. Test passed. + ``` + +5. **Pytest Doctest-Modules**: + ```bash + uv run --with pytest pytest --doctest-modules conversions/pressure_conversions.py + # 1 passed in 0.02s + ``` diff --git a/work/R23-T028/__init__.py b/work/R23-T028/__init__.py new file mode 100644 index 000000000000..935fbd320b87 --- /dev/null +++ b/work/R23-T028/__init__.py @@ -0,0 +1 @@ +# work/R23-T028 package marker diff --git a/work/R23-T028/test_output.txt b/work/R23-T028/test_output.txt new file mode 100644 index 000000000000..c211ce7ca69f --- /dev/null +++ b/work/R23-T028/test_output.txt @@ -0,0 +1,83 @@ +=== 1. Repo-Config Ruff Check === +All checks passed! +=== 2. Ruff Format Check === +1 file already formatted +=== 3. Strict Ruff Check (--select ALL) === +warning: `incorrect-blank-line-before-class` (D203) and `blank-line-before-class` (D211) are incompatible. Ignoring `incorrect-blank-line-before-class`. +warning: `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible. Ignoring `multi-line-summary-second-line`. +CPY001 Missing copyright notice at top of file +--> conversions/pressure_conversions.py:1:1 + +Found 1 error. +=== 4. Doctests === +Trying: + pressure_conversion(4, "atm", "pascal") +Expecting: + 405300 +ok +Trying: + pressure_conversion(1, "pascal", "psi") +Expecting: + 0.00014401981999999998 +ok +Trying: + pressure_conversion(1, "bar", "atm") +Expecting: + 0.986923 +ok +Trying: + pressure_conversion(3, "kilopascal", "bar") +Expecting: + 0.029999991892499998 +ok +Trying: + pressure_conversion(2, "megapascal", "psi") +Expecting: + 290.074434314 +ok +Trying: + pressure_conversion(4, "psi", "torr") +Expecting: + 206.85984 +ok +Trying: + pressure_conversion(1, "inHg", "atm") +Expecting: + 0.0334211 +ok +Trying: + pressure_conversion(1, "torr", "psi") +Expecting: + 0.019336718261000002 +ok +Trying: + pressure_conversion(4, "wrongUnit", "atm") +Expecting: + Traceback (most recent call last): + ... + ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: + atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr +ok +2 items had no tests: + pressure_conversions + pressure_conversions.FromTo +1 item passed all tests: + 9 tests in pressure_conversions.pressure_conversion +9 tests in 3 items. +9 passed. +Test passed. +=== 5. Pytest Doctest-Modules === +============================= test session starts ============================== +platform darwin -- Python 3.15.2, pytest-9.1.1, pluggy-1.6.0 +rootdir: /Users/md.sohail/Documents/snai/TheAlgorithms-Python +configfile: pyproject.toml +plugins: anyio-4.15.1 +collected 1 item + +conversions/pressure_conversions.py . [100%] + +============================= slowest 10 durations ============================= +0.01s call conversions/pressure_conversions.py::conversions.pressure_conversions.pressure_conversion + +(2 durations < 0.005s hidden. Use -vv to show these durations.) +============================== 1 passed in 0.02s ===============================