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

Use of literal values in generic types #8605

Closed
timfi opened this issue Mar 30, 2020 · 1 comment
Closed

Use of literal values in generic types #8605

timfi opened this issue Mar 30, 2020 · 1 comment

Comments

@timfi
Copy link

timfi commented Mar 30, 2020

Hi, I was toying around with some typing in a small but "hacky" vector-maths module and found myself wondering if it was possible to make a generic type along the lines of Vector[3] where the 3 holds actual meaning/can be used in some fashion. I am unsure if this is possible with mypy as it stands now, and if not if this is something that is ever going to be possible (be it because it is "ugly" or outside of what mypy is trying to achieve).

For example some of such "vector-math-module" code could look like this:

class Vector(...):
    def __init__(self, *values: Union[float, int]):
        self._values = values

    def __add__(self: Vector[T], other: Vector[T]) -> Vector[T]:
        return Vector(*(a + b for a, b in zip(self._values, other._values)))

Vector(1, 2, 3) + Vector(1, 2, 3)
# -> mypy: ok

Vector(1, 2, 3) + Vector(1, 2)
# -> mypy: type mismatch
@msullivan
Copy link
Collaborator

Yeah, this has been discussed. We'd like to probably support something along this lines to enable better numpy support, but though a lot of things have been discussed it isn't on the roadmap right now to get core dev work done on it.

#3345 is one issue about it; there might be others.

Thanks!

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

2 participants