Skip to content

Commit

Permalink
Component: make deprecated arg error via illegal args; deprecate 'siz…
Browse files Browse the repository at this point in the history
…e' (#3123)
  • Loading branch information
kmantel authored Nov 21, 2024
1 parent 272f3d1 commit 8822de0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
19 changes: 19 additions & 0 deletions psyneulink/core/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,9 @@ class Component(MDFSerializable, metaclass=ComponentsMeta):
componentType = None

standard_constructor_args = {EXECUTE_UNTIL_FINISHED, FUNCTION_PARAMS, MAX_EXECUTIONS_BEFORE_FINISHED, RESET_STATEFUL_FUNCTION_WHEN, INPUT_SHAPES}
deprecated_constructor_args = {
'size': 'input_shapes',
}

# helper attributes for MDF model spec
_model_spec_id_parameters = 'parameters'
Expand Down Expand Up @@ -2150,8 +2153,11 @@ def alias_conflicts(alias, passed_name):

conflicting_aliases = []
unused_constructor_args = {}
deprecated_args = {}
for p in self.parameters:
if p.name in illegal_passed_args:
# p must have a constructor_argument, because otherwise
# p.name would not be in illegal_passed_args
assert p.constructor_argument is not None
unused_constructor_args[p.name] = p.constructor_argument

Expand All @@ -2164,13 +2170,26 @@ def alias_conflicts(alias, passed_name):
if alias_conflicts(p, passed_name):
conflicting_aliases.append((p.source.name, passed_name, p.name))

for arg in illegal_passed_args:
try:
deprecated_args[arg] = self.deprecated_constructor_args[arg]
except KeyError:
continue

# raise constructor arg errors
if len(unused_constructor_args) > 0:
raise create_illegal_argument_error([
f"'{arg}': must use '{constr_arg}' instead"
for arg, constr_arg in unused_constructor_args.items()
])

# raise deprecated argument errors
if len(deprecated_args) > 0:
raise create_illegal_argument_error([
f"'{arg}' is deprecated. Use '{new_arg}' instead"
for arg, new_arg in deprecated_args.items()
])

# raise generic illegal argument error
unknown_args = illegal_passed_args.difference(unused_constructor_args)
if len(unknown_args) > 0:
Expand Down
26 changes: 26 additions & 0 deletions tests/components/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ def test_execute_manual_context(self, component_type):

class TestConstructorArguments:
class NewTestMech(pnl.Mechanism_Base):
deprecated_constructor_args = {
**pnl.Mechanism_Base.deprecated_constructor_args,
**{
'deprecated_param': 'new_param',
}
}

class Parameters(pnl.Mechanism_Base.Parameters):
cca_param = pnl.Parameter('A', constructor_argument='cca_constr')
param_with_alias = pnl.Parameter(None, constructor_argument='pwa_constr_arg', aliases=['pwa_alias'])
Expand Down Expand Up @@ -226,6 +233,25 @@ def test_invalid_argument(self, cls_, argument_name, param_value, params_dict_en
constr_arg = getattr(cls_.parameters, argument_name).constructor_argument
assert f"'{argument_name}': must use '{constr_arg}' instead" in str(err.value)

@pytest.mark.parametrize(
'cls_, argument_name, new_name',
[
(NewTestMech, 'deprecated_param', 'new_param'),
(NewTestMech, 'size', 'input_shapes'),
(pnl.TransferMechanism, 'size', 'input_shapes'),
]
)
@pytest.mark.parametrize('params_dict_entry', [NotImplemented, 'params'])
def test_invalid_argument_deprecated(self, cls_, argument_name, new_name, params_dict_entry):
with pytest.raises(
pnl.ComponentError,
match=(
rf".*Illegal argument in constructor \(type: {cls_.__name__}\):"
f"\n\t'{argument_name}' is deprecated. Use '{new_name}' instead"
)
):
cls_(**nest_dictionary({argument_name: new_name}, params_dict_entry))

@pytest.mark.parametrize(
'cls_, param_name, param_value, alias_name, alias_value',
[
Expand Down

0 comments on commit 8822de0

Please sign in to comment.