pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Disallow instantiating abstract classes

Open yangdanny97 opened this issue 8 months ago • 1 comments

https://pyrefly.org/sandbox/?code=MQAg6gpgNgxg9gWwiALnVALZAFAngJwgDMpcQBlAQwDsATAIzgA8AaTASwGcQuRKQADpQDmEAFCgA7lkIhccAK4gYNEJPzsUENpwyVZNWiCgR91PowUpUuAe2rCe5vCgxxqAOgkSQACQgGBsqKAiYgRKYoCoTccEQc3JyGjEwAXD4AtCAA4hDWhJRQGSjsSDYCyDBYMADW9o4REAyUtZkgAKqcyIQAbqZQAPoothAAFACUqOj2nBUw1vYR+IRGwxWcbRBMFRpI1NaSmhiYyACSACIAouGR0RDcrpQHyJwKAgJw+NajEB7CHnwrHB4AhQnltCBhHBitDaMR7Jp2O42G4+vg2HkYOM2mANFo+HQQPgFM5cK53ME4SBaOxCPNSE45Ip8CB6Pg4JIuvgxGIiOyEOV6jxQZ9rJR6JwUPgWigkOTaDyYFBKJxuABBCVSmUAIRVEHSIENIAAAuLJdL5nK3AqjdTiCAmKMulAiJMMgA+EDmg22o1CVWK5WqkAAYXcMEIWlGGvNOr14x9hrh8UdztdIA9Xqlid9kei5gARAWeUwQABeUPhyNjSYgUAAeQA0mIyBWY1r5rquhM6yAAHJwaxNsRAA

If the class defines an abstract method or inherits an abstract method & doesn't override it, then it cannot be instantiated.

yangdanny97 avatar Mar 27 '25 23:03 yangdanny97

We also need the following:

  • disallow calling abstract class methods
  • skip return check to allow ... and docstring and NotImplemented for abstract method bodies

yangdanny97 avatar May 28 '25 15:05 yangdanny97

To add to this, if a class inherits abstract methods from an ABC and is marked final, it must implement them. Currently Pyrefly does not warn about this while Mypy and Pyright do. Pyright actually doesn't warn about this if the base class is not an ABC, but Mypy still does. Sandbox example.

I don't know if this should be raised as a separate issue/feat or be a part of this one.

VillePuuska avatar Jun 07 '25 09:06 VillePuuska

Added the two tasks as sub-issues.

yangdanny97 avatar Jun 18 '25 01:06 yangdanny97