Tinche / incant

https://incant.threeofwands.com
Apache License 2.0
65 stars 3 forks source link

UnboundLocalError when reusing dependencies #4

Closed sscherfke closed 1 year ago

sscherfke commented 1 year ago

Incant fails when the invoked function uses a dependency, that is already used by another dependency:

import incant

incanter = incant.Incanter()

@incanter.register_by_name
def x() -> int:
    return 1

@incanter.register_by_name
def y(x: int) -> int:
    return x + 1

def func(x: int, y: int) -> None:
    assert x + 1 == y

incanter.invoke(func)
Traceback (most recent call last):
  File "/home/stefan/.../lib/tasks.py", line 28, in <module>
    incanter.invoke(func)
  File "/home/stefan/emsconda/lib/python3.10/site-packages/incant/__init__.py", line 112, in invoke
    return self.prepare(fn, is_async=False)(*args, **kwargs)
  File "<incant generated invoke of func>", line 2, in invoke_func
UnboundLocalError: local variable '_incant_local_1' referenced before assignment

It does not matter whether I use register_by_name or register_by_type.

Python is 3.10, incant is 22.1.0.

Tinche commented 1 year ago

Interesting, will take a look!

Tinche commented 1 year ago

This should be fixed on main. I plan on doing a little more polish and releasing in the next few days. Mind retesting against main until then?

I've added your case to the test suite.

sscherfke commented 1 year ago

Thanks for the quick fix :)

sscherfke commented 1 year ago

One more question: Will x() only be called once and the same result used for both, y() and func()? Like with pytest fixtures?

Tinche commented 1 year ago

Only once! The same result (the same local variable, in fact) will be passed to all dependents.

Tinche commented 1 year ago

Fix released