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?
On Sat, Dec 12, 2020 at 2:51 PM Rebecca Chen email@example.com wrote:
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
- 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.
On Sat, Dec 12, 2020 at 7:45 AM Guido van Rossum firstname.lastname@example.org 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 email@example.com 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 Traut Contributor to pyright & pylance Microsoft Corp. _______________________________________________ Typing-sig mailing list -- firstname.lastname@example.org To unsubscribe send an email to email@example.com https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: firstname.lastname@example.org
-- --Guido (mobile) _______________________________________________ Typing-sig mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: email@example.com