Skip to content

Commit

Permalink
refactor: review
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Jan 15, 2025
1 parent 3323dcf commit 19f29fa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 34 deletions.
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ dependencies = [
[tool.hatch.envs.default]
installer="uv"

[tool.hatch.envs.cov.scripts]
gh=[
"- rm htmlcov/*",
"gh run download -n html-report -D htmlcov",
"xdg-open htmlcov/index.html",
]

[tool.hatch.envs.docs.scripts]
build = "mkdocs build --strict"
deploy = "mkdocs gh-deploy"
Expand Down
14 changes: 5 additions & 9 deletions src/inline_snapshot/_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,20 @@ def eval(self, node):


class Adapter:
# TODO remove context
context: SourceFile

adapter_context: AdapterContext
context: AdapterContext

def __init__(self, context: AdapterContext):
self.adapter_context = context
self.context = context.file
self.context = context

def get_adapter(self, old_value, new_value) -> Adapter:
if type(old_value) is not type(new_value):
from .value_adapter import ValueAdapter

return ValueAdapter(self.adapter_context)
return ValueAdapter(self.context)

adapter_type = get_adapter_type(old_value)
if adapter_type is not None:
return adapter_type(self.adapter_context)
return adapter_type(self.context)
assert False

def assign(self, old_value, old_node, new_value):
Expand All @@ -87,7 +83,7 @@ def assign(self, old_value, old_node, new_value):
def value_assign(self, old_value, old_node, new_value):
from .value_adapter import ValueAdapter

adapter = ValueAdapter(self.adapter_context)
adapter = ValueAdapter(self.context)
result = yield from adapter.assign(old_value, old_node, new_value)
return result

Expand Down
18 changes: 12 additions & 6 deletions src/inline_snapshot/_adapter/dict_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def assign(self, old_value, old_node, new_value):
if key is None:
warnings.warn_explicit(
"star-expressions are not supported inside snapshots",
filename=self.context._source.filename,
filename=self.context.file._source.filename,
lineno=value.lineno,
category=InlineSnapshotSyntaxWarning,
)
Expand All @@ -88,7 +88,7 @@ def assign(self, old_value, old_node, new_value):
):
if not key in new_value:
# delete entries
yield Delete("fix", self.context._source, node, old_value[key])
yield Delete("fix", self.context.file._source, node, old_value[key])

to_insert = []
insert_pos = 0
Expand All @@ -109,12 +109,15 @@ def assign(self, old_value, old_node, new_value):

if to_insert:
new_code = [
(self.context._value_to_code(k), self.context._value_to_code(v))
(
self.context.file._value_to_code(k),
self.context.file._value_to_code(v),
)
for k, v in to_insert
]
yield DictInsert(
"fix",
self.context._source,
self.context.file._source,
old_node,
insert_pos,
new_code,
Expand All @@ -126,12 +129,15 @@ def assign(self, old_value, old_node, new_value):

if to_insert:
new_code = [
(self.context._value_to_code(k), self.context._value_to_code(v))
(
self.context.file._value_to_code(k),
self.context.file._value_to_code(v),
)
for k, v in to_insert
]
yield DictInsert(
"fix",
self.context._source,
self.context.file._source,
old_node,
len(old_value),
new_code,
Expand Down
22 changes: 11 additions & 11 deletions src/inline_snapshot/_adapter/generic_call_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def assign(self, old_value, old_node, new_value):
result = yield from self.value_assign(old_value, old_node, new_value)
return result

call_type = self.adapter_context.eval(old_node.func)
call_type = self.context.eval(old_node.func)

if not (isinstance(call_type, type) and self.check_type(call_type)):
result = yield from self.value_assign(old_value, old_node, new_value)
Expand All @@ -113,7 +113,7 @@ def assign(self, old_value, old_node, new_value):
if isinstance(pos_arg, ast.Starred):
warnings.warn_explicit(
"star-expressions are not supported inside snapshots",
filename=self.context._source.filename,
filename=self.context.file._source.filename,
lineno=pos_arg.lineno,
category=InlineSnapshotSyntaxWarning,
)
Expand All @@ -124,7 +124,7 @@ def assign(self, old_value, old_node, new_value):
if kw.arg is None:
warnings.warn_explicit(
"star-expressions are not supported inside snapshots",
filename=self.context._source.filename,
filename=self.context.file._source.filename,
lineno=kw.value.lineno,
category=InlineSnapshotSyntaxWarning,
)
Expand All @@ -147,7 +147,7 @@ def assign(self, old_value, old_node, new_value):
for arg_pos, node in list(enumerate(old_node.args))[len(new_args) :]:
yield Delete(
"fix",
self.context._source,
self.context.file._source,
node,
self.argument(old_value, arg_pos),
)
Expand All @@ -156,11 +156,11 @@ def assign(self, old_value, old_node, new_value):
for insert_pos, value in list(enumerate(new_args))[len(old_node.args) :]:
yield CallArg(
flag="fix",
file=self.context._source,
file=self.context.file._source,
node=old_node,
arg_pos=insert_pos,
arg_name=None,
new_code=self.context._value_to_code(value.value),
new_code=self.context.file._value_to_code(value.value),
new_value=value.value,
)

Expand All @@ -171,7 +171,7 @@ def assign(self, old_value, old_node, new_value):
# delete entries
yield Delete(
"fix" if missing else "update",
self.context._source,
self.context.file._source,
kw.value,
self.argument(old_value, kw.arg),
)
Expand Down Expand Up @@ -201,11 +201,11 @@ def assign(self, old_value, old_node, new_value):

yield CallArg(
flag="fix",
file=self.context._source,
file=self.context.file._source,
node=old_node,
arg_pos=insert_pos,
arg_name=key,
new_code=self.context._value_to_code(value),
new_code=self.context.file._value_to_code(value),
new_value=value,
)
to_insert = []
Expand All @@ -218,11 +218,11 @@ def assign(self, old_value, old_node, new_value):

yield CallArg(
flag="fix",
file=self.context._source,
file=self.context.file._source,
node=old_node,
arg_pos=insert_pos,
arg_name=key,
new_code=self.context._value_to_code(value),
new_code=self.context.file._value_to_code(value),
new_value=value,
)

Expand Down
11 changes: 7 additions & 4 deletions src/inline_snapshot/_adapter/sequence_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def assign(self, old_value, old_node, new_value):
if isinstance(e, ast.Starred):
warnings.warn_explicit(
"star-expressions are not supported inside snapshots",
filename=self.context.filename,
filename=self.context.file.filename,
lineno=e.lineno,
category=InlineSnapshotSyntaxWarning,
)
Expand All @@ -83,21 +83,24 @@ def assign(self, old_value, old_node, new_value):
old_position += 1
elif c == "i":
new_value_element = next(new)
new_code = self.context._value_to_code(new_value_element)
new_code = self.context.file._value_to_code(new_value_element)
result.append(new_value_element)
to_insert[old_position].append((new_code, new_value_element))
elif c == "d":
old_value_element, old_node_element = next(old)
yield Delete(
"fix", self.context._source, old_node_element, old_value_element
"fix",
self.context.file._source,
old_node_element,
old_value_element,
)
old_position += 1
else:
assert False

for position, code_values in to_insert.items():
yield ListInsert(
"fix", self.context._source, old_node, position, *zip(*code_values)
"fix", self.context.file._source, old_node, position, *zip(*code_values)
)

return self.value_type(result)
Expand Down
8 changes: 4 additions & 4 deletions src/inline_snapshot/_adapter/value_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def assign(self, old_value, old_node, new_value):
if not old_value == new_value:
warnings.warn_explicit(
f"inline-snapshot will be able to fix f-strings in the future.\nThe current string value is:\n {new_value!r}",
filename=self.context._source.filename,
filename=self.context.file._source.filename,
lineno=old_node.lineno,
category=InlineSnapshotInfo,
)
Expand All @@ -50,18 +50,18 @@ def assign(self, old_value, old_node, new_value):
elif (
old_node is not None
and update_allowed(old_value)
and self.context._token_of_node(old_node) != new_token
and self.context.file._token_of_node(old_node) != new_token
):
flag = "update"
else:
# equal and equal repr
return old_value

new_code = self.context._token_to_code(new_token)
new_code = self.context.file._token_to_code(new_token)

yield Replace(
node=old_node,
file=self.context._source,
file=self.context.file._source,
new_code=new_code,
flag=flag,
old_value=old_value,
Expand Down

0 comments on commit 19f29fa

Please sign in to comment.