Skip to content

reportAbstractUsage check ignores __new__ return type #11255

@camillol

Description

@camillol

Background

Some classes define __new__ so that calling the class always instantiates some specific subclass; for example, instantiating pathlib.Path always produces a PosixPath or a WindowsPath.

Here is an example.

Note that you can type __new__ as def __new__(cls) -> Foo | Bar, and pyright correctly understands that if you write b = Base(), b's type is Foo | Bar.

Describe the bug

However, if your base class is abstract, pyright reports a "Cannot instantiate abstract class" error, even if the class(es) it actually instantiates are concrete. Example

Expected result
I think the reportAbstractUsage check should base its decision on __new__'s return type. If it returns a concrete class, or union of concrete classes, it should not flag it as an attempt to instantiate an abstract class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions