From d3398850c4b5bfb84b189b481f1786d4cc9c1dd2 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Tue, 28 Nov 2023 13:21:16 -0800 Subject: [PATCH] Fixed documentation for class and instance variable overrides. --- docs/type-concepts-advanced.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/type-concepts-advanced.md b/docs/type-concepts-advanced.md index 69e072c2d..fdcd5be1f 100644 --- a/docs/type-concepts-advanced.md +++ b/docs/type-concepts-advanced.md @@ -455,7 +455,7 @@ class Child(Parent): y = None # Error: Incompatible type ``` -The derived class can redeclare the type of a class or instance variable. If `reportIncompatibleVariableOverride` is enabled, the redeclared type must be the same as the type declared by the parent class or a subtype thereof. +The derived class can redeclare the type of a class or instance variable. If `reportIncompatibleVariableOverride` is enabled, the redeclared type must be the same as the type declared by the parent class. If the variable is immutable (as in a frozen `dataclass`), it is considered covariant, and it can be redeclared as a subtype of the type declared by the parent class. ```python class Parent: @@ -463,7 +463,7 @@ class Parent: y: int class Child(Parent): - x: int # This is OK because 'int' is a subtype of 'int | str | None' + x: int # Type error: 'x' cannot be redeclared with subtype because variable is mutable and therefore invariant y: str # Type error: 'y' cannot be redeclared with an incompatible type ```