Repository navigation
refactor: Use match statements in measurement document dispatch - #1272
Open
nathan-stender wants to merge 1 commit into
Open
nathan-stender wants to merge 1 commit into
nathan-stender wants to merge 1 commit into
Conversation
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>
stephenworlow
approved these changes
Sep 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
matchstatement conversions (4 sites)schema_mappers/adm/plate_reader/benchling/_2023/_09/plate_reader.pyschema_mappers/adm/plate_reader/rec/_2024/_06/plate_reader.pyschema_mappers/adm/plate_reader/rec/_2025/_03/plate_reader.pyschema_mappers/adm/spectrophotometry/benchling/_2023/_12/spectrophotometry.pyAll four were already flat equality tests on
measurement.type_with no compound conditions and no fall-through, so these are 1:1 translations. Theelif measurement.type_ in [...]branches become or-patterns. Everycaseuses 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 amatch.The only non-mechanical part of the diff is line wrapping: the extra indent level pushed a few calls past 88 chars, so
blackrewrapped 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 istype_and there is notypeattribute. That branch would have raisedAttributeErrorinstead of the intendedAllotropyParserError.Worth noting why strict mypy never caught this, since it's a general blind spot: that mapper's
MeasurementTypehas exactly the four members theif/elifchain covers, so mypy narrows the enum exhaustively, concludes theelse: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.12classifierpyproject.tomldeclaredrequires-python = ">=3.10"and classifiers for 3.10 and 3.11, but stopped there — even though CI runs a 3.12 job and the hatchtest_allmatrix 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 passedgit diff main --stat -- 'tests/parsers/*/testdata/*.json'— empty, confirming no parser output changedThe 3.10 run matters specifically here:
matchis 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:
blackis pinned at22.3.0(March 2022), old enough thatmatchparsing was a fair question. It parses and formats these correctly — no pin bump needed.🤖 Generated with Claude Code