Replies: 2 comments 3 replies
-
And I think python/mypy#14106 is a bug report for the identical issue for older mypy version. |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is slightly unusual and I'm not sure mypy is wrong about the "instantiating abstract class" message. I'd consider setting
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to make type annotation work with the
__new__
method in an abstract base class, the code is along these lines:(I know it's a bit unconventional, but for historical reasons we have an existing non-abstract class that we want to make abstract but keep the way client code makes instances).
This looks OK to me and it actually works, but
mypy
complains about the last line:I guess
mypy
assumes that__new__
tries to make an instance ofAbc
instead of a sub-class ofAbc
(I'm not sure that__new__
even allowed to return an instance of abstract class).I tried to workaround it by annotating
__new__
with actual types (this is a sort of lie because actual type can be almost anything in a real use case):and that causes this mypy error:
I managed to suppress the error if I use just a single concrete type on return annotation:
(mypy then complains about
return super().__new__(cls)
, but I can mask it).The last workaround is not ideal, of course, as mypy now thinks that
Abc(...)
always returnsImplX
, which is not true.I wonder if I'm doing something completely wrong and seriously abusing
__new__
, or if this is a limitation ofmypy
. Does anyone know of a better approach to solve this?This is with Python 3.11 and mypy 1.5.1.
Beta Was this translation helpful? Give feedback.
All reactions