Skip to content

Commit 04766f7

Browse files
committed
Add unit tests to verify fix for python#19003 involving overloaded __init__ methods in attrs classes
1 parent 166776b commit 04766f7

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test-data/unit/check-plugin-attrs.test

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,3 +2496,63 @@ Parent(run_type = None)
24962496
c = Child(run_type = None)
24972497
reveal_type(c.run_type) # N: Revealed type is "Union[builtins.int, None]"
24982498
[builtins fixtures/plugin_attrs.pyi]
2499+
2500+
[case testAttrsInitOverload1]
2501+
from typing import overload
2502+
2503+
import attrs
2504+
2505+
@attrs.frozen(init=False)
2506+
class C:
2507+
x: "int | str"
2508+
2509+
@overload
2510+
def __init__(self, x: int) -> None: ...
2511+
2512+
@overload
2513+
def __init__(self, x: str) -> None: ...
2514+
2515+
def __init__(self, x: "int | str") -> None:
2516+
self.__attrs_init__(x)
2517+
2518+
2519+
obj = C(1)
2520+
attrs.evolve(obj, x=2)
2521+
attrs.evolve(obj, x="2")
2522+
[builtins fixtures/plugin_attrs.pyi]
2523+
2524+
[case testAttrsInitOverload2]
2525+
import attrs
2526+
2527+
@attrs.frozen(init=False)
2528+
class C:
2529+
x: "int | str"
2530+
2531+
def __init__(self, x: "int | str", y: bool) -> None:
2532+
self.__attrs_init__(x)
2533+
2534+
obj = C(1, False)
2535+
attrs.evolve(obj, x=2)
2536+
[builtins fixtures/plugin_attrs.pyi]
2537+
2538+
[case testAttrsInitOverload3]
2539+
from typing import overload
2540+
2541+
import attrs
2542+
2543+
@attrs.frozen(init=False)
2544+
class C:
2545+
x: int | str
2546+
2547+
@overload
2548+
def __init__(self, x: int) -> None: ...
2549+
2550+
@overload
2551+
def __init__(self, x: str) -> None: ...
2552+
2553+
def __init__(self, x: int | str) -> None:
2554+
self.__attrs_init__(x)
2555+
2556+
obj = C(1)
2557+
attrs.evolve(obj, x=2)
2558+
[builtins fixtures/plugin_attrs.pyi]

0 commit comments

Comments
 (0)