AnswerDotAI / AnswerDotAI/fastcore
`@patch` breaks on iterable classes (e.g. Enums)`@patch` breaks on iterable classes (e.g. Enums)
- Dominant language
- Jupyter Notebook
- Stars
- 1.1k
- Forks
- 295
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 7
Description
Patching a method onto an `Enum` subclass fails:
```python
from enum import Enum
from fastcore.basics import patch
class Color(Enum):
RED = 1
GREEN = 2
@patch(cls_method=True)
def from_name(cls: Color, s): return cls[s.upper()]
# AttributeError: 'Color' object has no attribute '__name__'
```
The cause is in `patch_to`, which calls `tuplify(cls)`. An `Enum` class is
iterable, so instead of wrapping the class in a one-element tuple, `tuplify`
iterates it and returns its members. The loop body then runs against an enum
member instead of the class, and the member has no `__name__`.
This affects any class whose metaclass makes instances iterable, not just Enums.
A few shapes a fix could take:
1. In `patch_to`, wrap the class directly instead of tuplifying it:
`classes = (cls,) if isinstance(cls, type) else tuplify(cls)`. This keeps
list and union handling intact, since `patch` already converts unions to
tuples via `union2tuple`.
2. Same idea, but guard on tuples instead of types:
`classes = cls if isinstance(cls, tuple) else (cls,)`. Relies on unions
always arriving as tuples, which holds for the `patch` path but not for
direct `patch_to` calls with a bare union.
3. Change `tuplify` (or `listify`) to not iterate types. Broader blast radius,
since those are used widely.
Contributor guide
Research direction
Start in patch_to and inspect how it calls tuplify, then reproduce the Enum example from the issue. Verify that patching a method onto an iterable class succeeds while list and union handling remains intact; add or update focused tests if the existing test suite has coverage for patch_to.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100