Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type nextcord.Embed method parameters more specifically #1076

Open
EmreTech opened this issue Jul 2, 2023 · 1 comment
Open

Type nextcord.Embed method parameters more specifically #1076

EmreTech opened this issue Jul 2, 2023 · 1 comment
Labels
3.0 The issue/PR should go for the 3.0 release p: low Priority: low - not important to be worked on s: planned Status: the issue is planned to be worked on t: typing Type: typing - the types doesn't seem quite right

Comments

@EmreTech
Copy link
Collaborator

EmreTech commented Jul 2, 2023

Summary

The methods for nextcord.Embed should have properly typed parameters.

What is the feature request for?

The core library

The Problem

Currently, many of the parameters in nextcord.Embed methods that are supposed to be str are typed with Any. This is not ideal because static type checkers will allow you to provide any object, whether it's appropriate for the method. It's like that so the parameters can support any object then be stringified later, but it could lead to weird problems if not used correctly.

Here's an example of this:

def set_footer(self, *, text: Optional[Any] = None, icon_url: Optional[Any] = None) -> Self:

The Ideal Solution

nextcord.Embed methods should be typed more precisely to only allow objects that support conversion to str. For example, in our example method set_footer, if an object that supports conversion to a string (__str__) is provided, then the type checker will be happy. Otherwise, it will throw an error.

This can be accomplished by typing the parameter with a protocol that lists the supported conversion method. Unfortunately, Python does not have a built-in protocol for conversion to a string because technically every object can be converted to a string. We will have to make our own.

The Current Solution

No response

Additional Context

As discussed on the Discord Server, this issue will be for v3.

@EmreTech EmreTech added s: planned Status: the issue is planned to be worked on p: low Priority: low - not important to be worked on t: typing Type: typing - the types doesn't seem quite right 3.0 The issue/PR should go for the 3.0 release labels Jul 2, 2023
@Jodenee
Copy link
Contributor

Jodenee commented Mar 3, 2024

I'm not sure if a protocol would fit in this situation, as you know every class in python can be converted into a str with or without manually implementing the str magic method as python will just default to repr if str is not implemented, and that's where the issue is. It's impossible to tell if it is implemented or not with protocols as to protocols str is always defined.

But I am aware of one way of achieving this, what we can do instead is make use of an ABC with just the str magic method as an abstract method, but this comes with its own drawbacks such as, forcing the ABC to be inherited by the user's classes and it not supporting literal strings, so the typing would need to be a union of the ABC and str.

If you are fine with me using the above method, I wouldn't mind submitting a pr to resolve your issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.0 The issue/PR should go for the 3.0 release p: low Priority: low - not important to be worked on s: planned Status: the issue is planned to be worked on t: typing Type: typing - the types doesn't seem quite right
Projects
None yet
Development

No branches or pull requests

2 participants