Skip to content

Commit

Permalink
Provide overload type hints for search_issues variants (pycontribs#1861)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stealthii authored and Wojtini committed Sep 2, 2024
1 parent 07b84bf commit 10afa59
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
5 changes: 1 addition & 4 deletions examples/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

from collections import Counter
from typing import cast

from jira import JIRA
from jira.client import ResultList
Expand All @@ -25,9 +24,7 @@
props = jira.application_properties()

# Find all issues reported by the admin
# Note: we cast() for mypy's benefit, as search_issues can also return the raw json !
# This is if the following argument is used: `json_result=True`
issues = cast(ResultList[Issue], jira.search_issues("assignee=admin"))
issues: ResultList[Issue] = jira.search_issues("assignee=admin")

# Find the top three projects containing issues reported by admin
top_three = Counter([issue.fields.project.key for issue in issues]).most_common(3)
31 changes: 31 additions & 0 deletions jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3483,6 +3483,22 @@ def resolution(self, id: str) -> Resolution:

# Search

@overload
def search_issues(
self,
jql_str: str,
startAt: int = 0,
maxResults: int = 50,
validate_query: bool = True,
fields: str | list[str] | None = "*all",
expand: str | None = None,
properties: str | None = None,
*,
json_result: Literal[False] = False,
use_post: bool = False,
) -> ResultList[Issue]: ...

@overload
def search_issues(
self,
jql_str: str,
Expand All @@ -3492,6 +3508,21 @@ def search_issues(
fields: str | list[str] | None = "*all",
expand: str | None = None,
properties: str | None = None,
*,
json_result: Literal[True],
use_post: bool = False,
) -> dict[str, Any]: ...

def search_issues(
self,
jql_str: str,
startAt: int = 0,
maxResults: int = 50,
validate_query: bool = True,
fields: str | list[str] | None = "*all",
expand: str | None = None,
properties: str | None = None,
*,
json_result: bool = False,
use_post: bool = False,
) -> dict[str, Any] | ResultList[Issue]:
Expand Down
12 changes: 7 additions & 5 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,24 @@ def setUp(self):

def test_search_issues(self):
issues = self.jira.search_issues(f"project={self.project_b}")
issues = cast(ResultList[Issue], issues)
self.assertLessEqual(len(issues), 50) # default maxResults
for issue in issues:
self.assertTrue(issue.key.startswith(self.project_b))

def test_search_issues_json(self):
result = self.jira.search_issues(f"project={self.project_b}", json_result=True)
issues = result["issues"]
self.assertLessEqual(len(issues), 50) # default maxResults
for issue in issues:
self.assertTrue(issue["key"].startswith(self.project_b))

def test_search_issues_async(self):
original_val = self.jira._options["async"]
try:
self.jira._options["async"] = True
issues = self.jira.search_issues(
f"project={self.project_b}", maxResults=False
)
issues = cast(ResultList[Issue], issues)
self.assertEqual(len(issues), issues.total)
for issue in issues:
self.assertTrue(issue.key.startswith(self.project_b))
Expand All @@ -263,7 +268,6 @@ def test_search_issues_startat(self):

def test_search_issues_field_limiting(self):
issues = self.jira.search_issues(f"key={self.issue}", fields="summary,comment")
issues = cast(ResultList[Issue], issues)
self.assertTrue(hasattr(issues[0].fields, "summary"))
self.assertTrue(hasattr(issues[0].fields, "comment"))
self.assertFalse(hasattr(issues[0].fields, "reporter"))
Expand Down Expand Up @@ -292,7 +296,6 @@ def test_search_issues_fields_translating(self):

def test_search_issues_expand(self):
issues = self.jira.search_issues(f"key={self.issue}", expand="changelog")
issues = cast(ResultList[Issue], issues)
# self.assertTrue(hasattr(issues[0], 'names'))
self.assertEqual(len(issues), 1)
self.assertFalse(hasattr(issues[0], "editmeta"))
Expand All @@ -304,7 +307,6 @@ def test_search_issues_use_post(self):
with pytest.raises(JIRAError):
self.jira.search_issues(long_jql)
issues = self.jira.search_issues(long_jql, use_post=True)
issues = cast(ResultList[Issue], issues)
self.assertEqual(len(issues), 1)
self.assertEqual(issues[0].key, self.issue)

Expand Down

0 comments on commit 10afa59

Please sign in to comment.