Skip to content

Commit

Permalink
mdf: catch more pint parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kmantel committed Jan 15, 2025
1 parent 0ef9565 commit 3a06324
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions psyneulink/core/globals/mdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@
import psyneulink
import re
import tempfile
import tokenize
import types
import time
import warnings
from typing import Any, Union

from psyneulink.core.globals.keywords import \
MODEL_SPEC_ID_GENERIC, MODEL_SPEC_ID_PARAMETER_SOURCE, \
Expand Down Expand Up @@ -191,6 +193,29 @@ def default(self, o):
return str(o)


def _parse_pint_object(obj: Any) -> Union[pint.Unit, pint.Quantity, None]:
# varying and don't have a consistent way to identify otherwise
pint_errs = (
AssertionError,
AttributeError,
TypeError,
ValueError,
pint.errors.DefinitionSyntaxError,
tokenize.TokenError,
)
try:
return psyneulink._unit_registry.Unit(obj)
except pint_errs:
pass

try:
return psyneulink._unit_registry.Quantity(obj)
except pint_errs:
pass

return None


def _get_variable_parameter_name(obj):
try:
if obj.parameters.variable.mdf_name is not None:
Expand Down Expand Up @@ -482,18 +507,7 @@ def _parse_parameter_value(value, component_identifiers=None, name=None, parent_
if identifier in component_identifiers:
value = identifier

try:
psyneulink._unit_registry.Unit(value)
except (AttributeError, TypeError, ValueError, pint.errors.DefinitionSyntaxError):
pass
else:
value = f"'{value}'"

try:
psyneulink._unit_registry.Quantity(value)
except (AttributeError, TypeError, ValueError, pint.errors.DefinitionSyntaxError):
pass
else:
if _parse_pint_object(value) is not None:
value = f"'{value}'"

evaluates = False
Expand Down

0 comments on commit 3a06324

Please sign in to comment.