Skip to content

refactor: replace if/elif unit dispatch with _UNIT_POWERS lookup#10

Merged
underwoo merged 2 commits into
mainfrom
fix/issue-4-5-format-refactor
Jun 22, 2026
Merged

refactor: replace if/elif unit dispatch with _UNIT_POWERS lookup#10
underwoo merged 2 commits into
mainfrom
fix/issue-4-5-format-refactor

Conversation

@underwoo

Copy link
Copy Markdown
Owner

Fixes #4 and #5

Two refactors grouped because they touch the same code.

#4 — Dead initialization

Removed n = self.real — it was always overwritten by the if/elif chain.

#5 — Duplicate _UNIT_POWERS mapping

Replaced the 6-branch if/elif chain with a single dict lookup:

if unit not in _UNIT_POWERS:
    raise AssertionError(f"unhandled unit: {unit!r}")
n = self.real / self._convert ** _UNIT_POWERS[unit]

Adding a new unit is now a single-site change (add to _UNIT_POWERS). Unknown units raise AssertionError instead of silently defaulting to K.

Verification:

  • pytest: 47/47 passed
  • mypy: clean
  • pylint: clean

Closes #4 and #5

underwoo and others added 2 commits June 21, 2026 21:03
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…refactor

# Conflicts:
#	tests/test_fsize_init.py
@underwoo underwoo merged commit 95a589f into main Jun 22, 2026
9 checks passed
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.

refactor: n = self.real in __format__ is a dead initialization

1 participant