Skip to content

Commit 7c5a719

Browse files
plun1331pre-commit-ci[bot]CopilotNeloBlivionPaillat-dev
authored
refactor: rework how item.view is determined (#2981)
* feat: rework how item.view is determined * docs: changelog * style(pre-commit): auto fixes from pre-commit.com hooks * Update discord/ui/view.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: plun1331 <plun1331@gmail.com> * remove useless stuff * fuckin circles man * style(pre-commit): auto fixes from pre-commit.com hooks * Update discord/ui/item.py Signed-off-by: Paillat <jeremiecotti@ik.me> * Annotate return type for remove_item method Added return type annotation for remove_item method. Signed-off-by: Paillat <paillat@pycord.dev> --------- Signed-off-by: plun1331 <plun1331@gmail.com> Signed-off-by: Nelo <41271523+NeloBlivion@users.noreply.github.com> Signed-off-by: Paillat <paillat@pycord.dev> Signed-off-by: Paillat <jeremiecotti@ik.me> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Nelo <41271523+NeloBlivion@users.noreply.github.com> Co-authored-by: Paillat <paillat@pycord.dev>
1 parent 2074aea commit 7c5a719

File tree

6 files changed

+35
-40
lines changed

6 files changed

+35
-40
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ These changes are available on the `master` branch, but have not yet been releas
2222

2323
### Fixed
2424

25+
- Fixed the `view` attribute on many view items being incorrect.
26+
([#2981](https://github.com/Pycord-Development/pycord/pull/2981))
2527
- Fixed `TypeError` in paginator implementation when only passing `PageGroup` objects
2628
and `show_menu` is falsy.
2729
([#2993](https://github.com/Pycord-Development/pycord/pull/2993))
@@ -32,6 +34,9 @@ These changes are available on the `master` branch, but have not yet been releas
3234

3335
### Deprecated
3436

37+
- Deprecated manually setting the `view` attribute on view items.
38+
([#2981](https://github.com/Pycord-Development/pycord/pull/2981))
39+
3540
### Removed
3641

3742
- ⚠️ **Removed support for Python 3.9.**

discord/ui/action_row.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ def add_item(self, item: ViewItem) -> Self:
140140
if self.width + item.width > 5:
141141
raise ValueError(f"Not enough space left on this ActionRow")
142142

143-
item._view = self.view
144143
item.parent = self
145144

146145
self.children.append(item)
@@ -162,6 +161,7 @@ def remove_item(self, item: ViewItem | str | int) -> Self:
162161
self.children.remove(item)
163162
except ValueError:
164163
pass
164+
item.parent = None
165165
return self
166166

167167
def get_item(self, id: str | int) -> ViewItem | None:
@@ -351,13 +351,6 @@ def add_select(
351351

352352
return self.add_item(select)
353353

354-
@ViewItem.view.setter
355-
def view(self, value):
356-
self._view = value
357-
for item in self.children:
358-
item.parent = self
359-
item._view = value
360-
361354
def is_dispatchable(self) -> bool:
362355
return any(item.is_dispatchable() for item in self.children)
363356

discord/ui/container.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,6 @@ def add_item(self, item: ViewItem) -> Self:
149149
f"{item.__class__!r} cannot be added directly. Use ActionRow instead."
150150
)
151151

152-
item._view = self.view
153-
if hasattr(item, "items"):
154-
item.view = self
155152
item.parent = self
156153

157154
self.items.append(item)
@@ -170,12 +167,13 @@ def remove_item(self, item: ViewItem | str | int) -> Self:
170167
if isinstance(item, (str, int)):
171168
item = self.get_item(item)
172169
try:
173-
if isinstance(item, Container):
170+
if item.parent is self:
174171
self.items.remove(item)
175172
else:
176173
item.parent.remove_item(item)
177174
except ValueError:
178175
pass
176+
item.parent = None
179177
return self
180178

181179
def get_item(self, id: str | int) -> ViewItem | None:
@@ -361,15 +359,6 @@ def colour(self, value: int | Colour | None): # type: ignore
361359

362360
color = colour
363361

364-
@ViewItem.view.setter
365-
def view(self, value):
366-
self._view = value
367-
for item in self.items:
368-
item.parent = self
369-
item._view = value
370-
if hasattr(item, "items") or hasattr(item, "children"):
371-
item.view = value
372-
373362
def is_dispatchable(self) -> bool:
374363
return any(item.is_dispatchable() for item in self.items)
375364

discord/ui/item.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"ModalItem",
3636
)
3737

38+
from ..utils import warn_deprecated
39+
3840
if TYPE_CHECKING:
3941
from ..components import Component
4042
from ..enums import ComponentType
@@ -152,7 +154,7 @@ def __init__(self):
152154
self._view: V | None = None
153155
self._row: int | None = None
154156
self._rendered_row: int | None = None
155-
self.parent: ViewItem | BaseView | None = self.view
157+
self.parent: ViewItem | BaseView | None = None
156158

157159
@property
158160
def row(self) -> int | None:
@@ -208,10 +210,19 @@ def view(self) -> V | None:
208210
Optional[:class:`BaseView`]
209211
The parent view of this item, or ``None`` if the item is not attached to any view.
210212
"""
211-
return self._view
213+
if self._view:
214+
return self._view
215+
if self.parent:
216+
from .view import BaseView
217+
218+
if isinstance(self.parent, BaseView):
219+
return self.parent
220+
return self.parent.view
221+
return None
212222

213223
@view.setter
214-
def view(self, value) -> None:
224+
def view(self, value: V | None) -> None:
225+
warn_deprecated("Manually setting .view", since="2.7", removed="3.0")
215226
self._view = value
216227

217228
async def callback(self, interaction: Interaction):

discord/ui/section.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def remove_item(self, item: ViewItem | str | int) -> Self:
162162
self.items.remove(item)
163163
except ValueError:
164164
pass
165+
item.parent = None
165166
return self
166167

167168
def get_item(self, id: int | str) -> ViewItem | None:
@@ -226,8 +227,7 @@ def set_accessory(self, item: ViewItem) -> Self:
226227

227228
if not isinstance(item, ViewItem):
228229
raise TypeError(f"expected ViewItem not {item.__class__!r}")
229-
if self.view:
230-
item._view = self.view
230+
231231
item.parent = self
232232

233233
self.accessory = item
@@ -260,13 +260,6 @@ def set_thumbnail(
260260

261261
return self.set_accessory(thumbnail)
262262

263-
@ViewItem.view.setter
264-
def view(self, value):
265-
self._view = value
266-
for item in self.walk_items():
267-
item._view = value
268-
item.parent = self
269-
270263
def copy_text(self) -> str:
271264
"""Returns the text of all :class:`~discord.ui.TextDisplay` items in this section.
272265
Equivalent to the `Copy Text` option on Discord clients.

discord/ui/view.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,33 +236,40 @@ def add_item(self, item: ViewItem[V]) -> Self:
236236
raise TypeError(f"expected ViewItem not {item.__class__!r}")
237237

238238
item.parent = self
239-
item._view = self
240239
self.children.append(item)
241240
return self
242241

243-
def remove_item(self, item: ViewItem[V] | int | str) -> None:
242+
def remove_item(self, item: ViewItem[V] | int | str) -> Self:
244243
"""Removes an item from the view. If an :class:`int` or :class:`str` is passed,
245244
the item will be removed by ViewItem ``id`` or ``custom_id`` respectively.
246245
247246
Parameters
248247
----------
249248
item: Union[:class:`ViewItem`, :class:`int`, :class:`str`]
250249
The item, item ``id``, or item ``custom_id`` to remove from the view.
250+
251+
Returns
252+
-------
253+
:class:`BaseView`
254+
The view instance.
251255
"""
252256

253257
if isinstance(item, (str, int)):
254258
item = self.get_item(item)
255259
try:
256-
if isinstance(item.parent, BaseView):
260+
if item.parent is self:
257261
self.children.remove(item)
258262
else:
259263
item.parent.remove_item(item)
260264
except ValueError:
261265
pass
266+
item.parent = None
262267
return self
263268

264-
def clear_items(self) -> None:
269+
def clear_items(self) -> Self:
265270
"""Removes all items from this view."""
271+
for child in self.children:
272+
child.parent = None
266273
self.children.clear()
267274
return self
268275

@@ -580,7 +587,6 @@ def __init__(
580587
**func.__discord_ui_model_kwargs__
581588
)
582589
item.callback = partial(func, self, item)
583-
item._view = self
584590
item.parent = self
585591
setattr(self, func.__name__, item)
586592
self.children.append(item)
@@ -887,8 +893,6 @@ def add_item(self, item: ViewItem[V]) -> Self:
887893
)
888894

889895
super().add_item(item)
890-
if hasattr(item, "items"):
891-
item.view = self
892896
return self
893897

894898
def refresh(self, components: list[Component]):

0 commit comments

Comments
 (0)