Repository navigation
Parse a string through from_string, on both types that parse one - #150
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
oierlauzi
approved these changes
Sep 18, 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.



Answers @oierlauzi's question on #148: whether
parse_image_locationis morePythonic as
ImageLocation.parseorImageLocation.from_string, andconsistently with
DeviceIndex.Why a static method and not a constructor
DeviceIndexcould take a parsing constructor;ImageLocationcannot.The one-string signature is free on
DeviceIndex, soDeviceIndex("cuda:0")can only mean "parse". On
ImageLocationit is already taken — theconstructor gives a lone string the meaning "this path, whole file" — so a
parsing overload would compete for it, and
ImageLocation("3@stack.mrc")would have two readings with no way to ask for the other. Making both types
parse through a constructor is therefore not on the table; making both parse
through a named method is.
Why
from_stringand notparserexlib documents
parse_image_locationas "the exact inverse ofto_string", and on this side that inverse is visible in the name:from_*is also the stdlib's spelling for an alternative constructor(
fromisoformat,from_bytes,from_float), whereparseas a classmethodis rarer. That choice settles the failure mode too: every one of those raises,
so
from_stringraisesValueErrorand theOptionalthis returned beforeis gone. A caller who wants "parse it, or treat it as a path" writes
try/except, which says so.What changed on DeviceIndex
.def(py::init(&from_string))becomes.def_static("from_string", ...). Themessage and the
ValueErrorare the ones it already raised; only the spellingof the entry point moved.
DeviceIndex("cuda:0")stops working. That is a public API break, taken nowbecause it is as cheap as it will ever be: one call site in the package
(
_device.py, behindrexlib.device('cpu')), two tests, version 0.1.0, andthe PyPI upload still
if: false. Every other use ofDeviceIndexin therepository is the two-argument form and is untouched.
Also
parse_image_locationmodule function is gone from_binding.em.imageand from the package's
__all__.define_image_locationloses itspybind11::module_parameter, which hadnothing left to register and would have warned under
-Wextra.test_image_location.py, beside the type theyexercise, the way
test_device_index.pyalready keeps both.AGENTS.mdrecords the convention and the reasonImageLocationforces it.Checked
ruff check .passes.g++ -std=c++20 -fsyntax-only -Wall -Wextra -Wpedantic.parse_image_locationandparse_positionrather than guessed: empty, index zero, empty index, non-digit index, empty
path.
parse_image_locationsurvives anywhere.tests/em/image/.🤖 Generated with Claude Code