File: plugin.md

package info (click to toggle)
python-inline-snapshot 0.32.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,900 kB
  • sloc: python: 11,339; makefile: 40; sh: 36
file content (377 lines) | stat: -rw-r--r-- 13,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377


inline-snapshot provides a plugin architecture based on [pluggy](https://pluggy.readthedocs.io/en/latest/index.html) that can be used to extend and customize it.

## Overview

Plugins allow you to customize how inline-snapshot generates code for your snapshots. The primary use case is implementing custom representation logic through the `@customize` hook, which controls how Python objects are converted into source code.

### When to Use Plugins

You should consider creating a plugin when:

- You find yourself manually editing snapshots after they are generated
- You want to use custom constructors or factory methods in your snapshots
- You need to reference local/global variables instead of hardcoding values
- You want to store certain values in external files based on specific criteria
- You need special code representations for your custom types

### Plugin Capabilities

Plugins can:

- **Customize code generation**: Control how objects appear in snapshot code (e.g., use `Color.RED` instead of `Color(255, 0, 0)`)
- **Reference variables**: Use existing local or global variables in snapshots instead of literals
- **External storage**: Automatically store large or sensitive values in external files
- **Import management**: Automatically add necessary import statements to test files

## Plugin Discovery

inline-snapshot loads plugins at the beginning of the session.
It searches for plugins in:

* installed packages with an `inline-snapshot` entry point
* your pytest `conftest.py` files

### Loading Plugins from conftest.py

Loading plugins from `conftest.py` files is the recommended way when you want to change the behavior of inline-snapshot in your own project.

Simply use `@customize` on functions directly in your `conftest.py`:

``` python
from inline_snapshot.plugin import customize


@customize
def my_handler(value, builder):
    # your logic
    pass
```

All customizations defined in your `conftest.py` are active globally for all your tests.

### Creating a Plugin Package

To distribute inline-snapshot plugins as a package, register your plugin class using the `inline-snapshot` entry point in your `setup.py` or `pyproject.toml`:

=== "pyproject.toml (recommended)"
    ``` toml
    [project.entry-points.inline_snapshot]
    my_plugin = "my_package.plugin:MyInlineSnapshotPlugin"
    ```

=== "setup.py"
    ``` python
    setup(
        name="my-inline-snapshot-plugin",
        entry_points={
            "inline_snapshot": [
                "my_plugin = my_package.plugin",
            ],
        },
    )
    ```

Your plugin class should contain methods decorated with `@customize`, just like in conftest.py:

``` python title="my_package/plugin.py"
from inline_snapshot.plugin import customize, Builder


@customize
def my_custom_handler(value, builder: Builder):
    # Your customization logic here
    if isinstance(value, YourCustomType):
        return builder.create_call(YourCustomType, [value.arg])
```

Once installed, the plugin will be automatically loaded by inline-snapshot.

### Plugin Specification

::: inline_snapshot.plugin
    options:
      heading_level: 3
      members: [InlineSnapshotPluginSpec]
      show_root_heading: false
      show_bases: false
      show_source: false



## Customize Examples

The following examples demonstrate common use cases for the `@customize` hook. Each example shows how to implement custom representation logic for different scenarios.

The [customize][inline_snapshot.plugin.InlineSnapshotPluginSpec.customize] hook controls how inline-snapshot generates your snapshots.
You should use it when you find yourself manually editing snapshots after they were created by inline-snapshot.


### Custom constructor methods
One use case might be that you have a dataclass with a special constructor function that can be used for specific instances of this dataclass, and you want inline-snapshot to use this constructor when possible.

<!-- inline-snapshot-lib-set: rect.py -->
``` python title="rect.py"
from dataclasses import dataclass


@dataclass
class Rect:
    width: int
    height: int

    @staticmethod
    def make_square(size):
        return Rect(size, size)
```

You can define a hook in your `conftest.py` that checks if your value is a square and calls the correct constructor function.

<!-- inline-snapshot-lib-set: conftest.py -->
``` python title="conftest.py"
from rect import Rect
from inline_snapshot.plugin import customize
from inline_snapshot.plugin import Builder


@customize
def square_handler(value, builder: Builder):
    if isinstance(value, Rect) and value.width == value.height:
        return builder.create_call(Rect.make_square, [value.width])
```

This allows you to influence the code that is created by inline-snapshot.

<!-- inline-snapshot: create fix first_block outcome-passed=1 -->
``` python title="test_square.py"
from rect import Rect
from inline_snapshot import snapshot


def test_square():
    assert Rect.make_square(5) == snapshot(Rect.make_square(5))  # (1)!
    assert Rect(1, 1) == snapshot(Rect.make_square(1))  # (2)!
    assert Rect(1, 2) == snapshot(Rect(width=1, height=2))  # (3)!
    assert [Rect(3, 3), Rect(4, 5)] == snapshot(
        [Rect.make_square(3), Rect(width=4, height=5)]
    )  # (4)!
```

1. Your handler is used because you created a square
2. Your handler is used because you created a Rect that happens to have the same width and height
3. Your handler is not used because width and height are different
4. The handler is applied recursively to each Rect inside the list - the first is converted to `make_square()` while the second uses the regular constructor

### dirty-equal expressions
It can also be used to instruct inline-snapshot to use specific dirty-equals expressions for specific values.

<!-- inline-snapshot-lib-set: conftest.py -->
``` python title="conftest.py"
from inline_snapshot.plugin import customize
from inline_snapshot.plugin import Builder
from dirty_equals import IsNow


@customize
def is_now_handler(value):
    if value == IsNow():
        return IsNow
```

As explained in the [customize hook specification][inline_snapshot.plugin.
InlineSnapshotPluginSpec.customize], you can return types other than Custom objects.
inline-snapshot includes a built-in handler in its default plugin that converts dirty-equals expressions back into source code, which is why you can return `IsNow` directly without using the builder.
This approach is much simpler than using `builder.create_call()` for complex dirty-equals expressions.

<!-- inline-snapshot: create fix first_block outcome-passed=1 -->
``` python title="test_is_now.py"
from datetime import datetime
from dirty_equals import IsNow  # (1)!
from inline_snapshot import snapshot


def test_is_now():
    assert datetime.now() == snapshot(IsNow)
```

1. inline-snapshot also creates the imports when they are missing

!!! important
    inline-snapshot will never change the dirty-equals expressions in your code because they are [unmanaged](eq_snapshot.md#unmanaged-snapshot-values).
    Using `@customize` with dirty-equals is a one-way ticket. Once the code is created, inline-snapshot does not know if it was created by inline-snapshot itself or by the user and will not change it when you change the `@customize` implementation, because it has to assume that it was created by the user.


### Conditional external objects

`create_external` can be used to store values in external files if a specific criterion is met.

<!-- inline-snapshot-lib-set: conftest.py -->
``` python title="conftest.py"
from inline_snapshot.plugin import customize
from inline_snapshot.plugin import Builder


@customize
def long_string_handler(value, builder: Builder):
    if isinstance(value, str) and value.count("\n") > 5:
        return builder.create_external(value)
```

<!-- inline-snapshot: create fix first_block outcome-passed=1 outcome-errors=1 -->
``` python title="test_long_strings.py"
from inline_snapshot import external, snapshot


def test_long_strings():
    assert "a\nb\nc" == snapshot(
        """\
a
b
c\
"""
    )
    assert "a\n" * 50 == snapshot(
        external("uuid:e3e70682-c209-4cac-a29f-6fbed82c07cd.txt")
    )
```

### Reusing local variables

There are times when your local or global variables become part of your snapshots, like UUIDs or user names.
Customize hooks accept `local_vars` and `global_vars` as arguments that can be used to generate the code.

<!-- inline-snapshot-lib-set: conftest.py -->
``` python title="conftest.py"
from inline_snapshot.plugin import customize
from inline_snapshot.plugin import Builder


@customize
def local_var_handler(value, builder, local_vars):
    for var_name, var_value in local_vars.items():
        if var_name.startswith("v_") and var_value == value:
            return builder.create_code(var_name)
```

We check all local variables to see if they match our naming convention and are equal to the value that is part of our snapshot, and return the local variable if we find one that fits the criteria.


<!-- inline-snapshot: create fix first_block outcome-passed=1 -->
``` python title="test_user.py"
from inline_snapshot import snapshot


def get_data(user):
    return {"user": user, "age": 55}


def test_user():
    v_user = "Bob"
    some_number = 50 + 5

    assert get_data(v_user) == snapshot({"user": v_user, "age": 55})
```

inline-snapshot uses `v_user` because it met the criteria in your customization hook, but not `some_number` because it does not start with `v_`.
You can also do this only for specific types of objects or for a whitelist of variable names.
It is up to you to set the rules that work best in your project.

!!! note
    It is not recommended to check only for the value because this might result in local variables which become part of the snapshot just because they are equal to the value and not because they should be there (see `age=55` in the example above).
    This is also the reason why inline-snapshot does not provide default customizations that check your local variables.
    The rules are project-specific and what might work well for one project can cause problems for others.

### Creating special code

Let's say that you have an array of secrets which are used in your code.

<!-- inline-snapshot-lib: my_secrets.py -->
``` python title="my_secrets.py"
secrets = ["some_secret", "some_other_secret"]
```

<!-- inline-snapshot-lib: get_data.py -->
``` python title="get_data.py"
from my_secrets import secrets


def get_data():
    return {"data": "large data block", "used_secret": secrets[1]}
```

The problem is that `--inline-snapshot=create` puts your secret into your test.

<!-- inline-snapshot: create first_block outcome-passed=1 -->
``` python
from get_data import get_data
from inline_snapshot import snapshot


def test_my_class():
    assert get_data() == snapshot(
        {"data": "large data block", "used_secret": "some_other_secret"}
    )
```

Maybe this is not what you want because the secret is a different one in CI or for every test run or the raw value leads to unreadable tests.
What you can do now, instead of replacing `"some_other_secret"` with `secrets[1]` by hand, is to tell inline-snapshot in your *conftest.py* how it should generate this code.

<!-- inline-snapshot-lib-set: conftest.py -->
``` python title="conftest.py"
from my_secrets import secrets
from inline_snapshot.plugin import customize, Builder, ImportFrom


@customize
def secret_handler(value, builder: Builder):
    for i, secret in enumerate(secrets):
        if value == secret:
            return builder.create_code(
                f"secrets[{i}]",
                imports=[ImportFrom("my_secrets", "secrets")],
            )
```

The [`create_code()`][inline_snapshot.plugin.Builder.create_code] method takes the desired code representation. The `imports` parameter adds the necessary import statements.

inline-snapshot will now create the correct code and import statement when you run your tests with `--inline-snapshot=update`.

<!-- inline-snapshot: update outcome-passed=1 -->
``` python hl_lines="4 5 9"
from get_data import get_data
from inline_snapshot import snapshot

from my_secrets import secrets


def test_my_class():
    assert get_data() == snapshot(
        {"data": "large data block", "used_secret": secrets[1]}
    )
```

!!! question "why update?"
    `"some_other_secret"` was already a correct value for your assertion and `--inline-snapshot=fix` only changes code when the current value is not correct and needs to be fixed. `update` is the category for all other changes where inline-snapshot wants to generate different code which represents the same value as the code before.

    You only have to use `update` when you changed your customizations and want to use the new code representations in your existing tests. The new representation is also used by `create` or `fix` when you write new tests.

    The `update` category is not enabled by default for `--inline-snapshot=review/report`.
    You can read [here](categories.md#update) more about it.









## Reference
::: inline_snapshot.plugin
    options:
      heading_level: 3
      members: [hookimpl,customize,Builder,Custom,Import,ImportFrom]
      show_root_heading: false
      show_bases: false
      show_source: false