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

Allow a type of Zero #5203

Closed
LivInTheLookingGlass opened this issue Jun 11, 2018 · 5 comments
Closed

Allow a type of Zero #5203

LivInTheLookingGlass opened this issue Jun 11, 2018 · 5 comments

Comments

@LivInTheLookingGlass
Copy link

LivInTheLookingGlass commented Jun 11, 2018

I am requesting a feature. Often people want to be able to sum() their value types together. This is made easiest if their value types can accept an addition of 0. But the signature on such a method implies that it can accept any integer, which is false. The smallest working example would be:

from typing import Union


class Value(object):
    def __init__(self, val: int) -> None:
        self._value = val

    def __add__(self, other: Union[int, 'Value']) -> 'Value':
        if isinstance(other, Value):
            return Value(self._value, other._value)
        elif other == 0:
            return self
        else:
            return NotImplemented

sum((Value(1), Value(3))

It would be nice if this code could instead be written as:

from typing import Union, Zero


class Value(object):
    def __init__(self, val: int) -> None:
        self._value = val

    def __add__(self, other: Union[Zero, 'Value']) -> 'Value':
        if isinstance(other, Value):
            return Value(self._value, other._value)
        elif other == 0:
            return self
        else:
            return NotImplemented

sum((Value(1), Value(3))

This more clearly communicates the intent of the method.

@ilevkivskyi
Copy link
Member

This is a duplicate of #3062 which is already on our short/medium term roadmap. With the literal types you will be able to just write Zero = Literal[0].

@gvanrossum
Copy link
Member

I'm also curious where the convention to just start with 0 comes from. Do you have any examples of real code that uses that? (I understand the principle, I am just wondering who would do such a thing.)

@LivInTheLookingGlass
Copy link
Author

I dont have a real example off the top of my head, except for code I was working on for a book club (Test Driven Development by Example). The book was written with Java in mind, and we decided that we should each implement it in our favorite language.

@gvanrossum
Copy link
Member

gvanrossum commented Jun 12, 2018 via email

@pawelswiecki
Copy link

pawelswiecki commented Oct 28, 2018

I think Union[Literal[0], Value] or Union[Literal[False], Value] (or even Union[Literal[""], Value], depending on the use-case) is analogous to Union[None, Value], that is to Optional[Value].

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

4 participants