pycontribs / jira

Python Jira library. Development chat available on https://matrix.to/#/#pycontribs:matrix.org
https://jira.readthedocs.io
BSD 2-Clause "Simplified" License
1.96k stars 870 forks source link

`client#search_issues`: `Incompatible types in assignment (expression has type "Union[str, Issue]", variable has type "Issue")` #1608

Closed stdedos closed 2 months ago

stdedos commented 1 year ago

Bug summary

Using

issues = jira.search_issues(query, maxResults=False)

Gives the aforementioned result:

 error: Incompatible types in assignment (expression has type "Union[str, Issue]", variable has type "Issue")  [assignment]

The culprit seems to be:

Returns:
            Union[Dict,ResultList]: Dict if ``json_result=True``

which should be quickly solveable with a little bit of https://mypy.readthedocs.io/en/stable/more_types.html#function-overloading

Is there an existing issue for this?

Jira Instance type

Jira Cloud (Hosted by Atlassian)

Jira instance version

No response

jira-python version

3.4.1

Python Interpreter version

3.9

Which operating systems have you used?

Reproduction steps

# 1. Given a Jira client instance
jira: JIRA
# 2. When I call the function with argument x
jira.the_function(x)
# 3.
...

Stack trace

-

Expected behaviour

No typing issues

Additional Context

No response

GPHemsley-RELX commented 1 year ago

I'm getting a different error in Python 3.11 that I think is caused by the same thing.

If I try to access anything that ResultList has but Dict[str, Any] doesn't, Pylance gets angry.

Cannot access member "total" for type "Dict[str, Any]"
  Member "total" is unknown
Argument of type "Literal[0]" cannot be assigned to parameter "__key" of type "str" in function "__getitem__"
  "Literal[0]" is incompatible with "str"

And if I try say that the return from search_issues is explicitly ResultList during variable assignment, I get this:

Expression of type "Dict[str, Any] | ResultList[Issue]" cannot be assigned to declared type "ResultList[Unknown]"
  Type "Dict[str, Any] | ResultList[Issue]" cannot be assigned to type "ResultList[Unknown]"
    "Dict[str, Any]" is incompatible with "ResultList[Unknown]"

or for ResultList[Issue]:

Expression of type "Dict[str, Any] | ResultList[Issue]" cannot be assigned to declared type "ResultList[Issue]"
  Type "Dict[str, Any] | ResultList[Issue]" cannot be assigned to type "ResultList[Issue]"
    "Dict[str, Any]" is incompatible with "ResultList[Issue]"

So in order to quiet the errors I have to explicitly say:

assert isinstance(issues, jira.client.ResultList)
Stealthii commented 5 months ago

@stdedos @GPHemsley-RELX: #1861 should resolve this.