Skip to content

Commit 83638ef

Browse files
committed
fixup! Add new standards for deprecating APIs
1 parent afbc33f commit 83638ef

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

mkdocs/docs/contributing.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,19 @@ To deprecate functions, methods, or properties, use the `@deprecated` decorator.
168168
```python
169169
from pyiceberg.utils._deprecations import deprecated
170170

171+
171172
@deprecated("1.5.0", "2.0.0")
172173
def old_function():
173174
pass
175+
176+
177+
old_function()
174178
```
175179

176180
This will warn:
177181

178182
```text
179-
Call to old_function, deprecated in 1.5.0, will be removed in 2.0.0.
183+
some_file.py:9: DeprecationWarning: __main__.old_function is deprecated and will be removed in 2.0.0.
180184
```
181185

182186
#### Adding a Recommendation
@@ -186,15 +190,19 @@ Optionally, include a recommendation to guide users toward an alternative featur
186190
```python
187191
from pyiceberg.utils._deprecations import deprecated
188192

193+
189194
@deprecated("1.5.0", "2.0.0", addendum="Use `new_function` instead.")
190195
def old_function():
191196
pass
197+
198+
199+
old_function()
192200
```
193201

194202
This will warn:
195203

196204
```text
197-
Call to old_function, deprecated in 1.5.0, will be removed in 2.0.0. Use `new_function` instead.
205+
some_file.py:9: DeprecationWarning: __main__.old_function is deprecated and will be removed in 2.0.0. Use `new_function` instead.
198206
```
199207

200208
### Keyword Arguments
@@ -206,31 +214,39 @@ To deprecate or rename keyword arguments, use the `@deprecated.argument` decorat
206214
```python
207215
from pyiceberg.utils._deprecations import deprecated
208216

217+
209218
@deprecated.argument("1.5.0", "2.0.0", "old_arg")
210219
def my_function(*, old_arg=True):
211220
pass
221+
222+
223+
my_function(old_arg=False)
212224
```
213225

214226
This will warn:
215227

216228
```text
217-
Call to my_function(old_arg), deprecated in 1.5.0, will be removed in 2.0.0.
229+
some_file.py:9: DeprecationWarning: __main__.my_function(old_arg) is deprecated and will be removed in 2.0.0.
218230
```
219231

220232
#### Renaming a Keyword Argument
221233

222234
```python
223235
from pyiceberg.utils._deprecations import deprecated
224236

237+
225238
@deprecated.argument("1.5.0", "2.0.0", "old_arg", rename="new_arg")
226239
def my_function(*, new_arg=True):
227240
pass
241+
242+
243+
my_function(old_arg=False)
228244
```
229245

230246
This will warn:
231247

232248
```text
233-
Call to my_function(old_arg), deprecated in 1.5.0, will be removed in 2.0.0. Use `new_arg` instead.
249+
some_file.py:9: DeprecationWarning: __main__.my_function(old_arg) is deprecated and will be removed in 2.0.0. Use 'new_arg' instead.
234250
```
235251

236252
### Constants and Enums
@@ -240,34 +256,45 @@ To deprecate constants or enums, use the `deprecated.constant` function.
240256
#### Deprecating a Constant
241257

242258
```python
259+
# some_file.py
243260
from pyiceberg.utils._deprecations import deprecated
244261

245262
deprecated.constant("1.5.0", "2.0.0", "OLD_CONSTANT", 42)
263+
264+
# deprecated_constant.py
265+
from some_file import OLD_CONSTANT
266+
267+
OLD_CONSTANT
246268
```
247269

248270
This will warn:
249271

250272
```text
251-
OLD_CONSTANT, deprecated in 1.5.0, will be removed in 2.0.0.
273+
deprecated_constant.py:3: DeprecationWarning: some_file.OLD_CONSTANT is deprecated and will be removed in 2.0.0.
252274
```
253275

254276
#### Deprecating an Enum
255277

256278
```python
279+
# some_file.py
257280
from enum import Enum
258281
from pyiceberg.utils._deprecations import deprecated
259282

260283
class MyEnum(Enum):
261284
OLD_VALUE = 42
262285

263286
deprecated.constant("1.5.0", "2.0.0", "MyEnum", MyEnum)
287+
264288
del MyEnum
289+
290+
# deprecated_enum.py
291+
from some_file import MyEnum
265292
```
266293

267294
This will warn:
268295

269296
```text
270-
MyEnum, deprecated in 1.5.0, will be removed in 2.0.0.
297+
deprecated_constant.py:1: DeprecationWarning: some_file.MyEnum is deprecated and will be removed in 2.0.0.
271298
```
272299

273300
### Topics
@@ -277,19 +304,20 @@ For deprecations that do not fit into the above categories, use the `deprecated.
277304
```python
278305
from pyiceberg.utils._deprecations import deprecated
279306

307+
280308
def some_function():
281309
# some logic
282310

283311
if condition:
284-
deprecated.topic("1.5.0", "2.0.0", addendum="The <TOPIC>")
312+
deprecated.topic("1.5.0", "2.0.0", prefix="The behaviour xyz", addendum="Do something else.")
285313

286314
# more logic
287315
```
288316

289317
This will warn:
290318

291319
```text
292-
The <TOPIC>, deprecated in 1.5.0, will be removed in 2.0.0.
320+
some_file.py:8: DeprecationWarning: The behaviour xyz is deprecated and will be removed in 2.0.0. Do something else.
293321
```
294322

295323
## Type annotations

pyiceberg/utils/_deprecations.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
if TYPE_CHECKING:
4242
from argparse import ArgumentParser, Namespace
43-
from typing import Any, Callable, ParamSpec, Self, TypeVar
43+
from typing import Any, Callable, ParamSpec, TypeVar
4444

4545
T = TypeVar("T")
4646
P = ParamSpec("P")
@@ -61,7 +61,7 @@ class DeprecationHandler:
6161
_version_tuple: tuple[int, ...] | None
6262
_version_object: Version | None
6363

64-
def __init__(self: Self, version: str) -> None:
64+
def __init__(self, version: str) -> None:
6565
"""Create a deprecation handle for the specified version.
6666
6767
:param version: The version to compare against when checking deprecation statuses.
@@ -93,7 +93,7 @@ def _version_less_than(self, version: str) -> bool:
9393
return self._version < target_version
9494

9595
def __call__(
96-
self: Self,
96+
self,
9797
deprecate_in: str,
9898
remove_in: str,
9999
*,
@@ -138,7 +138,7 @@ def inner(*args: P.args, **kwargs: P.kwargs) -> T:
138138
return deprecated_decorator
139139

140140
def argument(
141-
self: Self,
141+
self,
142142
deprecate_in: str,
143143
remove_in: str,
144144
argument: str,
@@ -195,7 +195,7 @@ def inner(*args: P.args, **kwargs: P.kwargs) -> T:
195195
return deprecated_decorator
196196

197197
def action(
198-
self: Self,
198+
self,
199199
deprecate_in: str,
200200
remove_in: str,
201201
action: ActionType,
@@ -210,7 +210,7 @@ class DeprecationMixin(Action):
210210
category: type[Warning]
211211
help: str # override argparse.Action's help type annotation
212212

213-
def __init__(inner_self: Self, *args: Any, **kwargs: Any) -> None:
213+
def __init__(inner_self, *args: Any, **kwargs: Any) -> None:
214214
super().__init__(*args, **kwargs)
215215

216216
category, message = self._generate_message(
@@ -236,7 +236,7 @@ def __init__(inner_self: Self, *args: Any, **kwargs: Any) -> None:
236236
inner_self.help = message
237237

238238
def __call__(
239-
inner_self: Self,
239+
inner_self,
240240
parser: ArgumentParser,
241241
namespace: Namespace,
242242
values: Any,
@@ -258,7 +258,7 @@ def __call__(
258258
return type(action.__name__, (DeprecationMixin, action), {}) # type: ignore[return-value]
259259

260260
def module(
261-
self: Self,
261+
self,
262262
deprecate_in: str,
263263
remove_in: str,
264264
*,
@@ -281,7 +281,7 @@ def module(
281281
)
282282

283283
def constant(
284-
self: Self,
284+
self,
285285
deprecate_in: str,
286286
remove_in: str,
287287
constant: str,
@@ -323,7 +323,7 @@ def __getattr__(name: str) -> Any:
323323
with warnings.catch_warnings(): # temporarily override warning handling
324324
warnings.simplefilter("always", category) # turn off filter
325325

326-
warnings.warn(message, category, stacklevel=3 + stack)
326+
warnings.warn(message, category, stacklevel=2 + stack)
327327
return value
328328

329329
if super_getattr:
@@ -334,7 +334,7 @@ def __getattr__(name: str) -> Any:
334334
module.__getattr__ = __getattr__ # type: ignore[method-assign]
335335

336336
def topic(
337-
self: Self,
337+
self,
338338
deprecate_in: str,
339339
remove_in: str,
340340
*,
@@ -371,7 +371,7 @@ def topic(
371371

372372
warnings.warn(message, category, stacklevel=2 + stack)
373373

374-
def _get_module(self: Self, stack: int) -> tuple[ModuleType, str]:
374+
def _get_module(self, stack: int) -> tuple[ModuleType, str]:
375375
"""Detect the module from which we are being called.
376376
377377
:param stack: The stacklevel increment.
@@ -408,7 +408,7 @@ def _get_module(self: Self, stack: int) -> tuple[ModuleType, str]:
408408
raise DeprecatedError("unable to determine the calling module")
409409

410410
def message(
411-
self: Self,
411+
self,
412412
deprecate_in: str,
413413
remove_in: str,
414414
prefix: str,
@@ -431,7 +431,7 @@ def message(
431431
return message
432432

433433
def _generate_message(
434-
self: Self,
434+
self,
435435
deprecate_in: str,
436436
remove_in: str,
437437
prefix: str | None,

0 commit comments

Comments
 (0)