Skip to content

KeyError: 'molecule_yml_date_modified' in molecule/state.py (line 187) — use safe .get() to avoid crash #4597

@allaunthefox

Description

@allaunthefox

Prerequisites

  • This was not already reported in the past (duplicate check)
  • It does reproduce it with code from main branch (latest unreleased version)
  • I include a minimal example for reproducing the bug
  • The bug is not trivial, as for those a direct pull-request is preferred
  • Running pip check does not report any conflicts
  • I was able to reproduce the issue on a different machine
  • The issue is not specific to any driver other than 'default' one

Environment

  • Molecule version: 25.12.0 (also present in 25.12.1.dev10)
  • Python: 3.12
  • OS/Distro: GitHub Codespace (see reproduction below)
  • Driver: default (core state bug; driver independent)
  • Method: direct property access and scenario test (see below for code)

What happened

Accessing State.molecule_yml_date_modified raises an unhandled KeyError when the internal state dictionary lacks the 'molecule_yml_date_modified' key:

KeyError Details

  • Exception type: KeyError
  • Exception text (str(e)): 'molecule_yml_date_modified'
  • missing_key (e.args[0]): molecule_yml_date_modified
  • repr(e): KeyError('molecule_yml_date_modified')
  • Traceback excerpt:
Traceback (most recent call last):
  File "<stdin>", line 26, in <module>
  File "/workspaces/deploy-system-unified/.venv/lib/python3.12/site-packages/molecule/state.py", line 187, in molecule_yml_date_modified
    return self._data["molecule_yml_date_modified"]
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'molecule_yml_date_modified'

Expected: Should return None (matching property type float | None) when the key is missing, not crash.

Reproducing example

Minimal direct Python reproduction:

from molecule.state import State
import tempfile

class MockScenario:
    def __init__(self):
        self.ephemeral_directory = tempfile.mkdtemp()

class MockConfig:
    def __init__(self):
        self._run_uuid = "test-uuid"
        self.is_parallel = False
        self.scenario = MockScenario()

config = MockConfig()
s = State(config)

# Ensure key is absent to trigger KeyError
if 'molecule_yml_date_modified' in s._data:
    del s._data['molecule_yml_date_modified']

# This raises the observed KeyError
print(s.molecule_yml_date_modified)


**Files and scripts for reproduction and verification attached: 'diff.md', 'errortrace.md', 'reproduce.py', 'keyerror.sh'**

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions