Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short-cut for the factory function (Feature request) #60

Open
mnieber opened this issue Jan 11, 2023 · 2 comments
Open

Short-cut for the factory function (Feature request) #60

mnieber opened this issue Jan 11, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@mnieber
Copy link

mnieber commented Jan 11, 2023

First of all, great work!

I wondered if the verbosity of the code could be further reduced by allowing the factory function to be used without arguments. In that case, the argument to factory should be derived from the type annotation. E.g.

@dataclass(kw_only=True)
class GenerateCodeFn(Entity):
    post_process_source_code_fn: PostProcessSourceCodeFn = factory

Of course, this is only a minor improvement, but still potentially a useful one.

By the way, it seems that the following alternative has a drawback (for me), since vscode fails to detect the type of post_process_source_code_fn and therefore symbol lookup doesn't work.

@dataclass(kw_only=True)
class GenerateCodeFn(Entity):
    post_process_source_code_fn = factory(PostProcessSourceCodeFn)
@biqqles
Copy link
Owner

biqqles commented Jan 15, 2023

Although I can see that the repetition is not that satisfying, I think this feature would add a lot of complexity and possibly fragility to dataclassy itself for a marginal reduction in verbosity (factories are not incredibly common, I think). Right now Factory (the implementing class) and factory have no knowledge whatsoever of data classes or the __annotations__ dict and I like that elegance.

Also note that the type hint and argument to factory are not necessarily redundant. For example, imagine the case where PostProcessSourceCodeFn has various subclasses. All are valid but one must be used as the default. In this case you could easily have post_process_source_code_fn: PostProcessSourceCodeFn = factory(PostProcessSourceCodeFnSubclass). You can see that sometimes both type hint and argument would be needed, which means you need to have two ways factory can be used, which adds complexity to the user.

@biqqles biqqles added the enhancement New feature or request label Jan 15, 2023
@mnieber
Copy link
Author

mnieber commented Jan 15, 2023

  1. I totally agree that type hint and argument to factory are not necessarily redundant. I already have use-cases where they are different.
  2. I think that this use of factory could be enabled without complicating the factory function itself. I suppose you do some introspection of the class that was annotated with dataclass? During this introspection, we could replace field: SomeType = factory with field: SomeType = factory(SomeType). In other words, the complexity would be added to the introspection, not to factory.

That said, it's only a minor improvement, I'm already very happy with the existing solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants