From 94ce763b05e9eb60c755d8a707ca63d8900f2e8b Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Sat, 23 Dec 2023 22:47:10 +0000 Subject: [PATCH] Show attrs auto_detect incompleteness This test demonstrates that addition of dunder methods in attrs classes isn't quite complete. While the existing test demonstrates that it was working for classes defined with `init=False`, the `__attrs_init__` method wasn't generated in the same way when `__init__` was defined with `auto_detect=True` enabled (either explicitly, or by default). This auto-generated `__attrs_init__` has been enabled in Attrs as of version 21.1.0. Related to: https://github.com/python/mypy/issues/10328 Ref: https://github.com/python-attrs/attrs/issues/793 Ref: https://www.attrs.org/en/stable/changelog.html --- test-data/unit/check-plugin-attrs.test | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test-data/unit/check-plugin-attrs.test b/test-data/unit/check-plugin-attrs.test index 0f379724553a..2b701b237122 100644 --- a/test-data/unit/check-plugin-attrs.test +++ b/test-data/unit/check-plugin-attrs.test @@ -1656,6 +1656,42 @@ reveal_type(A.__attrs_init__) # N: Revealed type is "def (self: __main__.A, b: [builtins fixtures/plugin_attrs.pyi] +[case testAttrsInitMethodGeneratedWhenAutoDetectTrue] +from typing import Tuple +import attr + +@attr.define(auto_detect=True) +class A: + b: int + c: str + def __init__(self, bc: Tuple[int, str]) -> None: + b, c = bc + self.__attrs_init__(b, c) + +reveal_type(A) # N: Revealed type is "def (bc: Tuple[builtins.int, builtins.str]) -> __main__.A" +reveal_type(A.__init__) # N: Revealed type is "def (self: __main__.A, bc: Tuple[builtins.int, builtins.str])" +reveal_type(A.__attrs_init__) # N: Revealed type is "def (self: __main__.A, b: builtins.int, c: builtins.str)" + +[builtins fixtures/plugin_attrs.pyi] + +[case testAttrsInitMethodGeneratedWhenAutoDetectTrueDefault] +from typing import Tuple +import attr + +@attr.define +class A: + b: int + c: str + def __init__(self, bc: Tuple[int, str]) -> None: + b, c = bc + self.__attrs_init__(b, c) + +reveal_type(A) # N: Revealed type is "def (bc: Tuple[builtins.int, builtins.str]) -> __main__.A" +reveal_type(A.__init__) # N: Revealed type is "def (self: __main__.A, bc: Tuple[builtins.int, builtins.str])" +reveal_type(A.__attrs_init__) # N: Revealed type is "def (self: __main__.A, b: builtins.int, c: builtins.str)" + +[builtins fixtures/plugin_attrs.pyi] + [case testAttrsClassWithSlots] import attr