Skip to content

loqs.tools.migrate.renames

Straight (module, name) rename/removal rewriting for old source files.

Reuses Serializable's own IMPORT_LOCATION_CHANGES_BY_VERSION table (the single source of truth for every version's class relocations/renames) as its data, via _get_cumulative_changes(0), plus a small number of additional entries that never needed a decode-time compatibility entry at all (e.g. a class renamed before its old name ever shipped), but are still worth fixing in a user's own source files.

Two entries are deliberately overridden to a deleted-outright (None) target rather than reused as-is, even though a real decode-time target exists for each: RepTuple and STIMDictNoiseModel. Decoding already-serialized data only ever needs to know where the modern class lives, since the decoded attribute values are already correctly shaped for it. Rewriting live source code is a different problem -- it needs the old constructor call to also make sense as a call to the new class, and for these two specifically it doesn't: RepTuple(rep, qubits, reptype)'s reptype dispatches across ~10 differently-shaped concrete GateRep/ InstrumentRep classes (no single new class to rewrite the call to), and STIMDictNoiseModel(model_or_dicts, ...) takes its gate/instrument data as one positional tuple where DictNoiseModel(gate_dict, inst_dict, ...) takes two separate positional arguments -- a blind text rewrite of either would produce code that runs but silently misbehaves or breaks outright. Both are still constructible today via a deprecated compatibility shim (see loqs.backends.model.__init__ for STIMDictNoiseModel; RepTuple has no such shim, since even its shim's own realistic call pattern -- an old GateRep/InstrumentRep enum member as reptype -- fails on that attribute access before a shim could ever run), so flagging rather than either rewriting or hard-erroring is the honest outcome here.

_update_imports is a thin wrapper around this same function, so frozen function source and whole user source files share one rewrite mechanism. Both rewrite passes resolve real code references via libcst's QualifiedNameProvider, so they can tell an actual usage of a renamed class apart from the same text appearing in a docstring, comment, or unrelated identifier, and update both the import statement and every usage site together. Neither rewrites or flags a name inside a quoted annotation (x: "PatchDict"), which keeps its old name.

Same-module renames (the *CastableTypes -> *Like renames, the instrument-rep rename and the PatchDict re-exports) go through LoQS's own pass, which keeps the user's import style: module aliases, as names and import placement. RenameCommand writes such a rename reached through import a.b as x as a.b.New without binding a (#161), and drops the from-import when the file also has import a.b. Module-changing renames use libcst's RenameCommand (libcst.codemod.commands.rename); LoQS adds the from-import it drops when the file has both import <old module> and a from-import of the old name.

A few real RenameCommand behaviors, which now apply only to module-changing renames, worth knowing about before trusting this pass blindly:

  1. It always adds the new import at module scope, even when the old one was function-local (e.g. a lazy import used to avoid a circular import, a real pattern in this codebase). Still valid Python, but changes where the import happens.
  2. It drops a renamed import entirely if the renamed name ends up with no remaining real code reference (i.e. it was only ever mentioned in a comment/docstring, or never used at all) -- it does not distinguish "genuinely unused" from "moved but the rest of this rename batch already removed its only usage".
  3. Chaining several RenameCommand passes over the same module (one per renamed name the file really references, done here since each has its own old/new location) can leave a stray blank line and trailing comma behind in a multi-name from ... import (...) block when an earlier pass's partial cleanup isn't reformatted by a later pass. Still valid Python, just untidy.

None of these are incorrect Python, but review the diff before committing rather than applying a whole-file rewrite unseen, same as any other automated rewrite.

Attributes

Name Type Value Doc
RENAMES dict[tuple[str, str], tuple[str, str] | None] {**_get_cumulative_changes(0), ('instrumentreps', _OLD_INSTRUMENT_REP_NAME): ('instrumentreps', 'OutcomeOperationDictInstrumentRep'), ('reps', 'RepTuple'): None, ('stimdictmodel', 'STIMDictNoiseModel'): None, ('recordables', 'PatchDict'): ('recordables', 'PatchLayout'), ('core', 'PatchDict'): ('core', 'PatchLayout')} (old_module, old_name) -> (new_module, new_name) | None (None means deleted outright, not relocated -- rewriting flags real remaining code references for manual review instead of guessing a replacement).

Functions

  1. It always adds the new import at module scope, even when the old one was function-local (e.g. a lazy import used to avoid a circular import, a real pattern in this codebase). Still valid Python, but changes where the import happens.
  2. It drops a renamed import entirely if the renamed name ends up with no remaining real code reference (i.e. it was only ever mentioned in a comment/docstring, or never used at all) -- it does not distinguish "genuinely unused" from "moved but the rest of this rename batch already removed its only usage".
  3. Chaining several RenameCommand passes over the same module (one per renamed name the file really references, done here since each has its own old/new location) can leave a stray blank line and trailing comma behind in a multi-name from ... import (...) block when an earlier pass's partial cleanup isn't reformatted by a later pass. Still valid Python, just untidy.

None of these are incorrect Python, but review the diff before committing rather than applying a whole-file rewrite unseen, same as any other automated rewrite.

rewrite_renames

rewrite_renames(source: str, renames: Mapping[tuple[str, str], tuple[str, str] | None] | None = None) -> MigrationResult

Rewrite every real code reference (import or usage) to a renamed (module, name) in renames throughout source, and flag every real code reference to a name deleted outright (a None entry) for manual review instead.

Parameters:

Name Type Description Default
source str

The full text of a .py file (or an extracted code cell -- see notebook).

required
renames Mapping[tuple[str, str], tuple[str, str] | None] | None

The (old_module, old_name) -> (new_module, new_name) | None table to rewrite against. Defaults to RENAMES; overriding it is mainly useful for testing against a synthetic table.

None