microsoft / microsoft/pyright

reportAbstractUsage check ignores __new__ return type

Open
#11,255 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**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](https://pyright-play.net/?pyrightVersion=1.1.408&pythonVersion=3.13&strict=true&code=GYJw9gtgBA%2BjwFcAuCQFM5QJYQA5hCSgEMA7UsJYpLMUgZwChRIokBPXLUgc2zwJF0ANzTEANjA640jHPkJQQZACaRGjAMbji9elABCutAC5GUC1BVpgUUsQhoAFPTTjgASigBaAHxR6JBAzS1ClNBQQUigAIgAjYxiNUOtbOFI0AHc4J216Lz8oADEwMCgAH0NiYPMwiyxbPOx9I1cQurCmgF4lVUgAOk0ACzAsTWcAbRKwABoqkABdD1q63VdFLD0EOO013PF6OadpuaMQD2WO8MjosDiAKzRNJH70rJy85a0dPWLSp1aaA87QsqTsDmcrncBX8gRqV3QN1iwFKSW%2Ba3mAOMwJWVhs4McLjcnh8sKCILCiNQ0Xi1TRcSgPUBTmWIjEkmkzjiHiAA).

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](https://pyright-play.net/?pyrightVersion=1.1.408&pythonVersion=3.13&strict=true&code=GYJw9gtgBA%2BjwFcAuCQFM5QJYQA5hCSgEMA7UsJYpLMUgZwChRISAjAY2zwKIEEAQgGEANO3pIQxDkghokACzAATZuGhIAnriykA5t3yEo6AG5piAGxhbcaRjiNEppZZEaMOl4vXpQBPmgAFIJCAJQAXIxQMVAAAsRsElIycooq0bHKaMBQpMRyQfRolsBhUAC0AHxQyRFQAHRNHrFQ2blwpGgA7nBBXvTl1VAAYmBgUAA%2B-sQgUa2tWLkD2H4BxfMLWysAvCZkbhANHEpYHMEA2mNgYgEgALphmVs%2BxcZYvgic3r79lvRiILXW6zMJPLYxdAoECkKBgNgAKzQMganR6fQGT08Pz81yC6zQkWebRyeQKwWKpSGNTqxNaUNQsIARMBxkyPF5XjMQPjAkTWu0yYVKWVKjTJJstgyYVAmWxZuzGGwoHsCUEnmYLNZbME2GEgA)

**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.

Contributor guide

Open the contributing guide

Research direction

Start with the linked Pyright Playground examples and the reportAbstractUsage check named in the issue. Reproduce the abstract-class diagnostic, then trace how __new__ return types are considered. Done means concrete classes or unions returned by __new__ no longer trigger the diagnostic, while genuinely abstract instantiation still does.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.