Open dabdine opened 3 months ago
That sounds neat. To clarify though, is there a specific ask here? I've not used Pydantic at all and I've only just recently started using type annotations in my Python code but have yet to start using them in this project in particular.
The ask is to natively support Pydantic by adding the pydantic schema methods (__get_pydantic_core_schema__
, __get_pydantic_json_schema__
) to the Rule class, just throwing out thoughts that if that's implemented it's probably best to do it in a separate module so there isn't a dependency on Pydantic.
That sounds cool. I like the idea of offering optional integrations with other popular libraries. I'm planning on doing something similar with SQLAlchemy eventually so I can get type info for my ORM models.
@zeroSteiner Thanks for creating this lib and found how easy to write rule are in english. This might be slightly related to this PR, however I am looking to understand from regular user of Pydantic Classes:
I trying to write type_resolver
for my Pydantic class which has fields that of types str, int, Enum, Nested-Pydantic such as:
class Person(BaseModel):
name : str
gender : GenderEnum
dob : datetime
licence:LicenceModel
class LicenceModel(BaseModel):
lic_number : int
lic_type : LicTypeEnum
so, how can I create context
for rule-engine for datatype Person.
context = rule_engine.Context(
resolver = rule_engine.resolve_attribute,
type_resolver = rule_engine.type_resolver_from_dict({
'name': rule_engine.DataType.STRING,
'gender': ??,
'licence': ??,
'dob': rule_engine.DataType.DATETIME
})
1.1 I tried to set type { "licence.lic_number" : rule_engine.DataType. FLOAT }
however, compiling Rule('person.lic_number == 123') failed with error AttributeResolutionError
1.2 What type can be set for gender
and licence
? If it not possible in current version, would it be the case that I should convert the Pydantic Model into purely dict and then determine the type of the gender and licence.
Unfortunately, there are two things that are unsupported by what you're trying to do.
OBJECT
data type for defining typed compound data. The existing compound types, e.g. ARRAY, MAPPING, and SET all require their member types to be the same. There isn't a ticket for this, but I am planning on implementing it in the next release, which will be v4.6. At that point, you'd at least be able to do this, albeit with a bit more effort because of the lack of Pydantic support. It'd likely involve defining a type like license_t = DataType.OBJECT('License', {'lic_number': DataType.FLOAT, 'lic_type': DataType.STRING})
and then person_t = DataType.OBJECT('Person', {'license': license_t, ...
. That's the plan anyways.
Hola! Working with some rules now that I'm loading from markdown front-matter. To parse / validate the front-matter I'm using Pydantic (v2). During parsing, I wanted to natively transform a string field containing a
rule-engine
rule to aRule
class object. Here's the approach I took:The
PydanticRule
takes a generic type parameter that is used to define the schema supplied toContext
whenRule
is instantiated. This allows the benefit of syntax/symbol error detection when the rule is compiled (read into the pydantic model) instead of at runtime.I think it's a good idea to leave pydantic out of this lib (unless folks really need it). However, it may make sense to create a separate lib that contains the types so rule-engine can be used this way.
Also, we'd probably want to spend more time on the pydantic/python -> rule-engine type conversion. I haven't fully tested that yet.