Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Unreleased
- clearer error messages from `thing_or_things` and `decorator`: they name the function and, where there is one, the fix. Exception types are unchanged
- `thing_or_things` supports keyword-only parameters as the thing
- `thing_or_things` ignores `self` / `cls` when finding the thing, so methods like `get(self, ids)` no longer need it named
- `thing_or_things` ignores a method's `self` / `cls` when finding the thing, so methods like `get(self, ids)` no longer need it named
- `thing_or_things` treats a frozenset as many things, like a set; it rejects async functions and unhashable things up front
- decorators built with `decorator` (including `thing_or_things`) raise TypeError when stacked below `@classmethod` / `@staticmethod`, instead of silently misbehaving
- fix truthiness and mixed-type operators on ambiguous functions: `bool()` / `if` on a str or list result, `one == 1.0`, `1.5 + one`, `'a' + text`
Expand Down
14 changes: 9 additions & 5 deletions ambiguous/thing_or_things.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ def thing_or_things(fn, arg_name=None):
if not named:
raise TypeError(f"{name}() takes no arguments, so has no thing")

# prefer positional args, falling back to keyword-only ones. On
# methods, self / cls is never the thing
# prefer positional args, falling back to keyword-only ones
positional = [p for p in named if p.kind is not p.KEYWORD_ONLY]

# on methods, ie. defined in a class body, self / cls is never the thing
owner, _, _ = getattr(fn, "__qualname__", "").rpartition(".")
is_method = owner and not owner.endswith("<locals>")
if is_method and positional and positional[0].name in ("self", "cls"):
positional = positional[1:]

candidates = (
[p for p in positional if p.name not in ("self", "cls")]
or [p for p in named if p.kind is p.KEYWORD_ONLY]
or named
positional or [p for p in named if p.kind is p.KEYWORD_ONLY] or named
)
required = [p for p in candidates if p.default is p.empty]

Expand Down
8 changes: 8 additions & 0 deletions tests/test_thing_or_things.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ def kwonly(self, *, ids):
self.assertEqual({1: 1}, Repo().get([1]))
self.assertEqual(1, Repo().kwonly(ids=1))

def test_self_param_on_function(self):
# outside a class, a param named cls is just a param
@thing_or_things
def css(cls, prefix="x-"):
return {c: prefix + c for c in cls}

self.assertEqual("x-btn", css("btn"))

def test_method_hint(self):
# name the method by its qualname, and suggest the thing, not self
with self.assertRaisesRegex(
Expand Down
Loading