Skip to content

Replace per-field assignment in update_by_squad_number_async with a setattr loop #641

Description

@nanotaboada

Problem

services/player_service.py::update_by_squad_number_async copies the incoming
Pydantic model onto the ORM object one attribute at a time:

player.first_name = player_model.first_name
player.middle_name = player_model.middle_name
player.last_name = player_model.last_name
# ...ten assignments in total

Every field addition needs another line here, and a missed line fails silently
(the field just never updates). This is manual property mapping, a .NET/Java
habit. Note that create_async in the same module already uses the idiomatic
form — Player(**player_model.model_dump()) — so the two paths are
inconsistent.

Proposed Solution

Dump the validated model to a dict and assign with setattr in a loop.
model_dump() uses the Python field names (snake_case), which match the ORM
attribute names, so the mapping is 1:1.

for field, value in player_model.model_dump().items():
    setattr(player, field, value)

The route layer already guarantees player_model.squad_number == squad_number
(400 on mismatch), so including the natural key in the loop is safe.

Suggested Approach

  1. Replace the ten assignments with the loop above.
  2. Decide whether to keep an explicit player.squad_number = squad_number
    after the loop (documents that the path parameter is authoritative) or rely
    on the route guard — leave a one-line comment either way.
  3. Run uv run pytest, including the PUT tests (existing update, mismatch,
    unknown squad number).

Acceptance Criteria

  • Field copying is a single loop, not per-field assignments
  • Full-replace PUT semantics preserved
  • PUT tests pass, including the squad-number mismatch case
  • Consistent with create_async's use of model_dump()
  • CHANGELOG.md updated

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpriority:lowNice-to-have improvement. Can be deferred without blocking other work.pythonPull requests that update Python codepython:idiomsRefactors toward idiomatic Python (PEP conventions, stdlib idioms)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions