Thanks Rebecca (and Eric for pointing me privately towards an example in
the wild).
This is clearly a deep issue, and I'm still not sure what to think of it. I
agree that typevars are fairly often mistaken for unions (AnyStr
especially), and also for type aliases. We ought to warn users against
that. It's very subtle, e.g. I thought that AnyStr would be equivalent to
Union[str, bytes], but it's not:
```
def f(a: AnyStr):
...
def g(a: Union[str, bytes]):
f(a)
```
produces an error in mypy:
```
Value of type variable "AnyStr" of "f" cannot be "Union[str, bytes]"
[type-var]
```
Pyright finds no fault with it currently, and I think technically that's
better.
(Replacing AnyStr with A defined here:
```
A= TypeVar("A", bound=Union[str, bytes])
```
makes that code valid in mypy too, BTW.)
But making users believe that a typevar is like a union or type alias just
causes disappointments when they start using *two* typevars:
```
def f(a1: AnyStr, a2: AnyStr):
...
f("", b"") # Error!
```
(Pyright doesn't flag this as an error. I don't see how `f("", b"")` would
solve the typevar though.)
I know I previously argued against strictness here, and I think that
technically there is nothing wrong with such code, but it seems we should
at least educate users better, maybe at least with a warning?
--Guido
On Sat, Dec 12, 2020 at 2:51 PM Rebecca Chen
Eric, pytype still emits an error for a TypeVar that appears only once in a function signature.
If it helps, misunderstandings I've seen a lot are:
- Using a TypeVar instead of a Union (especially common with typing.AnyStr) - Using a TypeVar to mean "subclass of", e.g.:
BaseClassVar = TypeVar("BaseClassVar", bound=BaseClass) def f(x: BaseClassVar): ... # the intention is that this means "a subclass of BaseClass but not BaseClass itself". I still have not figured out where this idea came from.
- Using a TypeVar in a class without inheriting from Generic.
Best, Rebecca
On Sat, Dec 12, 2020 at 7:45 AM Guido van Rossum
wrote: Interesting. Can you give (or link to) some examples of the misunderstandings? I’d like to understand what might lead them to that (other than cargo-culting).
On Fri, Dec 11, 2020 at 22:27 Eric Traut
wrote: I apologize for reopening this discussion, but I'm considering reversing our earlier decision and adding an error (or at least a warning) in pyright for a TypeVar that appears only once in a function signature.
We continue to receive a stream of bug reports filed against pyright that are actually bugs in the user's code based on misuse (and a misunderstanding) of TypeVars in generic functions. Most of these misunderstandings could be avoided if we introduction an error message in this case.
Rebecca, I'm curious what decision you made for pytype. Does it still emit an error in this case, or did you decide to remove the check based on this discussion?
-Eric
-- Eric Traut Contributor to pyright & pylance Microsoft Corp. _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --Guido (mobile) _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: rechen@google.com
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...