Skip to content

fix: raise clear ValueError for unsupported MemoryUnit - #1336

Merged
Revathyvenugopal162 merged 7 commits into
mainfrom
migration/pr-1334
Sep 22, 2026
Merged

Revathyvenugopal162 merged 7 commits into
mainfrom
migration/pr-1334

Conversation

@pyansys-ci-bot

Copy link
Copy Markdown
Collaborator

This PR is a mirror pull request created from fix: raise clear ValueError for unsupported MemoryUnit to allow the code to access PyDyna CICD secrets.

Check the original PR made by @guhou-hvi for more details.

Closes #1334

Original pull request

fix: raise clear ValueError for unsupported MemoryUnit

Summary

BaseRunner.get_memory_string() raises a bare KeyError for any MemoryUnit other than MB/GB. This PR replaces that with a clear ValueError matching what the enum docstring already promises.

Root cause

MemoryUnit in src/ansys/dyna/core/run/options.py defines five values:

class MemoryUnit:
    """Memory unit for LS-DYNA command line.  Only MB and GB are currently supported."""

    BYTE = 0
    KB = 1
    MB = 2
    GB = 3
    TB = 4

But get_memory_string() only maps two of them:

unit = {MemoryUnit.MB: "m", MemoryUnit.GB: "G"}[self.memory_unit]

Passing MemoryUnit.BYTE, KB, or TB (or any other value) raises:

KeyError: 1

MemoryUnit is a plain class of ints, so the message is just the raw integer — it doesn't mention which enum member was used, doesn't list what's supported, and doesn't align with the docstring's "Only MB and GB are currently supported" promise. All three concrete runners (LinuxRunner, WindowsRunner, DockerRunner) call self.get_memory_string() while building the command line, so the failure surfaces late and looks like an internal lookup error.

Changes

src/ansys/dyna/core/run/base_runner.py:

  • Hoist the supported units map into a local units dict.

  • Check membership explicitly and raise ValueError with the offending value and the supported names:

    f"Unsupported memory unit: {self.memory_unit}. "
    f"Only MemoryUnit.MB and MemoryUnit.GB are supported."
  • Return f"{self.memory}{units[self.memory_unit]}" unchanged for supported units.

tests/test_base_runner.py (new):

  • test_memory_string_supported – parameterised over MB → "20m" and GB → "20G".
  • test_memory_string_unsupported_raises_value_error – parameterised over BYTE, KB, TB; asserts ValueError with the supported-units message.
  • test_memory_string_error_mentions_offending_value – checks the message includes the value the caller passed.
  • test_memory_string_default_is_mb – pins the default from BaseRunner.__init__.

The new tests are pure mocks (no solver, no Docker, no filesystem) and deliberately do not carry pytest.mark.run, so they execute in the default unit test matrix (pytest -m "not codegen and not run" in CI) rather than only in the solver-backed job.

Testing

Check Result
pytest tests/test_base_runner.py -v 7 passed
Regression pytest tests/test_localrun.py tests/test_windows_runner.py -m run 36 passed, 2 failed
Same regression with the fix stashed (baseline) 2 failed, identical errors
ruff check src tests All checks passed
ruff format --check Files already formatted

The two test_localrun.py failures are pre-existing environmental issues on this machine, unrelated to this change:

  • test_find_solver_with_version – expects Ansys 231, host has 221.
  • test_find_solver_latest_install – Linux-style path separator (/) asserted against Windows os.path.join output (\).

Impact

Callers that today pass MemoryUnit.BYTE/KB/TB will receive a ValueError instead of a KeyError. ValueError is a subclass of Exception, so any code catching the general Exception still works; code specifically catching KeyError (unlikely — the raw message gives no hint that this is a supported-units issue) would need to update. Default (MemoryUnit.MB) and MemoryUnit.GB behaviour is unchanged.

Scope

Only BaseRunner.get_memory_string() changes. No modification to MemoryUnit itself, the three concrete runners, or any docstring in this PR. Related local_solver.py follow-up (env var robustness) is being tracked separately and does not conflict.

@Revathyvenugopal162 Revathyvenugopal162 changed the title migrated (PR 1334): fix: raise clear ValueError for unsupported MemoryUnit fix: raise clear ValueError for unsupported MemoryUnit Sep 18, 2026
@Revathyvenugopal162

Copy link
Copy Markdown
Contributor

@koubaa This Pr is created from #1334 , please review

Comment thread tests/test_base_runner.py Resolved
Comment thread tests/test_base_runner.py Resolved
Comment thread tests/test_base_runner.py Resolved
@Revathyvenugopal162
Revathyvenugopal162 merged commit 2fa7308 into main Sep 22, 2026
17 checks passed
@Revathyvenugopal162
Revathyvenugopal162 deleted the migration/pr-1334 branch September 22, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Anything related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants