Skip to content

Commit 4a574c2

Browse files
authored
Add some workarounds to simplify bootstrap (#5614)
Also a tiny refactoring.
1 parent fe0f933 commit 4a574c2

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

mypy/checker.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import mypy.checkexpr
3838
from mypy.checkmember import (
3939
map_type_from_supertype, bind_self, erase_to_bound, type_object_type,
40-
analyze_descriptor_access
40+
analyze_descriptor_access, is_final_node
4141
)
4242
from mypy import messages
4343
from mypy.subtypes import (
@@ -1263,8 +1263,7 @@ def check_method_or_accessor_override_for_base(self, defn: Union[FuncBase, Decor
12631263
base_attr = base.names.get(name)
12641264
if base_attr:
12651265
# First, check if we override a final (always an error, even with Any types).
1266-
if (isinstance(base_attr.node, (Var, FuncBase, Decorator))
1267-
and base_attr.node.is_final):
1266+
if is_final_node(base_attr.node):
12681267
self.msg.cant_override_final(name, base.name(), defn)
12691268
# Second, final can't override anything writeable independently of types.
12701269
if defn.is_final:
@@ -1330,10 +1329,8 @@ def check_method_override_for_base_with_name(
13301329
if isinstance(original_type, AnyType) or isinstance(typ, AnyType):
13311330
pass
13321331
elif isinstance(original_type, FunctionLike) and isinstance(typ, FunctionLike):
1333-
# mypyc hack to workaround mypy misunderstanding multiple inheritance (#3603)
1334-
base_attr_node = base_attr.node # type: Any
1335-
if (isinstance(base_attr_node, (FuncBase, Decorator))
1336-
and not is_static(base_attr_node)):
1332+
if (isinstance(base_attr.node, (FuncDef, OverloadedFuncDef, Decorator))
1333+
and not is_static(base_attr.node)):
13371334
bound = bind_self(original_type, self.scope.active_self_type())
13381335
else:
13391336
bound = original_type
@@ -1587,9 +1584,9 @@ def check_compatibility(self, name: str, base1: TypeInfo,
15871584
ok = True
15881585
# Final attributes can never be overridden, but can override
15891586
# non-final read-only attributes.
1590-
if isinstance(second.node, (Var, FuncBase, Decorator)) and second.node.is_final:
1587+
if is_final_node(second.node):
15911588
self.msg.cant_override_final(name, base2.name(), ctx)
1592-
if isinstance(first.node, (Var, FuncBase, Decorator)) and first.node.is_final:
1589+
if is_final_node(first.node):
15931590
self.check_no_writable(name, second.node, ctx)
15941591
# __slots__ is special and the type can vary across class hierarchy.
15951592
if name == '__slots__':

mypy/checkmember.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def check_final_member(name: str, info: TypeInfo, msg: MessageBuilder, ctx: Cont
314314
"""Give an error if the name being assigned was declared as final."""
315315
for base in info.mro:
316316
sym = base.names.get(name)
317-
if sym and isinstance(sym.node, (Var, FuncBase, Decorator)) and sym.node.is_final:
317+
if sym and is_final_node(sym.node):
318318
msg.cant_assign_to_final(name, attr_assign=True, ctx=ctx)
319319

320320

@@ -839,3 +839,8 @@ def erase_to_bound(t: Type) -> Type:
839839
if isinstance(t.item, TypeVarType):
840840
return TypeType.make_normalized(t.item.upper_bound)
841841
return t
842+
843+
844+
def is_final_node(node: Optional[SymbolNode]) -> bool:
845+
"""Check whether `node` corresponds to a final attribute."""
846+
return isinstance(node, (Var, FuncDef, OverloadedFuncDef, Decorator)) and node.is_final

0 commit comments

Comments
 (0)