False positive with Union of collections
shenriotpro opened this issue · comments
Stéphane Henriot commented
Hi folks,
import json
from typing import Any
def parse_json(s: str) -> dict[str, Any] | list[dict[str, Any]]:
return json.loads(s)
def foo() -> list[dict[str, Any]]:
res = []
l = parse_json('[{"abc": 123}]')
assert isinstance(l, list)
res.extend(l)
return res
def bar() -> list[dict[str, Any]]:
l = parse_json('[{"abc": 123}]')
assert isinstance(l, list)
return l
if __name__ == '__main__':
assert foo() == [{'abc': 123}]
I'm not sure why pytype is unhappy with foo (but bar is fine). It looks like a bug to me.
~$ mypy toy.py
Success: no issues found in 1 source file
~$ pytype toy.py
Computing dependencies
Analyzing 1 sources with 0 local dependencies
ninja: Entering directory `.pytype'
[1/1] check toy
FAILED: /home/user/.pytype/pyi/toy.pyi
/usr/bin/python3 -m pytype.main --imports_info /home/user/.pytype/imports/toy.imports --module-name toy --platform linux -V 3.10 -o /home/user/.pytype/pyi/toy.pyi --analyze-annotated --nofail --quick /home/user/toy.py
File "/home/user/toy.py", line 14, in foo: bad return type [bad-return-type]
Expected: List[Dict[str, Any]]
Actually returned: List[str]
For more details, see https://google.github.io/pytype/errors.html#bad-return-type
ninja: build stopped: subcommand failed.
Leaving directory '.pytype'
~$ pytype --version
2024.03.19
Cheers.