-
Notifications
You must be signed in to change notification settings - Fork 37
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
Crash with SingleFileSnapshotExtension #749
Comments
The single file snapshot extension is meant for string or byte data (like images). If you want to store a dict, you'll have to encode it somehow. Alternatively you can use the JSON extension which extends the single file extension. |
Gotcha. That makes sense, though it wasn't obvious to me. Here are a few places that I think this should be fixed: The documentation makes it sound like the difference is just whether it creates one or multiple files:
It should probably throw a more informative error message than "'str' object cannot be interpreted as an integer" And |
The latter is python just doing an automatic coercion. We don't have any sort of type checking or validation of the value being snapshotted. I can definitely see a use case for some sort of extension specific validation rules. Could likely solve the more detailed error message at the same time. Regarding documentation, are you interested in putting up a pull request? |
After sleeping on it, I still think it's pretty unintuitive that " I think the automatic coercion is inappropriate. The def serialize(self,data):
if this._write_mode == WriteMode.TEXT:
if not isinstance(data, str): raise TypeError(...)
return data
else:
return bytes(memoryview(data)) |
It depends.
Doing a straight |
It'd be fairly straightforward to add a version of the amber serializer that generates a file per test case. You can write a custom extension for this. In fact, I'd welcome that extension to be contributed upstream to syrupy itself. I acknowledge that the name can be a bit misleading considering the default serializer is the amber serializer. I'd consider the single file extension to be a base serializer used to create other serializers. Changing this behaviour (beyond just improving the error message) would be a breaking change which I'd prefer not to make in this case. For improving the error message and documentation, contributions are welcome. |
I would like to contribute to this, as it helps our issue to be solved as well: #837 |
Describe the bug
SingleFileSnapshotExtension
crashes when trying to serialize a Dict with a number value.To reproduce
run
pytest --snapshot-update
on a file containing the below test:Expected behavior
The test should generate a snapshot file without errors.
Screenshots
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: