Kwpolska / merge_args

Merge signatures of two functions with Advanced Hackery.
BSD 3-Clause "New" or "Revised" License
31 stars 6 forks source link

Support class constructors #13

Open snorkysnark opened 10 months ago

snorkysnark commented 10 months ago

Since types are also callable functions that return the type instance, I'd like for the following merge to be possible

class Foo:
    def __init__(self, bb: str) -> None:
        pass

@merge_args(Foo)
def run_cmd(aa: str, *args, **kwargs) -> Foo:
    return Foo(*args, **kwargs)

help(run_cmd)

Expected:

run_cmd(aa: str, bb: str) -> __main__.Foo

Error:

Traceback (most recent call last):
  File "/home/lisk/Code/ralsei/ralsei/console/./app2.py", line 9, in <module>
    @merge_args(Foo)
     ^^^^^^^^^^^^^^^
  File "/home/lisk/Code/ralsei/ralsei/__pypackages__/3.11/lib/merge_args.py", line 116, in _merge
    'co_flags': source.__code__.co_flags,
                ^^^^^^^^^^^^^^^
AttributeError: type object 'Foo' has no attribute '__code__'.