FIX Preserve defaultdicts in to_device - #1153
Conversation
|
Thanks for the PR. This sounds like an extreme edge case. Did you run into this issue yourself or is this a pro-active fix? |
Thanks! This was a proactive fix rather than something I encountered in an application. While reviewing to_device's handling of Mapping subclasses, I noticed that reconstructing mappings with type(X)(mapping) is incompatible with defaultdict, since its first positional argument is the default factory. |
b2c8f4d to
389df27
Compare
|
Thanks for clarifying. I think it's very unlikely that defaultdicts would be used here, that has never come up so far. Adding the extra complexity for a purely theoretical use case is not worth it. Still, thanks for the PR. |
Summary
collections.defaultdictinstances when recursively moving mapping valuesdefault_factoryProblem
to_devicesupports mapping inputs and reconstructs them withtype(X)(mapping). For adefaultdict, the first constructor argument is the default factory, so the public utility currently raises:This change uses the
defaultdict(default_factory, mapping)constructor while retaining the existing behavior for other mapping types.Tests
python -m pytest skorch/tests/test_utils.py -q -k TestToDevice(13 passed, 17 skipped)python -m pytest skorch/tests/test_utils.py -q(105 passed, 22 skipped)git diff --checkFull-file Flake8/Pylint checks report pre-existing findings in these files; the changed lines introduce no Flake8 diagnostics.
AI assistance
This pull request was prepared with Codex assistance. I reviewed the reproduced failure, the focused implementation, and the test results before submission.