Skip to content

Commit

Permalink
fix: fixed pydantic integration and HasRepr
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed May 24, 2024
1 parent ed732eb commit b43dd38
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 18 deletions.
7 changes: 4 additions & 3 deletions inline_snapshot/_code_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _(obj: MyCustomClass):


def code_repr(obj):
with mock.patch("builtins.repr", code_repr_dispatch):
with mock.patch("builtins.repr", code_repr):
result = code_repr_dispatch(obj)

try:
Expand Down Expand Up @@ -126,7 +126,7 @@ def _(v: IsDataclass):

try:
from pydantic import BaseModel
except ImportError:
except ImportError: # pragma: no cover
pass
else:

Expand All @@ -136,7 +136,8 @@ def _(model: BaseModel):
type(model).__qualname__
+ "("
+ ", ".join(
e + "=" + repr(getattr(model, e)) for e in model.__pydantic_fields_set__
e + "=" + repr(getattr(model, e))
for e in sorted(model.__pydantic_fields_set__)
)
+ ")"
)
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test(session):
"time-machine",
"mypy",
"pyright",
"pydantic",
)

cmd = []
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import os
import platform
import re
Expand All @@ -18,6 +19,7 @@
import inline_snapshot._external
from .utils import snapshot_env
from inline_snapshot import _inline_snapshot
from inline_snapshot import register_repr
from inline_snapshot._format import format_code
from inline_snapshot._inline_snapshot import Flags
from inline_snapshot._rewrite_code import ChangeRecorder
Expand All @@ -30,6 +32,16 @@
black.files.find_project_root = black.files.find_project_root.__wrapped__ # type: ignore


@register_repr
def _(v: executing.Source):
return f"<source {Path(v.filename).name}>"


@register_repr
def _(v: ast.AST):
return repr(ast.dump(v))


@pytest.fixture()
def check_update(source):
def w(source_code, *, flags="", reported_flags=None, number=1):
Expand Down
19 changes: 9 additions & 10 deletions tests/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from tempfile import TemporaryDirectory
from typing import Any

import black

import inline_snapshot._external
from .utils import snapshot_env
from inline_snapshot import _inline_snapshot
Expand All @@ -19,10 +17,6 @@
pytest_plugins = "pytester"


if hasattr(black.files.find_project_root, "__wrapped__"):
black.files.find_project_root = black.files.find_project_root.__wrapped__ # type: ignore


ansi_escape = re.compile(
r"""
\x1B # ESC
Expand Down Expand Up @@ -54,7 +48,7 @@ def read_files(self, dir: Path):
return {p.name: p.read_text() for p in dir.iterdir() if p.is_file()}

def run_inline(
self, *flags, changes=None, reported_flags=None, files=None
self, *flags, changes=None, reported_flags=None, changed_files=None
) -> Example:

with TemporaryDirectory() as dir:
Expand Down Expand Up @@ -102,12 +96,17 @@ def run_inline(
assert sorted(snapshot_flags) == reported_flags

if changes is not None:
assert [repr(c) for c in all_changes] == changes
assert [c for c in all_changes] == changes

recorder.fix_all()

if files is not None:
assert files == self.read_files(tmp_path)
if changed_files is not None:
current_files = {}

for name, content in sorted(self.read_files(tmp_path).items()):
if name not in self.files or self.files[name] != content:
current_files[name] = content
assert changed_files == current_files

return Example(self.read_files(tmp_path))

Expand Down
36 changes: 35 additions & 1 deletion tests/test_code_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,40 @@ class container:
)


def test_dataclass_field_repr(check_update):

Example(
"""\
from inline_snapshot import snapshot
from dataclasses import dataclass,field
@dataclass
class container:
a: int
b: int = field(default=5,repr=False)
assert container(a=1,b=5) == snapshot()
"""
).run_inline(
"create",
changed_files=snapshot(
{
"test_something.py": """\
from inline_snapshot import snapshot
from dataclasses import dataclass,field
@dataclass
class container:
a: int
b: int = field(default=5,repr=False)
assert container(a=1,b=5) == snapshot(container(a=1))
"""
}
),
).run_inline()


def test_flag(check_update):

assert (
Expand Down Expand Up @@ -196,7 +230,7 @@ class Color(Enum):
"""
).run_inline(
"create",
files=snapshot(
changed_files=snapshot(
{
"test_something.py": """\
from enum import Enum
Expand Down
38 changes: 34 additions & 4 deletions tests/test_example.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
from executing import Source

from .example import Example
from inline_snapshot import HasRepr
from inline_snapshot import snapshot
from inline_snapshot._change import Replace


def test_diff_multiple_files():
def test_example():

Example(
"""
e = Example(
{
"test_a.py": """
from inline_snapshot import snapshot
def test_a():
assert 1==snapshot(2)
""",
).run_pytest(
"test_b.py": "1+1",
},
)

e.run_pytest(
"--inline-snapshot=create,fix",
)

e.run_inline(
"fix",
reported_flags=snapshot(["fix"]),
changes=snapshot(
[
Replace(
flag="fix",
source=HasRepr(Source, "<source test_a.py>"),
node="Constant(value=2)",
new_code="1",
old_value=2,
new_value=1,
)
]
),
).run_inline(
"fix",
changed_files=snapshot({}),
)
42 changes: 42 additions & 0 deletions tests/test_pydantic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from .example import Example
from inline_snapshot import snapshot


def test_pydantic_repr():

Example(
"""
from pydantic import BaseModel
from inline_snapshot import snapshot
class M(BaseModel):
size:int
name:str
age:int=4
def test_pydantic():
assert M(size=5,name="Tom")==snapshot()
"""
).run_inline(
"create",
changed_files=snapshot(
{
"test_something.py": """\
from pydantic import BaseModel
from inline_snapshot import snapshot
class M(BaseModel):
size:int
name:str
age:int=4
def test_pydantic():
assert M(size=5,name="Tom")==snapshot(M(name="Tom", size=5))
\
"""
}
),
).run_inline()

0 comments on commit b43dd38

Please sign in to comment.