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

Empty lists result in overly broad types #289

Open
DRMacIver opened this issue Apr 18, 2023 · 1 comment
Open

Empty lists result in overly broad types #289

DRMacIver opened this issue Apr 18, 2023 · 1 comment
Labels

Comments

@DRMacIver
Copy link

All of the following is tested on MonkeyType 23.3.0.

Suppose I have the following function as foo.py:

def bar(nums):
    return 0

And run the following code in test_foo.py:

from foo import bar

if __name__ == '__main__':
    bar([1])
    bar([])

Then apply this with:

monkeytype run test_foo.py
monkeytype apply foo

This results in the following type:

from typing import Any, List, Union

def bar(nums: List[Union[Any, int]]) -> int:
    return 0

Presumably the root cause of this is that an empty list does not have an obvious type associated with its element, so it is being typed as List[Any], but this results in fairly non-ideal behaviour because it means that if a function that takes a list of a specific type is ever called with an empty list, it will instead get this over broad type

@carljm carljm added the bug label Apr 18, 2023
@carljm
Copy link
Contributor

carljm commented Apr 18, 2023

Hmm, thanks for the report. Seems like this must have regressed at some point, since I'm fairly sure it wasn't the case when I used MonkeyType more actively. Will investigate.

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

No branches or pull requests

2 participants