Bug? @overload
ed __init__()
on a Generic
class
#9701
Replies: 1 comment
-
You're using aspects of the type system that are not well defined or specified. In particular, you're using value-constrained type variables — and with literal type constraints. I recommend rethinking your approach so you can use type system features that are better defined. One obvious solution is to create a class hierarchy — one subclass for "color", "flavor" and "size" — rather than trying to make a single class act upon all three types and then using a string to differentiate between them. As for your question about bugs in pyright, I think pyright is doing the right thing here, so I don't think any of these are bugs. The first set of errors (in Class1) are legitimate and dictated by the draft typing spec section on overlapping overloads. Not surprisingly, mypy generates the same errors in this case. The second set of errors (in Class2) also appears to be legitimate, although mypy doesn't generate errors in this case. I think this is a bug in mypy, which doesn't fully implement the latest updates in the Python typing spec regarding constructors. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to have a
Generic
class set specific values for itsTypeVar
s based on parameters passed toThatClass.__init__()
using a set of@overload
rules. There doesn't seem to be a good (no errors) way to annotate this situation, but it's not clear to me which subset of these error messages represent bugs in pyright, and which are expected limitations of the tool.If I annotate the relevant parameters in the
__init__()
@overload
s using the narrowed, concrete types, I get errors about the implementation (non-@overload
method & body) being incompatible, presumably because pyright doesn't understand that these are equivalent (Class1
).If I only annotate
self
on the@overload
s, pyright warns only the 1st signature will ever be used, because all three@overload
s use the "same" types for their non-self
parameters – again, pyright hasn't taken into account that the annotation ofself
changes the semantic meaning of theTypeVar
s associated with the class (Class2
).With both approaches, pyright does the right thing with the constructor is actually called (i.e., the constructed instances are assigned the right specialization of
Class1
orClass2
as the case may be); the problems (or reports of problems) occur only where the constructors are declared.My suspicion is that both sets of error messages are bugs in pyright (in the sense that they represent surprising and undesired behaviour), but this may be based on a misunderstanding of the limitations of type inference.
I'd appreciate advice on where issues should be filed, or suggestions on a more appropriate way to handle this.
[pyright playground]:
Beta Was this translation helpful? Give feedback.
All reactions