make ExitStack/AsyncExitStack not appear as abstract#15488
make ExitStack/AsyncExitStack not appear as abstract#15488KotlinIsland wants to merge 1 commit intopython:mainfrom
ExitStack/AsyncExitStack not appear as abstract#15488Conversation
Hmm, isn't that a bug in those tools? These classes don't have any abstract methods on them. Having ABCMeta as the metaclass isn't sufficient to make a class abstract. Lots of classes have that metaclass but can still be soundly instantiated at runtime |
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
no, it's intended behaviour, most languages with abstract classes work like that: https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMi4zLjEwIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiYWJzdHJhY3QgY2xhc3MgQVxuXG5mdW4gbWFpbigpIHtcbiAgICBBKClcbn0ifQ== so the author of the class can indicate: "you shouldn't instantiate this class directly, it's abstract. you should subtype it"
|
that's certainly interesting, but it's not how Python works at runtime and I think type checkers should follow the runtime here. No other type checker has this behaviour -- here's a gist you can load into multiplay: 4abd6426cf1b97d8453e1822236246f9. |
at runtime python doesn't care about type annotations, so to say that we should always be relaxed as the runtime permits isn't valid to me. i think this is a matter of opinion, i think it is a very valuable addition to the type checker that i have taken advantage of in my work, and the need for it was what motivated me and others to implement it to begin with of course i wouldn't want to corrupt typeshed to appease some bizarre non-standard feature, but for this specific case, it is a simple fix that actually reduces the duplication of the implementation, while also helping to remove some false positives from these tools |

in basedpyright/pycharm the explicit usage of
ABCMeta/ABCcauses the class to be treated as abstract and report an error. these classes are defined a little different to reality, so they hadABCMeta, i've just moved that to the base class to resolve the problems it was causing