Skip to content

Inferred subclass override of a ClassVar is also a ClassVar. Fixes #4715. #4718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,7 @@ def analyze_lvalue(self, lval: Lvalue, nested: bool = False,
v = Var(lval.name)
v.info = self.type
v.is_initialized_in_class = True
v.is_inferred = not explicit_type
v.set_line(lval)
v._fullname = self.qualified_name(lval.name)
lval.node = v
Expand Down
15 changes: 14 additions & 1 deletion mypy/semanal_pass3.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
Node, Expression, MypyFile, FuncDef, FuncItem, Decorator, RefExpr, Context, TypeInfo, ClassDef,
Block, TypedDictExpr, NamedTupleExpr, AssignmentStmt, IndexExpr, TypeAliasExpr, NameExpr,
CallExpr, NewTypeExpr, ForStmt, WithStmt, CastExpr, TypeVarExpr, TypeApplication, Lvalue,
TupleExpr, RevealTypeExpr, SymbolTableNode, SymbolTable, Var, ARG_POS, OverloadedFuncDef
TupleExpr, RevealTypeExpr, SymbolTableNode, SymbolTable, Var, ARG_POS, OverloadedFuncDef,
MDEF,
)
from mypy.types import (
Type, Instance, AnyType, TypeOfAny, CallableType, TupleType, TypeVarType, TypedDictType,
Expand Down Expand Up @@ -258,6 +259,18 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
node = self.sem.lookup(s.lvalues[0].name, s, suppress_errors=True)
if node:
self.analyze(node.type_override, node)
# Subclass attribute assignments with no type annotation should be
# assumed to be classvar if overriding a declared classvar from the base
# class.
if (isinstance(s.lvalues[0], NameExpr) and s.lvalues[0].kind == MDEF
and isinstance(s.lvalues[0].node, Var)):
var = s.lvalues[0].node
if var.info is not None and var.is_inferred and not var.is_classvar:
for base in var.info.mro[1:]:
tnode = base.names.get(var.name())
if (tnode is not None and isinstance(tnode.node, Var)
and tnode.node.is_classvar):
var.is_classvar = True
super().visit_assignment_stmt(s)

def visit_for_stmt(self, s: ForStmt) -> None:
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-classvar.test
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,22 @@ class A:
class B(A):
x = 2 # type: ClassVar[int]

[case testOverrideClassVarWithImplicitClassVar]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
class B(A):
x = 2

[case testOverrideClassVarWithImplicitThenExplicit]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
class B(A):
x = 2
class C(B):
x = 3 # type: ClassVar[int]

[case testOverrideOnABCSubclass]
from abc import ABCMeta
from typing import ClassVar
Expand Down