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

feat(types) C++ int and bool Literals in Python Literal Type #5463

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

InvincibleRMC
Copy link
Contributor

Description

typing.Literal now can accept straight ints and bools, rather than only string Literals previously. For more information check out the python pep here.

Suggested changelog entry:

Update `typing.Literal` type to support C++ ints and bools

@InvincibleRMC InvincibleRMC marked this pull request as ready for review December 12, 2024 18:47
@InvincibleRMC
Copy link
Contributor Author

Failing test is unrelated see #5464.

Comment on lines +125 to +132
typedef py::typing::Literal<26,
0x14,
py::typing::StringLiteral("\"hello world\""),
py::typing::StringLiteral("b\"hello world\""),
py::typing::StringLiteral("u\"hello world\""),
true,
py::typing::StringLiteral("Color.RED"),
py::typing::StringLiteral("None")>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just skimming over this, but would a simple string literal than have to use:
py::typing::Literal<py::typing::StringLiteral("hello")>?
Would be nice if strings could be used directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What could be done I believe is only allow each Literal of the same type. Then I don't think you would need the py::typing::Literal<py::typing::StringLiteral("hello")> however, you wouldn't be able to mix non-type templates inside Literals and would need to use Unions.

Copy link
Contributor Author

@InvincibleRMC InvincibleRMC Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After messing around with various solutions the only one I could get working is make different named literals.

py::typing::LiteralString"Hi">
py::typing::LiteralInt<1, 2, 3>
py::typing::LiteralBool<true>

To avoid confusion if we wanted to move forward with this the current typing::StringLiteral would be change to typing::fixed_string.

@InvincibleRMC InvincibleRMC force-pushed the C++-int-and-bool-Literal-Types branch from efdf223 to 046277e Compare December 18, 2024 02:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants