Skip to content

Comments

Fix pickling of Artifact.from_type(...) classes#259

Closed
JacobHayes wants to merge 1 commit intogoldenfrom
pickle-dynamic-artifacts
Closed

Fix pickling of Artifact.from_type(...) classes#259
JacobHayes wants to merge 1 commit intogoldenfrom
pickle-dynamic-artifacts

Conversation

@JacobHayes
Copy link
Member

Description

Pickling of instances from Artifact.from_type(...) classes fails because the class name is not actually available in arti.artifacts. This fixes that by using a _DynamicArtifact intermediate class with a custom __reduce__ implementation that triggers the dynamic class to be regenerated and then an object instantiated.

While we could simply add the generated types to globals() after generation, that wouldn't handle:

  • loading a pickled representation from a separate session if that specific dynamic class hadn't already been generated in this session
  • classes with duplicate names (they're currently named based on the Type.__name__, but there might be multiple, different instance of the same type)

I think there are some larger changes we can make to Artifact/Producer that will remove the need for these dynamic classes (and thus these hacks), but this will suffice for now.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Tests have been added for simple pickle.{dumps,loads} in the same session, but I tested reloading a pickle file across sessions by hand (this is harder to reliably test for).

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in upstream modules

@codecov-commenter
Copy link

codecov-commenter commented Jul 30, 2022

Codecov Report

Merging #259 (e8c9679) into golden (b4f1c81) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            golden      #259   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           41        41           
  Lines         2534      2544   +10     
  Branches       523       523           
=========================================
+ Hits          2534      2544   +10     
Impacted Files Coverage Δ
src/arti/artifacts/__init__.py 100.00% <100.00%> (ø)

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@JacobHayes
Copy link
Member Author

Superseded by #263.

@JacobHayes JacobHayes closed this Aug 3, 2022
@JacobHayes JacobHayes deleted the pickle-dynamic-artifacts branch August 3, 2022 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants