Skip to content

Commit

Permalink
Add typing for context managers returning Self
Browse files Browse the repository at this point in the history
`Self` was introduced in python 3.11. Use `Self` from typing_extensions for previous Python versions
  • Loading branch information
echoix committed Jan 18, 2025
1 parent 169aacf commit fee14c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion sphinxtools/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
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

# ----------------------------------------------------------------------- #

Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion wx/lib/busy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
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

#---------------------------------------------------------------------------


Expand Down Expand Up @@ -68,7 +74,7 @@ def Close(self):


# 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()
Expand Down
9 changes: 8 additions & 1 deletion wx/lib/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
__docformat__ = "restructuredtext en"

# Standard Library
import sys
import functools
import inspect
import itertools
Expand All @@ -22,6 +23,12 @@
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

Expand Down Expand Up @@ -195,7 +202,7 @@ def wrapper(instance, dc, *args, **kwargs):

return wrapper

def __enter__(self):
def __enter__(self) -> Self:
self._save_items(self.dc)
return self

Expand Down

0 comments on commit fee14c6

Please sign in to comment.