Skip to content
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

declare abbreviations to teach inline-snapshot what it should generate #177

Open
15r10nk opened this issue Jan 12, 2025 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@15r10nk
Copy link
Owner

15r10nk commented Jan 12, 2025

inline-snapshot should ideally create snapshots that do not need to be modified by the developer. However, there are cases where the developer may want to use specific looking snapshots.

declare_abbreviation is intended to solve this problem by allowing the developer to specify what the snapshots should look like, and allow inline-snapshot to generate the correct code in the first place.

The idea is as follows:

  • inline-snapshot checks each snapshot value and inner value (dict/list/... child) to see if it can be abbreviated.
  • The abbreviation with the shortest representation is used if there are several ways to abbreviate a snapshot.

Below are some examples of what this could look like:

from inline_snapshot import snapshot, declare_abbreviation, Abbreviation
from dirty_equals import IsNow

from datetime import datetime, timezone


@declare_abbreviation
def _(value, context):
    if value == IsNow(tz=timezone.utc):
        return Abbreviation(IsNow, tz=timezone.utc)


def repeated(num, v):
    return [v] * num


@declare_abbreviation
def _(value, context):
    if value == [value[0]] * len(value):
        return Abbreviation(repeated, len(value), value[0])


def test():
    # the snapshots which will be generated by inline-snapshot

    assert [0,0,0,0,0] == snapshot(repeated(5, 0))
    assert datetime.now() == snapshot(IsNow(tz=timezone.utc))

context should give the developer some information about the context of the value (what the parent object is or what the key is if the value is inside a dictionary)

These abbreviations would be declared globally (in conftest.py).

This is currently just an idea and feedback is very welcome.

@samuelcolvin
Copy link

Thanks so much for addressing this, I wondered if such a thing would be possible but hadn't gotten around to creating an issue. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants