Code-Society-Lab / Code-Society-Lab/matrixpy

Custom Type Resolver

Open
#93 0 comments 0 reactions 1 assignee Claimed by @PenguinBoi12 View on GitHub
feature
Dominant language
Python
Stars
14
Forks
7
Avg merge
8d 19h
Merged PRs (30d)
5

Description

Argument parsing in `Command._convert_type` currently supports only types whose constructor accepts a raw string (`int("6")`, `float("3.14")`). There is no way to teach the framework how to parse a complex object, such as a `User` looked up by display name, without monkey-patching the command internals.

The idea is to let any class act as a type resolver by implementing a `resolve` classmethod. The argument parser detects this interface via duck typing and delegates to it instead of calling the constructor directly. An optional registration helper on `Bot` could cover types you don't own.

**Proposed API**
```python
class User:
@classmethod
def resolve(cls, value: str) -> Self:
return ... # build user from value

@bot.command()
async def profile(ctx, user: User):
await ctx.reply(f"Power level: {user.power_level}")
```

```python
# For types you don't own
@bot.type_resolver(Color)
def parse_color(value: str) -> Color:
return Color.from_hex(value)
```

The hook sits in `Command._convert_type`. The resolver lookup order should be: explicit registry on `Bot` first, then the `resolve` classmethod, then the bare constructor, then a passthrough to `str`. Error handling is needed, a `resolve` that raises should probably surface as a `BadArgumentError` rather than silently falling back to the raw string.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.