Skip to content

Converters should support annotated typing #1422

@CoderJoshDK

Description

@CoderJoshDK

Summary

Converters don't play nice with type checking. You can use Annotated as a way to satisfy both.

What is the feature request for?

disnake.ext.commands

The Problem

You have to do some funky workarounds to make both the type checker and the converter happy.

Instead, the converter can be implied from the metadata (smilier to FastAPI.)

- url: ValidURL
+ url: Annotated[str, ValidURL]

The Ideal Solution

from .converters import ValidURL

class Foo(commands.Cog):
    @commands.is_owner()
    async def set_command(
        self,
        base_url: Annotated[str, ValidURL] = "",
    ) -> None: ...

This would let base_url be a string while actually being converted in the background.

The Current Solution

Mock the type based on if TYPE_CHECKING

if TYPE_CHECKING:
    Extension = str
else:
    from monty.utils.converters import Extension

Code from monty python bot.

Additional Context

This change should be possible to make backwards compatible, since you would be inspecting the metadata of the object. But unsure since I am not familiar with this codebase enough.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions