microsoft / pyright

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

@overload fails for `__init_subclass__` when >=2 arguments are present #8436

Closed brentyi closed 1 month ago

brentyi commented 1 month ago

Followup to #8343. The example there seems fixed in the newest (1.1.371) release of pyright, but just adding an extra argument seems to break it:

from typing import overload

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

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

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

class Subclass2(Base2, x=3, y=3): ...  # An unexpected pyright error!

Where on the last line we get the errors:

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 "y" of type "str" in function "__init_subclass__"

I'm using Pyright 1.1.371, Python 3.12.

erictraut commented 1 month ago

Yeah, the problem was more subtle than I previously realized, and my last fix was incomplete.

A more complete fix will be included in the next release.

brentyi commented 1 month ago

Thanks Eric!!

erictraut commented 1 month ago

This is included in pyright 1.1.372.