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

False positive with Union of collections #1614

Open
shenriotpro opened this issue Apr 11, 2024 · 0 comments
Open

False positive with Union of collections #1614

shenriotpro opened this issue Apr 11, 2024 · 0 comments

Comments

@shenriotpro
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant