microsoft / pyright

Static Type Checker for Python
Other
13.04k stars 1.39k forks source link

Only first overload of `__init_subclass__` is matched against #8343

Closed brentyi closed 1 month ago

brentyi commented 1 month ago

Hello!

I noticed today that @overload doesn't seem to work with __init_subclass__.

Here's a reproduction of the error. It passes mypy, but pyright reports errors on the last line:

from typing import overload

class Base:
    @overload
    def __init_subclass__(cls, x: str) -> None: ...

    @overload
    def __init_subclass__(cls, x: int) -> None: ...

    def __init_subclass__(cls, x: int | str) -> None: ...

class Subclass(Base, x=3): ...

The specific errors are:

error: Incorrect keyword arguments for __init_subclass__ method (reportGeneralTypeIssues)
error: No overloads for "__init_subclass__" match the provided arguments (reportCallIssue)
error: Argument of type "Literal[3]" cannot be assigned to parameter "x" of type "str" in function "__init_subclass__"
    "Literal[3]" is incompatible with "str" (reportArgumentType)

The error vanish if I swap the order of the two overloads, which seems to indicate that pyright is only matching against the first one.

Thanks!!

erictraut commented 1 month ago

Thanks for the bug report and clean repro steps.

This will be fixed in the next release.

erictraut commented 1 month ago

This is addressed in pyright 1.1.371.