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

Issue with community.list #56

Closed
Demigodrick opened this issue Jul 16, 2023 · 8 comments
Closed

Issue with community.list #56

Demigodrick opened this issue Jul 16, 2023 · 8 comments

Comments

@Demigodrick
Copy link
Contributor

Demigodrick commented Jul 16, 2023

Before I submit a PR I just wanted to check I wasnt doing something wrong and overcomplicating.

I was trying to run the following code:

def get_communities():
    output = lemmy.community.list(limit=10, page= 1, sort=SortType.New, type_=ListingType.Local)
    print(output)

But i found that the output was always ALL communities regardless of the variables, i.e listing type and sort type.

I've had a look at communities.py and changed:

        list_community: dict = {}

        if limit is not None:
            list_community["limit"] = limit
        if page is not None:
            list_community["page"] = page
        if sort is not None:
            list_community["sort"] = sort.value
        if type_ is not None:
            list_community["type"] = type_.value

        if data := self._requestor.api(Request.GET, "/community/list", params=list_community):
            return data["communities"]
        return []

to

        params: dict[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"}
        return self._requestor.api(Request.GET, "/community/list", params=params)

Which now works when I pass the following:

def get_communities():
    output = lemmy.community.list(limit=10, page= 1, sort="New", type_="Local")
    print(output)

To circle back to the original question - is this a bug in the original code (in which case I'll submit a PR) or is it me running the query wrong?

Thanks

@db0
Copy link
Owner

db0 commented Jul 17, 2023

The code you replaced with should work identically. I'll run some tests

@NicKoehler
Copy link
Collaborator

NicKoehler commented Jul 18, 2023

the issue is here:

if type_ is not None:
    list_community["type"] = type_.value # < here

it needs to be changed to:

if type_ is not None:
    list_community["type_"] = type_.value # see underscore was missing on the "type" string

@db0
Copy link
Owner

db0 commented Jul 18, 2023

Ye the latter approach is better. It's what I've been using already as its future proof.

@db0
Copy link
Owner

db0 commented Jul 18, 2023

@Demigodrick feel free to send that PR

@NicKoehler
Copy link
Collaborator

The only problem with using this:

params: dict[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"}

is that there are enums where the actual value needs to be .value or we can change the enum class to do that automatically

@db0
Copy link
Owner

db0 commented Jul 18, 2023

Not sure what you mean by "value needs to be .value"

@NicKoehler
Copy link
Collaborator

Not sure what you mean by "value needs to be .value"

I mean like this:

list_community["type_"] = type_.value

@db0
Copy link
Owner

db0 commented Jul 19, 2023

Fixed in #58

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

3 participants