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

Useless reference_cls in ReferenceField #349

Open
suconakh opened this issue May 2, 2021 · 1 comment
Open

Useless reference_cls in ReferenceField #349

suconakh opened this issue May 2, 2021 · 1 comment

Comments

@suconakh
Copy link

suconakh commented May 2, 2021

I would like to implement my own Reference class with a custom fetch method and i've noticed that ReferenceField has a reference_cls argument. I have tried to use it with my own Reference inheriting from MotorAsyncIOReference, but it didn't work well. By looking at the ReferenceField sources i noticed that the field itself actually uses a new reference_cls. I've also looked at umongo.frameworks.motor_asyncio and found out that MotorAsyncIOReference is hardcoded in MotorAsyncIOBuilder._patch_field. Is there a reason for that?

if isinstance(field, ReferenceField):
field.io_validate.append(_reference_io_validate)
field.reference_cls = MotorAsyncIOReference

I am not familiar with the code and the library development in general but could this be fixed by doing something like this?

if isinstance(field, ReferenceField): 
    field.io_validate.append(_reference_io_validate) 
    if isinstance(field.reference_cls, umongo.data_objects.Reference):
        field.reference_cls = MotorAsyncIOReference

Or perhaps we can get rid of umongo.data_objects.Reference as a default value for reference_cls in ReferenceField constructor and only set default framework reference if user did not set his own.

# umongo.fields
class ReferenceField(BaseField, ma_bonus_fields.Reference):
    def __init__(self, document, *args, reference_cls=None, **kwargs):
        ...

# umongo.frameworks.motor_asyncio
if isinstance(field, ReferenceField):
    field.io_validate.append(_reference_io_validate)
    if field.reference_cls is not None:
        # Additional check for compatibility?
        if not issubclass(field.reference_cls, MotorAsyncIOReference):
            raise IncompatibleReference
    else:
        field.reference_cls = MotorAsyncIOReference

I came up with this solution and i am pretty sure there is more elegant way to do it. Obviously, this should be done for every framework

@lafrech
Copy link
Collaborator

lafrech commented May 2, 2021

I didn't take the time to investigate much but from what you're writing here, we could do what you propose.

Use None as default and override in framework if None. I wouldn't even bother adding the compatibility check. Modifying this is an advanced feature and the user should know what he's doing.

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

No branches or pull requests

2 participants