SimpleParsing icon indicating copy to clipboard operation
SimpleParsing copied to clipboard

Postprocessing wraps defaults during postprocessing

Open mezuzza opened this issue 10 months ago • 2 comments

Describe the bug The raw type sometimes gets passed into it's own constructor. This is fine for many classes which automatically create a new instance with the same values, but for classes which can be nested, this is problematic behavior.

To Reproduce

from dataclasses import dataclass, field

import simple_parsing


class Foo:
    def __init__(self, value: str) -> None:
        self.value = value

    def __str__(self) -> str:
        return f"Foo({str(self.value)})"


@dataclass
class Config:
    foo: Foo = field(default_factory=lambda: Foo("foo"))
    bar: Foo | None = field(default_factory=lambda: Foo("bar"))
    baz: Foo = field(default=Foo("baz"))
    baaz: Foo = Foo("baaz")


c = simple_parsing.parse(Config)

print(c.foo) # Foo(Foo("foo"))
print(c.bar) # Foo("bar")
print(c.baz) # Foo(Foo("baz"))
print(c.baaz) # Foo(Foo("baaz"))

Expected behavior None of the calls above should return a wrapped value.

print(c.foo) # Foo("foo")
print(c.bar) # Foo("bar")
print(c.baz) # Foo("baz")
print(c.baaz) # Foo("baaz")

Actual behavior A clear and concise description of what is happening.

print(c.foo) # Foo(Foo("foo"))
print(c.bar) # Foo("bar")
print(c.baz) # Foo(Foo("baz"))
print(c.baaz) # Foo(Foo("baaz"))

Desktop (please complete the following information):

  • Version 0.1.5
  • Python version: 3.12.2

Additional context Add any other context about the problem here.

mezuzza avatar Apr 27 '24 16:04 mezuzza