Skip to content

Fix a bug with custom attribute name clashes #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Make skorch compatible with sklearn 0.22
- Fixed a bug that could occur when a new "settable" (via `set_params`) attribute was added to `NeuralNet` whose name starts the same as an existing attribute's name

## [0.7.0] - 2019-11-29

Expand Down
5 changes: 4 additions & 1 deletion skorch/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,10 @@ def _check_kwargs(self, kwargs):
for key in kwargs:
if key.endswith('_'):
continue
for prefix in self.prefixes_:

# see https://github.com/skorch-dev/skorch/pull/590 for
# why this must be sorted
for prefix in sorted(self.prefixes_, key=lambda s: (-len(s), s)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future maintainability, add a comment about why this sorting is needed or at least a link to this issue. (It is not obvious at first glance why this is needed)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment

if key.startswith(prefix):
if not key.startswith(prefix + '__'):
missing_dunder_kwargs.append((prefix, key))
Expand Down
23 changes: 22 additions & 1 deletion skorch/tests/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_net_init_one_unknown_argument(self, net_cls, module_cls):
"should deal with the new arguments explicitely.")
assert e.value.args[0] == expected

def test_net_init_two_unknown_argument(self, net_cls, module_cls):
def test_net_init_two_unknown_arguments(self, net_cls, module_cls):
with pytest.raises(TypeError) as e:
net_cls(module_cls, lr=0.1, mxa_epochs=5,
warm_start=False, bathc_size=20)
Expand Down Expand Up @@ -231,6 +231,27 @@ def test_net_init_missing_dunder_and_unknown(
"did you mean iterator_train__shuffle?")
assert e.value.args[0] == expected

def test_net_with_new_attribute_with_name_clash(
self, net_cls, module_cls):
# This covers a bug that existed when a new "settable"
# argument was added whose name starts the same as the name
# for an existing argument
class MyNet(net_cls):
# add "optimizer_2" as a valid prefix so that it works
# with set_params
prefixes_ = net_cls.prefixes_[:] + ['optimizer_2']

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.optimizer_2 = torch.optim.SGD

# the following line used to raise this error: "TypeError: Got
# an unexpected argument optimizer_2__lr, did you mean
# optimizer__2__lr?" because it was erronously assumed that
# "optimizer_2__lr" should be dispatched to "optimizer", not
# "optimizer_2".
MyNet(module_cls, optimizer_2__lr=0.123) # should not raise

def test_fit(self, net_fit):
# fitting does not raise anything
pass
Expand Down