Seguir la guia de Pydantic para suprimir una advertencia by FanGoH · Pull Request #182 · cuenca-mx/clabe-python
⚠️ Potential issue | 🔴 Critical
🧩 Analysis chain
CRITICAL: Whitespace stripping behavior is broken.
Moving strip_whitespace to json_schema_extra only adds metadata to the JSON schema—it does not preserve the runtime whitespace stripping behavior that existed in Pydantic v1. The field will no longer strip whitespace from bank names during validation.
For Pydantic v2, use StringConstraints to preserve the behavior:
-from pydantic import BaseModel, Field +from pydantic import BaseModel, Field, StringConstraints +from typing import Annotated class BankConfigRequest(BaseModel): """ Validates and processes bank configuration requests. The class handles validation of bank names and codes, ensuring: - Bank names are non-empty strings - Banxico codes are exactly 5 digits """ - bank_name: str = Field( + bank_name: Annotated[str, StringConstraints(strip_whitespace=True)] = Field( min_length=1, description="Bank name must have at least 1 character.", - json_schema_extra={ - "strip_whitespace": True, - } )
This follows the Pydantic v2 migration guide and maintains the actual whitespace stripping functionality. As per coding guidelines.
Verify the current behavior with whitespace:
🏁 Script executed:
#!/bin/bash # Description: Test if whitespace stripping still works python3 << 'EOF' from clabe.validations import BankConfigRequest # Test with leading/trailing whitespace try: request = BankConfigRequest( bank_code_banxico="90716", bank_name=" Test Bank " ) print(f"bank_name value: '{request.bank_name}'") print(f"Whitespace stripped: {request.bank_name == 'Test Bank'}") except Exception as e: print(f"Error: {e}") EOF
Length of output: 59
CRITICAL: Whitespace stripping behavior is broken—verification confirms field retains spaces.
The test confirms that moving strip_whitespace to json_schema_extra does not preserve whitespace stripping. Input " Test Bank " remains unchanged instead of being stripped to "Test Bank". This violates the coding guideline requiring built-in Pydantic validators.
Fix at lines 77–83 using StringConstraints and Annotated:
from typing import Annotated from pydantic import StringConstraints bank_name: Annotated[str, StringConstraints(strip_whitespace=True)] = Field( min_length=1, description="Bank name must have at least 1 character.", )
This is the correct Pydantic v2 approach and will restore runtime whitespace stripping.
🤖 Prompt for AI Agents
In clabe/validations.py around lines 77–83, the Field currently uses
json_schema_extra to set strip_whitespace which does not perform runtime
stripping; replace that with Pydantic v2 string constraints by switching the
annotation to use typing.Annotated with
pydantic.StringConstraints(strip_whitespace=True) and remove strip_whitespace
from json_schema_extra so the bank_name field becomes an Annotated[str,
StringConstraints(strip_whitespace=True)] with the existing Field(min_length=1,
description=...) to restore runtime whitespace stripping.