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

applying sum() to List[Decimal] does not result in Decimal, but Union[Decimal, int] #7742

Closed
varunabgupta opened this issue Oct 18, 2019 · 1 comment
Labels

Comments

@varunabgupta
Copy link

Using mypy 0.700 (no additional flags) on the following method:

def add_array(array):
    # type: (List[Decimal]) -> Decimal
    return sum(array)

results in:

error: Incompatible return value type (got "Union[Decimal, int]", expected "Decimal")

Even if I add a check for an empty list:

def add_array(array):
    # type: (List[Decimal]) -> Decimal
    if len(array) == 0:
        return Decimal('0')
    return sum(array)

the same exact error is raised.

Is this intended?

@emmatyping
Copy link
Collaborator

Technically, if you pass an empty iterable like [], then sum will return 0 (as an int!). So the Union is correct.

In addition, mypy currently doesn't (nor likely will it) have support for checking the length of a list like in your second example. See also #7626 for a related issue.

Also, mypy 0.740 was just released, so you may want to update :)

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