-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Rename typevar('T', values=(t1, t2, ...)) to TypeVar('T', t1, t2, ...) #539
Comments
Renaming The One thing I'd like to point out is that if we use Before deciding the syntax for |
On the python-ideas thread I started just before Xmas there was quite a bit of push-back on the current semantics of typevar constraints from Eugene Toder. (Starting here: https://mail.python.org/pipermail/python-ideas/2014-December/030482.html .) Basically he claims that the more common type of constrained type variable should be covariant. I think this example would show it:
I am skeptical of his claim, but I am not sufficiently familiar with other type systems to refute it. All I can say is, if foo() receives an instance of an unknown subclass of Employee, how can it create an instance of the appropriate class (if it's not just returning the argument)? To which Eugene replied that in such cases the Employee class should require that ever subclass has a copying factory (https://mail.python.org/pipermail/python-ideas/2014-December/030488.html), which hardly seems a general solution. The discussion went fractal after that. Assuming we resolve that particular issue by ignoring or refuting it, we seem to have two issues on left the table before we can choose a syntax:
I propose to use |
Eugene's examples are more relevant than your example since they use F-bounded polymorphism (see below), and I agree that it would be nice to be able to support them. Another term used for type variable subtype constraints is bound, and an F-bound contains a constrained type variable in the bound. Here is another example motivated by builtins (
(I'm assuming that This could also be useful for things like He also proposes something called explicit protocols, which look interesting. I'm not sure if they solve exactly the same problem as the current mypy approach, since I don't seem anything restricting a type like |
Wow, there are a lot of different ideas here. Maybe we need separate issues to discuss them? Here or in the typehinting repo? (In my defense, Eugene also supplied the Employee example, in the first of the two messages I linked to; he used
|
Renamed Var to TypeVar in b7d16de to leave space for variable types in the future. |
As for explicit protocols, there are some competing (as in: not easily composable) standards here:
etc. etc. As ABCs are built-in, it seems natural to suggest that they should be used for defining interfaces. However, I understand that static analysis (so, the type checker) might not be able to process every abstract class in general. |
Łukasz, could you move the discussion on protocols to python/typing#11 ? |
In the typehinting PEP the proposal is to use Var() instead of typevar(), and to make the constraints just varargs, rather than a keyword parameter (and why would the keyword be 'values'?).
The semantics of typevar() are fine. The PEP currently seems to state that two Var() calls with the same name generate equivalent variables, whereas mypy treats every typevar() as unique. It also enforces that the typevar is assigned to a variable with the same name as the typevar argument. I think the mypy rules are better, and easier to implement, so I prefer those.
See also python/typing#1 for the first issue (renaming, signature change) and python/typing#29 for the second (uniqueness).
The text was updated successfully, but these errors were encountered: