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

[v0.17.0] - Check for record type will likely fail for dict records #87

Closed
joelghill opened this issue Jul 14, 2023 · 5 comments · Fixed by #88
Closed

[v0.17.0] - Check for record type will likely fail for dict records #87

joelghill opened this issue Jul 14, 2023 · 5 comments · Fixed by #88
Labels
bug Something isn't working

Comments

@joelghill
Copy link
Contributor

Haven't actually tested, but the logic in is_record_type in atproto.xrpc_client.models.utils will likely fail for dicts:

def is_record_type(model: t.Union[ModelBase, dict], types_module) -> bool:
    if isinstance(model, RecordModelBase) and hasattr(types_module, 'Main'):
        # for now records in Main. could be broken late
        if isinstance(model, dict):  # custom record
            return types_module.Main._type == model.get('$type')

        return types_module.Main._type == model._type

    return False

I think it should be:

def is_record_type(model: t.Union[ModelBase, dict], types_module) -> bool:
    if isinstance(model, RecordModelBase) and hasattr(types_module, 'Main'):
        # for now records in Main. could be broken late
        return types_module.Main._type == model._type

    if isinstance(model, dict):  # custom record
        return types_module.Main._type == model.get('$type')

    return False
@MarshalX
Copy link
Owner

MarshalX commented Jul 15, 2023

wow. looks of course legit due to invalid isinstance() logic except how it should work

i think it should be:

def is_record_type(model: t.Union[ModelBase, dict], types_module) -> bool:
    # for now records in Main. could be broken late
    if hasattr(types_module, 'Main'):
        if isinstance(model, RecordModelBase):
            return types_module.Main._type == model._type
        if isinstance(model, dict):  # custom record
            return types_module.Main._type == model.get('$type')

    return False

we don't rly support unknown records from custom lexicons at all we case we don't know this custom lexicon... by "custom record" i mean an extended version of the base record (described by official lexicons). extended with additional fields, for example

@MarshalX MarshalX added the bug Something isn't working label Jul 15, 2023
@MarshalX
Copy link
Owner

MarshalX commented Jul 15, 2023

more clear indents :

def is_record_type(model: t.Union[ModelBase, dict], types_module) -> bool:
    # for now, records in the Main class only. could be broken late
    if not hasattr(types_module, 'Main'):
        return False

    if isinstance(model, dict):  # custom (extended) record
        return types_module.Main._type == model.get('$type')

    return types_module.Main._type == model._type

@joelghill
Copy link
Contributor Author

Thanks! I'll take another crack at this tomorrow if I have time.

Do you have guidelines on the formatting, listing, unit test, etc? I see you're using Black and Ruff?

@MarshalX
Copy link
Owner

MarshalX commented Jul 16, 2023

Thanks! I'll take another crack at this tomorrow if I have time.

Do you have guidelines on the formatting, listing, unit test, etc? I see you're using Black and Ruff?

Not yet. All that you need is poetry run black . and poetry run ruff . --fix (of course after poetry install)

@MarshalX
Copy link
Owner

fixed in v0.0.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants