Fix callseq deduplication error, allow using Some|None=None args with no commutativity error, add ignore_basictypes_return for a ConversionPoint.from_fn

This commit is contained in:
2025-10-17 00:51:29 +03:00
parent dbecef1977
commit 74d78b1957
6 changed files with 53 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
[project]
name = "breakshaft"
version = "0.1.6.post4"
version = "0.1.6.post5"
description = "Library for in-time codegen for type conversion"
authors = [
{ name = "nikto_b", email = "niktob560@yandex.ru" }

View File

@@ -90,7 +90,7 @@ class GraphWalker:
if subg is not None:
variant_subgraphs.add(subg)
consumed = frozenset(point.requires) & from_types
consumed = (frozenset(point.requires) | frozenset(point.opt_args)) & from_types
variant = CallgraphVariant(point, frozenset(variant_subgraphs), consumed)
head = head.add_subgraph_variant(variant)

View File

@@ -56,7 +56,8 @@ class ConversionPoint:
def from_fn(cls,
func: Callable,
rettype: Optional[type] = None,
type_remap: Optional[dict[str, type]] = None) -> list[ConversionPoint]:
type_remap: Optional[dict[str, type]] = None,
ignore_basictype_return: bool = False) -> list[ConversionPoint]:
if type_remap is None:
annot = get_type_hints(func)
else:
@@ -86,7 +87,7 @@ class ConversionPoint:
if any(map(lambda x: fn_rettype_origin is x, cm_out_origins)) and is_context_manager_factory(func):
fn_rettype = get_args(fn_rettype)[0]
if is_basic_type_annot(rettype):
if not ignore_basictype_return and is_basic_type_annot(rettype):
return []
ret = []
@@ -96,7 +97,10 @@ class ConversionPoint:
if len(tuple_unwrapped) > 0 and Ellipsis not in tuple_unwrapped:
for t in tuple_unwrapped:
if not is_basic_type_annot(t):
ret += ConversionPoint.from_fn(func, rettype=t, type_remap=type_remap)
ret += ConversionPoint.from_fn(func,
rettype=t,
type_remap=type_remap,
ignore_basictype_return=ignore_basictype_return)
argtypes: list[list[type]] = []
orig_args = extract_func_args(func, type_remap)

View File

@@ -99,7 +99,7 @@ def deduplicate_callseq(conversion_models: list[ConversionRenderData]) -> list[C
argument_changed = False
found_model = False
for m in deduplicated_conv_models:
if not found_model and m == conv_model:
if not found_model and m.funchash == conv_model.funchash:
found_model = True
if found_model and m.inj_hash in argnames:

View File

@@ -47,6 +47,37 @@ def test_default_consumer_args():
assert dep == (123, '1')
def test_optional_default_none_consumer_args():
repo = ConvRepo()
@repo.mark_injector()
def b_to_a(b: B | None = None) -> A:
return A(int(b.b))
@repo.mark_injector()
def a_to_b(a: A) -> B | None:
return B(float(a.a))
@repo.mark_injector()
def int_to_a(i: int) -> A:
return A(i)
def consumer(dep: A, opt_dep: optC = '42') -> tuple[int, str]:
return dep.a, opt_dep
fn1 = repo.get_conversion((B,), consumer, force_commutative=True, force_async=False, allow_async=False)
dep = fn1(B(42.1))
assert dep == (42, '42')
fn2 = repo.get_conversion((int,), consumer, force_commutative=True, force_async=False, allow_async=False)
dep = fn2(123)
assert dep == (123, '42')
fn3 = repo.get_conversion((int, optC), consumer, force_commutative=True, force_async=False, allow_async=False)
dep = fn3(123, '1')
assert dep == (123, '1')
def test_default_inj_args():
repo = ConvRepo()
@@ -79,7 +110,6 @@ def test_default_inj_args():
def test_default_graph_override():
repo = ConvRepo()
@repo.mark_injector()

View File

@@ -17,7 +17,7 @@ type optC = str
def test_default_consumer_args():
repo = ConvRepo()
repo = ConvRepo(store_sources=True)
@repo.mark_injector()
def b_to_a(b: B) -> A:
@@ -106,3 +106,13 @@ def test_pipeline_with_subgraph_duplicates():
assert b_to_a_calls[0] == 1
assert cons1_calls[0] == 5
assert cons2_calls[0] == 4
def convertor(_5891515089754: "<class 'test_pipeline.B'>"):
# <function test_default_consumer_args.<locals>.b_to_a at 0x7f5bb1be02c0>
_5891515089643 = _conv_funcmap[8751987548204](b=_5891515089754)
# <function test_default_consumer_args.<locals>.consumer1 at 0x7f5bb1be0c20>
_8751987542640 = _conv_funcmap[8751987548354](dep=_5891515089643)
# <function test_default_consumer_args.<locals>.consumer2 at 0x7f5bb1be0540>
_8751987537115 = _conv_funcmap[8751987548244](dep=_5891515089643, dep1=_8751987542640)
return _8751987542640