Harder task: Robust, concurrency-safe LoginForm validation in a Flask-like package
Goal
Implement a robust LoginForm validation class in a Flask-like project structure, with attention to performance, edge cases, and concurrency safety. The tests will import the class via a package located at /app and will interact with it as described below.
What you must implement
Create a Python package at /app with an init.py, and a subpackage /app/flaskr/ that also contains an init.py.
Provide a module /app/flaskr/viewmodels.py containing the LoginForm class.
API and behavior
Class name: LoginForm
Constructor:
init(self, username, password, loginform_id=None)
Accepts username (str or None), password (str or None), and loginform_id (optional, any type; tests pass a string)
Stores the provided values on the instance for later use
Initializes internal state for error handling in a way that is safe for concurrent usage (no shared global state; per-instance state only)
Public methods:
def validate(self) -> bool
Must be idempotent and thread-safe across instances
Clears and reinitializes the internal error list for every call
Validation rules (must be enforced in all cases):
Treat whitespace-only strings as missing (use stripping)
Username is required: if username is None or username.strip() == '', record an error
Password is required: if password is None or password.strip() == '', record an error
If any errors are recorded, return False; otherwise return True
Do not perform any precedence or cross-field dependency checks beyond the two required fields
The loginform_id is stored and may be used for tracing/logging, but must not affect validation
The internal error state should be reset on each call to validate
def errors(self) -> list
Returns the current list of error messages accumulated by the last call to validate()
Must return a shallow copy to prevent external mutation of internal state
Error messages
Any reasonable, stable messages are acceptable (e.g., "Username is required.", "Password is required.")
The tests only assert the number of errors, not the exact content; however, provide consistent messages as described
Edge cases and performance
Strings containing only whitespace are treated as missing
Large inputs (very long strings) should not raise exceptions; validation should operate in linear time relative to input lengths
Unicode and non-ASCII characters must be supported in both fields
The loginform_id parameter is accepted and stored but not used in validation (used for correlation/logging in real scenarios)
No external dependencies beyond the Python standard library
Design considerations
The class should be explicitly designed for high-scale usage (per-invocation isolated state, no shared mutable state, fast operations)
The implementation should be easily testable and maintainable, with clear separation between input storage, validation, and error collection
Provide a compact internal mechanism for error collection (e.g., a private list, with a small helper if you like)
Acceptance criteria
A package at /app with:
/app/init.py
/app/flaskr/init.py
/app/flaskr/viewmodels.py containing the LoginForm class as specified
The provided LoginForm class must implement:
init(self, username, password, loginform_id=None)
validate(self) -> bool
errors(self) -> list
Behavior matches the original test expectations while incorporating the stricter, concurrency-safe and edge-case aware requirements:
Test 1: LoginForm(username='', password='', loginform_id='loginform_id')
validate() returns False
errors() returns a list with 2 items
Test 2: LoginForm(username=None, password=None, loginform_id='loginform_id')
validate() returns False
errors() returns a list with 2 items
Test 3: LoginForm(username='', password='aaa', loginform_id='loginform_id')
validate() returns False
errors() returns a list with 1 item
Test 4: LoginForm(username='zzz', password='', loginform_id='loginform_id')
validate() returns False
errors() returns a list with 1 item
The solution must be placed under /app/ as described, and imports in tests should work as:
from flaskr.viewmodels import LoginForm
Notes
Do not alter the tests; ensure the interface and behavior align with the described API and acceptance criteria.
The messages can be simple and stable; the critical aspect is the correct count of errors per scenario.