Skip to content

Commit d191fbe

Browse files
committed
naming: @generic_addmethod more descriptive than @generic_for
Avoid semantically loaded word, it has nothing to do with `for` loops.
1 parent 4b655c6 commit d191fbe

4 files changed

Lines changed: 18 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ For future plans, see our [Python language version support status](https://githu
2121

2222
- Robustness: several auxiliary syntactic constructs such as `local[]`/`delete[]` (for `do[]`), `call_cc[]` (for `with continuations`), `it` (for `aif[]`), `with expr`/`with block` (for `let_syntax`/`abbrev`), and `q`/`u`/`kw` (for `prefix`) now detect *at macro expansion time* if they appear outside any valid lexical context, and raise `SyntaxError` (with a descriptive message) if so. That is, the error is now raised *at compile time*. Previously these constructs could only raise an error at run time, and not all of them could detect the error even then.
2323

24-
- `unpythonic.dispatch.generic_for`: add methods to a generic function defined elsewhere.
24+
- `unpythonic.dispatch.generic_addmethod`: add methods to a generic function defined elsewhere.
2525

2626
- Python 3.8 and 3.9 support added.
2727

doc/features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2986,7 +2986,7 @@ The core idea can be expressed in fewer than 100 lines of Python; ours is (as of
29862986
29872987
**Changed in v0.14.3**. *The `@generic` and `@typed` decorators can now decorate also instance methods, class methods and static methods (beside regular functions, as previously in 0.14.2).*
29882988
2989-
**Changed in v0.15.0**. *The `dispatch` and `typecheck` modules providing this functionality are now considered stable (no longer experimental). Added the `@generic_for` parametric decorator that can register a new method on an existing generic function originally defined in another lexical scope.*
2989+
**Changed in v0.15.0**. *The `dispatch` and `typecheck` modules providing this functionality are now considered stable (no longer experimental). Added the `@generic_addmethod` parametric decorator that can register a new method on an existing generic function originally defined in another lexical scope.*
29902990
29912991
The ``generic`` decorator allows creating multiple-dispatch generic functions with type annotation syntax.
29922992

unpythonic/dispatch.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
https://docs.python.org/3/library/functools.html#functools.singledispatch
77
"""
88

9-
__all__ = ["generic", "generic_for", "typed"]
9+
__all__ = ["generic", "generic_addmethod", "typed"]
1010

1111
from functools import partial, wraps
1212
from itertools import chain
@@ -37,7 +37,7 @@
3737
# """
3838

3939
@register_decorator(priority=98)
40-
def generic_for(target):
40+
def generic_addmethod(target):
4141
"""Parametric decorator. Add a method to function `target`.
4242
4343
Like `@generic`, but the target function on which the method will be
@@ -55,10 +55,10 @@ def f(x: int):
5555
5656
5757
# main.py
58-
from unpythonic import generic_for
58+
from unpythonic import generic_addmethod
5959
import example
6060
61-
@generic_for(example.f)
61+
@generic_addmethod(example.f)
6262
def f(x: float):
6363
...
6464
"""
@@ -212,7 +212,7 @@ def _getfullname(f):
212212
def _register_generic(fullname, f):
213213
"""Register a method for a generic function.
214214
215-
This is a low-level function; you'll likely want `generic` or `generic_for`.
215+
This is a low-level function; you'll likely want `generic` or `generic_addmethod`.
216216
217217
fullname: str, fully qualified name of target function to register
218218
the method on, used as key in the dispatcher registry.

unpythonic/tests/test_dispatch.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import typing
77
from ..fun import curry
8-
from ..dispatch import generic, generic_for, typed
8+
from ..dispatch import generic, generic_addmethod, typed
99

1010
@generic
1111
def zorblify(x: int, y: int):
@@ -91,20 +91,20 @@ def runtests():
9191
test[gargle(42, 6.022e23, "hello") == "int, float, str"]
9292
test[gargle(1, 2, 3) == "int"] # as many as in the [int, float, str] case
9393

94-
with testset("@generic_for"):
94+
with testset("@generic_addmethod"):
9595
@generic
9696
def f1(x: typing.Any):
9797
return False
98-
@generic_for(f1)
98+
@generic_addmethod(f1)
9999
def f2(x: int):
100100
return x
101101
test[f1("hello") is False]
102102
test[f1(42) == 42]
103103

104104
def f3(x: typing.Any): # not @generic!
105105
return False
106-
with test_raises[TypeError, "should not be able to @generic_for a non-generic function"]:
107-
@generic_for(f3)
106+
with test_raises[TypeError, "should not be able to @generic_addmethod a non-generic function"]:
107+
@generic_addmethod(f3)
108108
def f4(x: int):
109109
return x
110110

@@ -269,12 +269,12 @@ def flippable(x: typing.Any): # default
269269
# Since these are in the same lexical scope as the original definition of the
270270
# generic function `flippable`, we could do this using `@generic`, but
271271
# later extensions (which are the whole point of traits) will need to specify
272-
# on which function the new methods are to be registered, using `@generic_for`.
272+
# on which function the new methods are to be registered, using `@generic_addmethod`.
273273
# So let's do that to show how it's done.
274-
@generic_for(flippable)
274+
@generic_addmethod(flippable)
275275
def flippable(x: str): # noqa: F811
276276
return IsFlippable()
277-
@generic_for(flippable)
277+
@generic_addmethod(flippable)
278278
def flippable(x: int): # noqa: F811
279279
return IsNotFlippable()
280280

@@ -289,7 +289,7 @@ def flippable(x: int): # noqa: F811
289289
def flip(x: typing.Any):
290290
return flip(flippable(x), x)
291291

292-
# Implementation of `flip`. Same comment about `@generic_for` as above.
292+
# Implementation of `flip`. Same comment about `@generic_addmethod` as above.
293293
#
294294
# Here we provide one implementation for "flippable" objects and another one
295295
# for "nonflippable" objects. Note this dispatches regardless of the actual
@@ -299,10 +299,10 @@ def flip(x: typing.Any):
299299
# We could also add methods for specific types if needed. Note this is not
300300
# Julia, so the first matching definition wins, instead of the most specific
301301
# one.
302-
@generic_for(flip)
302+
@generic_addmethod(flip)
303303
def flip(traitvalue: IsFlippable, x: typing.Any): # noqa: F811
304304
return x[::-1]
305-
@generic_for(flip)
305+
@generic_addmethod(flip)
306306
def flip(traitvalue: IsNotFlippable, x: typing.Any): # noqa: F811
307307
raise TypeError(f"{repr(x)} is IsNotFlippable")
308308

0 commit comments

Comments
 (0)