Skip to content

Commit

Permalink
Handle missing attributes with default field values in DataClassState
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianopaz committed Nov 20, 2024
1 parent 2ce289c commit 2ec8d27
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pymc/step_methods/hmc/quadpotential.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def current_mean(self, out=None):
class QuadPotentialDiagAdaptExpState(QuadPotentialDiagAdaptState):
_alpha: float
_stop_adaptation: float
_variance_estimator: ExpWeightedVarianceState
_variance_estimator: ExpWeightedVarianceState | None = None

_variance_estimator_grad: ExpWeightedVarianceState | None = None

Expand Down
8 changes: 6 additions & 2 deletions pymc/step_methods/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from copy import deepcopy
from dataclasses import Field, dataclass, fields
from dataclasses import MISSING, Field, dataclass, fields
from typing import Any, ClassVar

import numpy as np
Expand Down Expand Up @@ -67,7 +67,11 @@ def sampling_state(self) -> DataClassState:
state_class = self._state_class
kwargs = {}
for field in fields(state_class):
val = getattr(self, field.name)
val = getattr(self, field.name, field.default)
if val is MISSING:
raise AttributeError(
f"{type(self).__name__!r} object has no attribute {field.name!r}"
)
_val: Any
if isinstance(val, WithSamplingState):
_val = val.sampling_state
Expand Down

0 comments on commit 2ec8d27

Please sign in to comment.