Skip to content

Commit b4ac0e2

Browse files
authored
Remove newstyle variable given old-style class support should be removed (#2561)
1 parent 523eeb4 commit b4ac0e2

File tree

7 files changed

+13
-113
lines changed

7 files changed

+13
-113
lines changed

astroid/helpers.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ def _object_type(
6767

6868
for inferred in node.infer(context=context):
6969
if isinstance(inferred, scoped_nodes.ClassDef):
70-
if inferred.newstyle:
71-
metaclass = inferred.metaclass(context=context)
72-
if metaclass:
73-
yield metaclass
74-
continue
70+
metaclass = inferred.metaclass(context=context)
71+
if metaclass:
72+
yield metaclass
73+
continue
7574
yield builtins.getattr("type")[0]
7675
elif isinstance(
7776
inferred,
@@ -194,8 +193,6 @@ def _type_check(type1, type2) -> bool:
194193
if not all(map(has_known_bases, (type1, type2))):
195194
raise _NonDeducibleTypeHierarchy
196195

197-
if not all([type1.newstyle, type2.newstyle]):
198-
return False
199196
try:
200197
return type1 in type2.mro()[:-1]
201198
except MroError as e:

astroid/interpreter/objectmodel.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -514,19 +514,13 @@ def attr___doc__(self):
514514

515515
@property
516516
def attr___mro__(self):
517-
if not self._instance.newstyle:
518-
raise AttributeInferenceError(target=self._instance, attribute="__mro__")
519-
520517
mro = self._instance.mro()
521518
obj = node_classes.Tuple(parent=self._instance)
522519
obj.postinit(mro)
523520
return obj
524521

525522
@property
526523
def attr_mro(self):
527-
if not self._instance.newstyle:
528-
raise AttributeInferenceError(target=self._instance, attribute="mro")
529-
530524
other_self = self
531525

532526
# Cls.mro is a method and we need to return one in order to have a proper inference.
@@ -565,10 +559,6 @@ def attr___subclasses__(self):
565559
This looks only in the current module for retrieving the subclasses,
566560
thus it might miss a couple of them.
567561
"""
568-
if not self._instance.newstyle:
569-
raise AttributeInferenceError(
570-
target=self._instance, attribute="__subclasses__"
571-
)
572562

573563
qname = self._instance.qname()
574564
root = self._instance.root()

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,8 +1882,7 @@ def my_meth(self, arg):
18821882
),
18831883
)
18841884
_other_fields = ("name", "is_dataclass", "position")
1885-
_other_other_fields = ("locals", "_newstyle")
1886-
_newstyle: bool | None = None
1885+
_other_other_fields = "locals"
18871886

18881887
def __init__(
18891888
self,
@@ -1983,36 +1982,11 @@ def postinit(
19831982
self.bases = bases
19841983
self.body = body
19851984
self.decorators = decorators
1986-
self._newstyle = newstyle
19871985
self._metaclass = metaclass
19881986
self.position = position
19891987
self.doc_node = doc_node
19901988
self.type_params = type_params or []
19911989

1992-
def _newstyle_impl(self, context: InferenceContext | None = None):
1993-
if context is None:
1994-
context = InferenceContext()
1995-
if self._newstyle is not None:
1996-
return self._newstyle
1997-
for base in self.ancestors(recurs=False, context=context):
1998-
if base._newstyle_impl(context):
1999-
self._newstyle = True
2000-
break
2001-
klass = self.declared_metaclass()
2002-
# could be any callable, we'd need to infer the result of klass(name,
2003-
# bases, dict). punt if it's not a class node.
2004-
if klass is not None and isinstance(klass, ClassDef):
2005-
self._newstyle = klass._newstyle_impl(context)
2006-
if self._newstyle is None:
2007-
self._newstyle = False
2008-
return self._newstyle
2009-
2010-
_newstyle = None
2011-
newstyle = property(
2012-
_newstyle_impl,
2013-
doc=("Whether this is a new style class or not\n\n" ":type: bool or None"),
2014-
)
2015-
20161990
@cached_property
20171991
def blockstart_tolineno(self):
20181992
"""The line on which the beginning of this block ends.
@@ -2033,14 +2007,12 @@ def block_range(self, lineno: int) -> tuple[int, int]:
20332007
"""
20342008
return self.fromlineno, self.tolineno
20352009

2036-
def pytype(self) -> Literal["builtins.type", "builtins.classobj"]:
2010+
def pytype(self) -> Literal["builtins.type"]:
20372011
"""Get the name of the type that this node represents.
20382012
20392013
:returns: The name of the type.
20402014
"""
2041-
if self.newstyle:
2042-
return "builtins.type"
2043-
return "builtins.classobj"
2015+
return "builtins.type"
20442016

20452017
def display_type(self) -> str:
20462018
"""A human readable type of this node.
@@ -2580,7 +2552,6 @@ def _valid_getattr(node):
25802552
try:
25812553
return _valid_getattr(self.getattr("__getattr__", context)[0])
25822554
except AttributeInferenceError:
2583-
# if self.newstyle: XXX cause an infinite recursion error
25842555
try:
25852556
getattribute = self.getattr("__getattribute__", context)[0]
25862557
return _valid_getattr(getattribute)
@@ -2667,16 +2638,12 @@ def mymethods(self):
26672638
def implicit_metaclass(self):
26682639
"""Get the implicit metaclass of the current class.
26692640
2670-
For newstyle classes, this will return an instance of builtins.type.
2671-
For oldstyle classes, it will simply return None, since there's
2672-
no implicit metaclass there.
2641+
This will return an instance of builtins.type.
26732642
26742643
:returns: The metaclass.
2675-
:rtype: builtins.type or None
2644+
:rtype: builtins.type
26762645
"""
2677-
if self.newstyle:
2678-
return builtin_lookup("type")[1][0]
2679-
return None
2646+
return builtin_lookup("type")[1][0]
26802647

26812648
def declared_metaclass(
26822649
self, context: InferenceContext | None = None
@@ -2799,10 +2766,6 @@ def _islots(self):
27992766
return None
28002767

28012768
def _slots(self):
2802-
if not self.newstyle:
2803-
raise NotImplementedError(
2804-
"The concept of slots is undefined for old-style classes."
2805-
)
28062769

28072770
slots = self._islots()
28082771
try:
@@ -2842,11 +2805,6 @@ def grouped_slots(
28422805
else:
28432806
yield None
28442807

2845-
if not self.newstyle:
2846-
raise NotImplementedError(
2847-
"The concept of slots is undefined for old-style classes."
2848-
)
2849-
28502808
try:
28512809
mro = self.mro()
28522810
except MroError as e:
@@ -2912,17 +2870,8 @@ def _compute_mro(self, context: InferenceContext | None = None):
29122870
if base is self:
29132871
continue
29142872

2915-
try:
2916-
mro = base._compute_mro(context=context)
2917-
bases_mro.append(mro)
2918-
except NotImplementedError:
2919-
# Some classes have in their ancestors both newstyle and
2920-
# old style classes. For these we can't retrieve the .mro,
2921-
# although in Python it's possible, since the class we are
2922-
# currently working is in fact new style.
2923-
# So, we fallback to ancestors here.
2924-
ancestors = list(base.ancestors(context=context))
2925-
bases_mro.append(ancestors)
2873+
mro = base._compute_mro(context=context)
2874+
bases_mro.append(mro)
29262875

29272876
unmerged_mro: list[list[ClassDef]] = [[self], *bases_mro, inferred_bases]
29282877
unmerged_mro = clean_duplicates_mro(unmerged_mro, self, context)

astroid/objects.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ def super_mro(self):
109109
super_=self,
110110
)
111111

112-
if not mro_type.newstyle:
113-
raise SuperError("Unable to call super on old-style classes.", super_=self)
114-
115112
mro = mro_type.mro()
116113
if self.mro_pointer not in mro:
117114
raise SuperError(

tests/test_builder.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -519,36 +519,6 @@ def test_object(self) -> None:
519519
obj_ast = self.builder.inspect_build(object)
520520
self.assertIn("__setattr__", obj_ast)
521521

522-
def test_newstyle_detection(self) -> None:
523-
data = """
524-
class A:
525-
"old style"
526-
527-
class B(A):
528-
"old style"
529-
530-
class C(object):
531-
"new style"
532-
533-
class D(C):
534-
"new style"
535-
536-
__metaclass__ = type
537-
538-
class E(A):
539-
"old style"
540-
541-
class F:
542-
"new style"
543-
"""
544-
mod_ast = builder.parse(data, __name__)
545-
self.assertTrue(mod_ast["A"].newstyle)
546-
self.assertTrue(mod_ast["B"].newstyle)
547-
self.assertTrue(mod_ast["E"].newstyle)
548-
self.assertTrue(mod_ast["C"].newstyle)
549-
self.assertTrue(mod_ast["D"].newstyle)
550-
self.assertTrue(mod_ast["F"].newstyle)
551-
552522
def test_globals(self) -> None:
553523
data = """
554524
CSTE = 1
@@ -833,7 +803,6 @@ def test_class_base_props(self) -> None:
833803
self.assertEqual(klass.parent.frame(), module)
834804
self.assertEqual(klass.root(), module)
835805
self.assertEqual(klass.basenames, [])
836-
self.assertTrue(klass.newstyle)
837806

838807
def test_class_locals(self) -> None:
839808
"""Test the 'locals' dictionary of an astroid class."""

tests/test_python3.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ class SubTest(Test): pass
155155
)
156156
)
157157
klass = astroid["SubTest"]
158-
self.assertTrue(klass.newstyle)
159158
metaclass = klass.metaclass()
160159
self.assertIsInstance(metaclass, nodes.ClassDef)
161160
self.assertEqual(metaclass.name, "ABCMeta")

tests/test_scoped_nodes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,7 @@ def test_cls_special_attributes_1(self) -> None:
10021002
self.assertIsInstance(cls.getattr("__module__")[0], nodes.Const)
10031003
self.assertEqual(cls.getattr("__module__")[0].value, "data.module")
10041004
self.assertEqual(len(cls.getattr("__dict__")), 1)
1005-
if not cls.newstyle:
1006-
self.assertRaises(AttributeInferenceError, cls.getattr, "__mro__")
1005+
10071006
for cls in (nodes.List._proxied, nodes.Const(1)._proxied):
10081007
self.assertEqual(len(cls.getattr("__bases__")), 1)
10091008
self.assertEqual(len(cls.getattr("__name__")), 1)

0 commit comments

Comments
 (0)