yanyongyu / githubkit

The modern, all-batteries-included GitHub SDK for Python, including rest api, graphql, webhooks, like octokit!
https://yanyongyu.github.io/githubkit/
MIT License
204 stars 25 forks source link

Type checking error when using `created` filter in `list_workflow_runs` function #137

Closed oscarsj closed 2 months ago

oscarsj commented 2 months ago

When I use list_workflow_run functions I'm using created parameter to filter out workflow runs. The way of using this parameter is by providing a string with an arithmetic comparison and a stringified datetime, something like:

wfs = github.rest.actions.list_workflow_runs(
    owner=owner,
    repo=repo_name,
    workflow_id=workflow_id,
    branch=branch_name,
    event="workflow_dispatch",
    created=f">={dispatch_time.strftime('%Y-%m-%dT%H:%M:%SZ')}",
).parsed_data

as you can see, the type of the created parameter is a str.

But when I run mypy type checking over this piece of code, I get:

pip freeze | grep -i githubkit
GitHubKit==0.11.8

mypy scripts/test.py
scripts/test.py:25: error: Argument "created" to "list_workflow_runs" of "ActionsClient" has incompatible type "str"; expected "Literal[Unset._UNSET] | datetime | None"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

This type checking result is misleading and breaks our automated type-safety checks.

This started happening in version 0.11.8. I think this behaviour can be traced to this line changed: https://github.com/yanyongyu/githubkit/blob/81c7e863211f2a5a2cf1fb03c4a8c7b0f926e02a/githubkit/typing.py#L83 on PR https://github.com/yanyongyu/githubkit/pull/117/files#diff-70cf106ab334e2539867d80d8c49a85c44184d1d8c319b3a95c1484a0e5cafbdR83

If I downgrade to 0.11.7 and run mypy, I get no errors:

pip freeze | grep -i githubkit
GitHubKit==0.11.7

mypy scripts/test.py
Success: no issues found in 1 source file

The script I used to test that the created filter indeed was working and expecting a string is: https://gist.github.com/oscarsj/600804cb56752ea0047c82220173162b

The expected behaviour is that created parameter (and possibly other datetime-like params used for filtering) has a type compatible with str.

yanyongyu commented 2 months ago

This is due to the github openapi schema change. In the latest schema, the created query is described as:

created:
  name: created
  description: Returns workflow runs created within the given date-time range.
    For more information on the syntax, see "[Understanding the search syntax](https://docs.github.com/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax#query-for-dates)."
  in: query
  required: false
  schema:
    type: string
    format: date-time

The format of the field is set to date-time. We need to make a patch to override this.

oscarsj commented 2 months ago

Thanks for the swift reaction! ❤

yanyongyu commented 2 months ago

This will be addressed in the next release 😄