Skip to content

Postprocessing wraps defaults during postprocessing  #311

Open

Description

@mezuzza

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.

Activity

  1. lebrice commented on Apr 30, 2024

    @lebrice
    Owner

    Hi @mezuzza, thanks for posting!

    Interesting bug you found there! I'll take a look at whats going on.

  2. mezuzza commented on Apr 30, 2024

    @mezuzza
    Author

    A quick thing to help:
    I noticed that the actual extra wrapping happens in postprocessing. I understand the code path, but don't have enough context to figure out why it's taking the code path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions