Code generation
You can use almost any python datatype and also complex values like datatime.date
, because repr()
is used to convert the values to source code.
The default __repr__()
behaviour can be customized.
It might be necessary to import the right modules to match the repr()
output.
from inline_snapshot import snapshot
import datetime
def something():
return {
"name": "hello",
"one number": 5,
"numbers": list(range(10)),
"sets": {1, 2, 15},
"datetime": datetime.date(1, 2, 22),
"complex stuff": 5j + 3,
"bytes": b"byte abc\n\x16",
}
def test_something():
assert something() == snapshot()
from inline_snapshot import snapshot
import datetime
def something():
return {
"name": "hello",
"one number": 5,
"numbers": list(range(10)),
"sets": {1, 2, 15},
"datetime": datetime.date(1, 2, 22),
"complex stuff": 5j + 3,
"bytes": b"byte abc\n\x16",
}
def test_something():
assert something() == snapshot(
{
"name": "hello",
"one number": 5,
"numbers": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"sets": {1, 2, 15},
"datetime": datetime.date(1, 2, 22),
"complex stuff": (3 + 5j),
"bytes": b"byte abc\n\x16",
}
)
The code is generated in the following way:
- The value is copied with
value = copy.deepcopy(value)
and it is checked if the copied value is equal to the original value. - The code is generated with
repr(value)
(which can be customized) -
Strings which contain newlines are converted to triple quoted strings.
-
The code is formatted with black.
-
The whole file is formatted with black if it was formatted before.
Note
The black formatting of the whole file could not work for the following reasons:
- black is configured with cli arguments and not in a configuration file.
Solution: configure black in a configuration file - inline-snapshot uses a different black version.
Solution: specify which black version inline-snapshot should use by adding black with a specific version to your dependencies.
- black is configured with cli arguments and not in a configuration file.