From b5e5cb419a672134f57755cf58ccd06665f25978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sat, 18 Jan 2025 00:58:32 +0000 Subject: [PATCH] Add typing for context managers returning Self `Self` was introduced in python 3.11. Use `Self` from typing_extensions for previous Python versions --- sphinxtools/utilities.py | 6 +++++- wx/lib/busy.py | 8 +++++++- wx/lib/plot/utils.py | 9 ++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/sphinxtools/utilities.py b/sphinxtools/utilities.py index e218ff90..d330bd49 100644 --- a/sphinxtools/utilities.py +++ b/sphinxtools/utilities.py @@ -32,6 +32,10 @@ from .constants import CPP_ITEMS, VERSION, VALUE_MAP from .constants import RE_KEEP_SPACES, EXTERN_INHERITANCE from .constants import DOXYROOT, SPHINXROOT, WIDGETS_IMAGES_ROOT +if sys.version_info >= (3, 11): + from typing import Self +else: + from typing_extensions import Self # ----------------------------------------------------------------------- # @@ -622,7 +626,7 @@ class PickleFile(object): def __init__(self, fileName): self.fileName = fileName - def __enter__(self): + def __enter__(self) -> Self: self.read() return self diff --git a/wx/lib/busy.py b/wx/lib/busy.py index 49e7a26c..d0a43543 100644 --- a/wx/lib/busy.py +++ b/wx/lib/busy.py @@ -13,9 +13,15 @@ A class like :class:`wx.BusyInfo` but which doesn't take up so much space by def and which has a nicer look. """ +import sys import wx from wx.lib.stattext import GenStaticText as StaticText +if sys.version_info >= (3, 11): + from typing import Self +else: + from typing_extensions import Self + #--------------------------------------------------------------------------- @@ -68,7 +74,7 @@ class BusyInfo(object): # Magic methods for using this class as a Context Manager - def __enter__(self): + def __enter__(self) -> Self: return self def __exit__(self, exc_type, exc_val, exc_tb): self.Close() diff --git a/wx/lib/plot/utils.py b/wx/lib/plot/utils.py index 202bcfa6..010ed83b 100644 --- a/wx/lib/plot/utils.py +++ b/wx/lib/plot/utils.py @@ -13,6 +13,7 @@ This is a collection of utilities used by the :mod:`wx.lib.plot` package. __docformat__ = "restructuredtext en" # Standard Library +import sys import functools import inspect import itertools @@ -22,6 +23,12 @@ from warnings import warn as _warn import wx import numpy as np +if sys.version_info >= (3, 11): + from typing import Self +else: + from typing_extensions import Self + + class PlotPendingDeprecation(wx.wxPyDeprecationWarning): pass @@ -195,7 +202,7 @@ class TempStyle(object): return wrapper - def __enter__(self): + def __enter__(self) -> Self: self._save_items(self.dc) return self