From 33acf3cb884bd07286402ecd19ac94ff43327c65 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Thu, 23 Jul 2020 07:34:58 -0700 Subject: [PATCH] Fixed bug in dataclass logic that reported spurious error when initializing attribute with `field(init=False)`. --- server/src/analyzer/typeEvaluator.ts | 1 + server/src/tests/samples/dataclass10.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/server/src/analyzer/typeEvaluator.ts b/server/src/analyzer/typeEvaluator.ts index c7586809a..f10ccc7c0 100644 --- a/server/src/analyzer/typeEvaluator.ts +++ b/server/src/analyzer/typeEvaluator.ts @@ -1776,6 +1776,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, printTypeFlags: (p) => p.hasDefault && p.includeInInit ); if ( + includeInInit && !hasDefaultValue && firstDefaultValueIndex >= 0 && firstDefaultValueIndex < insertIndex diff --git a/server/src/tests/samples/dataclass10.py b/server/src/tests/samples/dataclass10.py index 0c7262150..cfe2208c1 100644 --- a/server/src/tests/samples/dataclass10.py +++ b/server/src/tests/samples/dataclass10.py @@ -31,3 +31,15 @@ test = Child(prop_2="test", prop_4="hi") assert test.prop_1 == "test" assert test.prop_2 == "test" + +@dataclass +class HandshakeMessage: + reset_reason_hex: str + reset_data_hex: str + device_id: str = field(default="") + reset_reason: str = field(init=False) + reset_data: str = field(init=False) + + def __post_init__(self): + reset_reason = "calculated value" + reset_data = "calculated value"