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

Fix: Remove UNSET objects from lists #26

Merged
merged 2 commits into from
Apr 1, 2023

Conversation

dosisod
Copy link
Contributor

@dosisod dosisod commented Mar 31, 2023

Example repro:

from datetime import datetime, timezone
import asyncio

from githubkit import GitHub

async def main():
    github = GitHub()

    now = datetime.now(timezone.utc)

    await github.rest.checks.async_create(
        "example",
        "repo",
        data={
            "name": "name",
            "head_sha": "0000000000000000000000000000000000000000",
            "external_id": "0000000000000000000000000000000000000000",
            "status": "completed",
            "conclusion": "failure",
            "started_at": now,
            "completed_at": now,
            "output": {
                "title": "some title",
                "summary": "some summary",
                "annotations": [
                    {
                        "path": "file.txt",
                        "start_line": 1,
                        "end_line": 1,
                        "start_column": 1,
                        "annotation_level": "failure",
                        "message": "some message",

                        # Missing default fields here are causing the issue
                    }
                ],
            },
        },
    )

asyncio.run(main())

Running with githubkit version 0.9.8:

$ python3 file.py
Traceback (most recent call last):
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/core.py", line 273, in _arequest
    json=obj_to_jsonable(json),
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 55, in obj_to_jsonable
    return {k: obj_to_jsonable(v) for k, v in obj.items()}
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 55, in <dictcomp>
    return {k: obj_to_jsonable(v) for k, v in obj.items()}
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 55, in obj_to_jsonable
    return {k: obj_to_jsonable(v) for k, v in obj.items()}
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 55, in <dictcomp>
    return {k: obj_to_jsonable(v) for k, v in obj.items()}
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 58, in obj_to_jsonable
    return [obj_to_jsonable(item) for item in obj]
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 58, in <listcomp>
    return [obj_to_jsonable(item) for item in obj]
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 55, in obj_to_jsonable
    return {k: obj_to_jsonable(v) for k, v in obj.items()}
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 55, in <dictcomp>
    return {k: obj_to_jsonable(v) for k, v in obj.items()}
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/utils.py", line 63, in obj_to_jsonable
    return pydantic_encoder(obj)
  File "pydantic/json.py", line 90, in pydantic.json.pydantic_encoder
TypeError: Object of type 'Unset' is not JSON serializable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "REDACTED/file.py", line 41, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "REDACTED/file.py", line 11, in main
    await github.rest.checks.async_create(
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/rest/checks.py", line 276, in async_create
    return await self._github.arequest(
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/core.py", line 347, in arequest
    raw_resp = await self._arequest(
  File "REDACTED/.venv/lib/python3.10/site-packages/githubkit/core.py", line 280, in _arequest
    raise RequestError(repr(e)) from e
githubkit.exception.RequestError: TypeError("Object of type 'Unset' is not JSON serializable")

I figured out that the exclude_unset() function doesn't traverse lists, so UNSET objects inside dicts are not excluded if they are inside a list.

There might be other data types we might want to add to this list (set, tuple, etc), though I didn't check to see how often these where used in the models (if at all).

@yanyongyu yanyongyu added the bug Something isn't working label Apr 1, 2023
githubkit/utils.py Outdated Show resolved Hide resolved
Copy link
Owner

@yanyongyu yanyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yanyongyu yanyongyu changed the title Remove UNSET objects from lists Fix: Remove UNSET objects from lists Apr 1, 2023
@yanyongyu yanyongyu merged commit 5ba5574 into yanyongyu:master Apr 1, 2023
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 this pull request may close these issues.

None yet

2 participants