Open Kamforka opened 1 year ago
Yeah that sounds useful for sure. A couple things come to mind though.
DataClass.from_type
and DataClass.from_value
. I'd really like to keep type of type mapping logic consolidated for maintenance in the future.type_resolver_from_dict
though.This is cool stuff for sure, thanks for sharing.
Thanks for the feedback @zeroSteiner , glad you find it useful!
Let me just reply to your points one by one:
So what do you think, should I follow up with a PR?
Created a sample implementation in #56 feel free to check it out, I ran the tests locally and they seem to work fine, also added a guard to tests the modern type hinting syntax (list[str], dict[str, str], etc.
) only from python v3.9 and above.
Closing this since I'm pretty sure it was completed when I merged #56 and I just forgot about this ticket.
Thanks @zeroSteiner for this amazing library, I am exploring it for a particular usecase.
The type hinting feature is really important for my usecase and I want to detect the issues at compile time. My input is a python dataclass. Is it possible to build a general resolver like type_resolver_from_dict
which can work on the dataclass recursively, at compile time. .. @Kamforka do you have any idea here?
I think it should work based on my notes in this PR. If you have an example of what you're trying to do that's failing, I can look into it.
class Type(str, Enum):
A = "A"
@dataclass
class Name:
name: str
dct: dict[str, str]
typ: Type = Type.A
@dataclass
class User:
name: Name
friends: list[Name]
email: str
context = rule_engine.Context(
resolver=rule_engine.resolve_attribute,
)
rule = rule_engine.Rule('friends.bb == "Vader"', context=context)
# Want this to generate SymbolResolutionError
# but its passing
I wold want to do something like
context = rule_engine.Context(
resolver=rule_engine.resolve_attribute,
type_resolver= type_resolver_for_dataclass(User)
)
rule = rule_engine.Rule('friends.bb == "Vader"', context=context)
@akr31 I think the larger issue is that there isn't a way to define an object with attributes in Rule Engine. I've been meaning to add it recently because I've had my own use case that's very similar to what you're describing. After looking into this a bit more I don't think I ever fully added type resolvers for dataclasses. It looks like the PR I referenced only added support to DataType.from_type
to accept type annotations (e.g. typing.Dict[str, str]
) and not full-on dataclass instances.
I'd like to get this working but it might take me a while. I've reopened the ticket so I don't forget. I need to finish my current goal of adding a proper BYTES
datatype. After that, I'll look at OBJECT
which will be a pre-requisite for this work.
I was wondering if the project could benefit from an automatic type resolver extraction feature.
I have an example implementation that I created for my own use-case, but I found it quite generic, and I believe it might make sense to add it to the core library.
The type resolver implementation looks like this:
And to test it one can do:
It should produce a type resolver like the below:
Please tell me if you think it makes sense to add it to the lib and I can work it out more.