Open flisboac opened 2 years ago
I don't know how (or whether) mypy's implementation of PEP 612 handles overloads. The PEP doesn't provide much guidance about how an overloaded function should bind to a ParamSpec. I think pyre handles this case in some manner. When I implemented PEP 612 in pyright, I chose not to support this case, so pyright emits an explicit error for your code above. Mypy doesn't emit an error, but it also doesn't appear to handle the binding of an overloaded signature to a ParamSpec.
@erictraut I know there's a PR going on regarding ParamSpec, but overall, support for it in mypy is somewhat decent. I tried it and in most places it works very well -- although things like Concatenate are entirely not implemented.
Now, it seems the ParamSpec is properly bound, but only for the first overload. You can swap overload positions, and they'll work as expected, which for me hints at the possibility of mypy not identifying all overloads in this specific scenario. I don't know if the problem is with __init__
or with the fact that the class is an ABC, or if it's something else (e.g. what if I used __new__
instead, would it be different?)
But I agree that ParamSpec could affect overload selection. Maybe the argument matching is not working, or is not implemented yet, which prevents mypy from selecting anything other the immediate/default overload (which in this case seems to be the first).
So far it's the first time I tried such an idiom (ie. __init__
to fix constructor arguments, and ParamSpec
to avoid replicating them in factory functions), and it's very much possible that this is not the intended way those functionalities should be used (if that's the case, please do tell me!).
Bug Report
Mypy doesn't seem to identify all overloads of the
__init__
of an ABC class. Didn't test if the same happens for concrete classes, though.To Reproduce
Please se this mypy playground: https://mypy-play.net/?mypy=latest&python=3.10&gist=0c727c040c7b0d58aa25931dc17cd09f
This can be reproduced with the following code snippet. Please read the comments in the code for some context in the example as well:
Expected Behavior
Should accept all overloads.
Actual Behavior
Only accepts the very first overload of
__init__
. Solution so far seems to be not to use overloads at all, and rely on just the single@abstractmethod
signature.Your Environment
mypy.ini
(and other config files): (Please see mypy playground link)