Skip to content

Commit

Permalink
Fixed documentation for class and instance variable overrides.
Browse files Browse the repository at this point in the history
  • Loading branch information
erictraut committed Nov 28, 2023
1 parent 5471f6c commit d339885
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/type-concepts-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,15 @@ 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:
x: int | str | None
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
```

Expand Down

0 comments on commit d339885

Please sign in to comment.