Skip to content

refactor: Use match statements in measurement document dispatch - #1272

Open
nathan-stender wants to merge 1 commit into
mainfrom
refactor/use-match-statements
Open

nathan-stender wants to merge 1 commit into
mainfrom
refactor/use-match-statements

Conversation

@nathan-stender

Copy link
Copy Markdown
Collaborator

Summary

Benchling is on Python 3.12, which clears the blocker recorded in the four TODO(switch-statement) markers ("use switch statement once Benchling can use 3.10 syntax"). This converts all four, fixes a latent bug found along the way, and adds a missing trove classifier.

1. match statement conversions (4 sites)

  • schema_mappers/adm/plate_reader/benchling/_2023/_09/plate_reader.py
  • schema_mappers/adm/plate_reader/rec/_2024/_06/plate_reader.py
  • schema_mappers/adm/plate_reader/rec/_2025/_03/plate_reader.py
  • schema_mappers/adm/spectrophotometry/benchling/_2023/_12/spectrophotometry.py

All four were already flat equality tests on measurement.type_ with no compound conditions and no fall-through, so these are 1:1 translations. The elif measurement.type_ in [...] branches become or-patterns. Every case uses a dotted enum name (a value pattern) — a bare name would be a capture pattern that matches anything, which is the classic way to silently break a match.

The only non-mechanical part of the diff is line wrapping: the extra indent level pushed a few calls past 88 chars, so black rewrapped them.

2. Latent bug fix: measurement.type -> measurement.type_

The 2023/09 plate reader mapper's fallback branch built its error message from measurement.type, but the dataclass field is type_ and there is no type attribute. That branch would have raised AttributeError instead of the intended AllotropyParserError.

Worth noting why strict mypy never caught this, since it's a general blind spot: that mapper's MeasurementType has exactly the four members the if/elif chain covers, so mypy narrows the enum exhaustively, concludes the else: body is unreachable, and skips type-checking it entirely. The bug was only reachable if a fifth enum member were added without updating the dispatch — which is precisely when you'd want the real error message.

3. Missing Programming Language :: Python :: 3.12 classifier

pyproject.toml declared requires-python = ">=3.10" and classifiers for 3.10 and 3.11, but stopped there — even though CI runs a 3.12 job and the hatch test_all matrix includes 3.12. This is packaging metadata only; it corrects what PyPI advertises.

Testing

  • hatch run lint — clean (ruff, black, mypy: no issues in 795 source files)
  • hatch run test_all.py3.10:pytest tests/ -q — 1520 passed
  • git diff main --stat -- 'tests/parsers/*/testdata/*.json' — empty, confirming no parser output changed

The 3.10 run matters specifically here: match is 3.10 syntax, and the repo's floor is 3.10, so this verifies the conversions on the oldest supported interpreter rather than just the 3.11 default env.

One thing reviewers may want to confirm: black is pinned at 22.3.0 (March 2022), old enough that match parsing was a fair question. It parses and formats these correctly — no pin bump needed.

🤖 Generated with Claude Code

Benchling is on Python 3.12, which clears the TODO(switch-statement) markers
asking for match statements "once Benchling can use 3.10 syntax". Converts all
four measurement-type dispatches. Each was already a flat equality test on
measurement.type_, so these are 1:1 translations — the `in [...]` branches
become or-patterns.

Also fixes a latent bug uncovered while converting: the 2023/09 plate reader
mapper's fallback branch referenced `measurement.type`, but the field is
`type_`, so that branch would have raised AttributeError instead of the
intended AllotropyParserError. Strict mypy never caught it because that
mapper's MeasurementType has exactly the four members the chain covers, so
mypy narrows the enum exhaustively, marks the branch unreachable, and skips
type-checking its body.

Adds the missing Python 3.12 trove classifier, which CI and the hatch test
matrix have both covered for a while.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

2 participants