Closed joshMaybell closed 3 hours ago
Pyright's behavior here is correct. There are potentially an infinite number of classes that derive from Base
, so exhaustion cannot be statically verified.
Your options are:
reportMatchNotExhaustive
check in your project# type: ignore
or # pragma: ignore
commenttest
accepts
type ChildTypes = ChildA | ChildB
def test(obj: ChildTypes):
...
My recommendation is option 4. I use this technique frequently in my code.
Makes sense. Thanks for the suggestions!
Describe the bug
When using a match statement to test the type of an object, pyright expects cases even for non-instantiable abstract base classes.
Code or Screenshots
The above snippet causes a
reportMatchNotExhaustive
flag to be raised in thetest
function. Modifying the match block to include a branch forBase
resolves the error:Of course, attempting to call
test
with an instance of typeBase
is not possible because such an object cannot be created.This issue also exists when using typing.Protocol instead of abc.ABC. However, in this case the inclusion of the
case Base()
branch results in an additionalreportGeneralTypeIssues
flag because Base is not@runtime_checkable
.VS Code extension or command-line The above snippets result in the described behavior in at least the following environments (tested only in strict mode):
python 3.11.10: coc-pyright version 1.1.380 pyright (vscode) version 1.1.385 pylance (vscode) version 2024.10.1 pyright (cli) version 1.1.380
Python 3.12.3: pyright (cli) version 1.1.385