Skip to content

Commit d0558cb

Browse files
Adopt "future" behavior of statement() and frame()
1 parent 698a376 commit d0558cb

File tree

3 files changed

+15
-65
lines changed

3 files changed

+15
-65
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ Release date: TBA
146146

147147
Refs #2141
148148

149+
* ``frame()`` raises ``ParentMissingError`` and ``statement()`` raises ``StatementMissing`` for
150+
missing parents regardless of the value of the ``future`` argument (which gave this behavior
151+
already).
152+
153+
Refs #1217
154+
149155
* Remove deprecated ``Ellipsis``, ``ExtSlice``, ``Index`` nodes.
150156

151157
Refs #2152

astroid/nodes/node_ng.py

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -288,39 +288,15 @@ def parent_of(self, node) -> bool:
288288
"""
289289
return any(self is parent for parent in node.node_ancestors())
290290

291-
@overload
292-
def statement(self, *, future: None = ...) -> nodes.Statement | nodes.Module:
293-
...
294-
295-
@overload
296-
def statement(self, *, future: Literal[True]) -> nodes.Statement:
297-
...
298-
299-
def statement(
300-
self, *, future: Literal[None, True] = None
301-
) -> nodes.Statement | nodes.Module:
291+
def statement(self, *, future: Literal[None, True] = None) -> nodes.Statement:
302292
"""The first parent node, including self, marked as statement node.
303293
304-
TODO: Deprecate the future parameter and only raise StatementMissing and return
305-
nodes.Statement
306-
307-
:raises AttributeError: If self has no parent attribute
308-
:raises StatementMissing: If self has no parent attribute and future is True
294+
:raises StatementMissing: If self has no parent attribute.
309295
"""
310296
if self.is_statement:
311297
return cast("nodes.Statement", self)
312298
if not self.parent:
313-
if future:
314-
raise StatementMissing(target=self)
315-
warnings.warn(
316-
"In astroid 3.0.0 NodeNG.statement() will return either a nodes.Statement "
317-
"or raise a StatementMissing exception. AttributeError will no longer be raised. "
318-
"This behaviour can already be triggered "
319-
"by passing 'future=True' to a statement() call.",
320-
DeprecationWarning,
321-
stacklevel=2,
322-
)
323-
raise AttributeError(f"{self} object has no attribute 'parent'")
299+
raise StatementMissing(target=self)
324300
return self.parent.statement(future=future)
325301

326302
def frame(
@@ -332,20 +308,10 @@ def frame(
332308
:class:`ClassDef` or :class:`Lambda`.
333309
334310
:returns: The first parent frame node.
311+
:raises ParentMissingError: If self has no parent attribute.
335312
"""
336313
if self.parent is None:
337-
if future:
338-
raise ParentMissingError(target=self)
339-
warnings.warn(
340-
"In astroid 3.0.0 NodeNG.frame() will return either a Frame node, "
341-
"or raise ParentMissingError. AttributeError will no longer be raised. "
342-
"This behaviour can already be triggered "
343-
"by passing 'future=True' to a frame() call.",
344-
DeprecationWarning,
345-
stacklevel=2,
346-
)
347-
raise AttributeError(f"{self} object has no attribute 'parent'")
348-
314+
raise ParentMissingError(target=self)
349315
return self.parent.frame(future=future)
350316

351317
def scope(self) -> nodes.LocalsDictNodeNG:

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import warnings
1717
from collections.abc import Generator, Iterable, Iterator, Sequence
1818
from functools import cached_property, lru_cache
19-
from typing import TYPE_CHECKING, ClassVar, Literal, NoReturn, TypeVar, overload
19+
from typing import TYPE_CHECKING, ClassVar, Literal, NoReturn, TypeVar
2020

2121
from astroid import bases, util
2222
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PYPY_7_3_11_PLUS
@@ -377,34 +377,12 @@ def fully_defined(self) -> bool:
377377
"""
378378
return self.file is not None and self.file.endswith(".py")
379379

380-
@overload
381-
def statement(self, *, future: None = ...) -> Module:
382-
...
383-
384-
@overload
385-
def statement(self, *, future: Literal[True]) -> NoReturn:
386-
...
387-
388-
def statement(self, *, future: Literal[None, True] = None) -> Module | NoReturn:
380+
def statement(self, *, future: Literal[None, True] = None) -> NoReturn:
389381
"""The first parent node, including self, marked as statement node.
390382
391-
When called on a :class:`Module` with the future parameter this raises an error.
392-
393-
TODO: Deprecate the future parameter and only raise StatementMissing
394-
395-
:raises StatementMissing: If no self has no parent attribute and future is True
383+
When called on a :class:`Module` this raises a StatementMissing.
396384
"""
397-
if future:
398-
raise StatementMissing(target=self)
399-
warnings.warn(
400-
"In astroid 3.0.0 NodeNG.statement() will return either a nodes.Statement "
401-
"or raise a StatementMissing exception. nodes.Module will no longer be "
402-
"considered a statement. This behaviour can already be triggered "
403-
"by passing 'future=True' to a statement() call.",
404-
DeprecationWarning,
405-
stacklevel=2,
406-
)
407-
return self
385+
raise StatementMissing(target=self)
408386

409387
def previous_sibling(self):
410388
"""The previous sibling statement.

0 commit comments

Comments
 (0)