Add type_hint_literal macro - #6398
Open
bschoenmaeckers wants to merge 1 commit into
Open
Conversation
bschoenmaeckers
force-pushed
the
inspect-literal-macro
branch
from
September 9, 2026 12:43
275b627 to
490545d
Compare
Tpt
reviewed
Sep 9, 2026
Tpt
left a comment
Contributor
There was a problem hiding this comment.
Nice! I am a bit scared by this added complexity: do we have actual usescases where it will be useful?
Member
Author
My usecase is a local project that implements a pub enum Period {
Daily,
Weekly,
Monthly,
Quarterly,
SemiAnnually,
Yearly,
}
impl FromPyObject<'_, '_> for Period {
type Error = PyErr;
const INPUT_TYPE: PyStaticExpr = pyo3::type_hint_subscript!(
pyo3::type_hint_identifier!("typing", "Literal"),
PyStaticExpr::Constant {
value: PyStaticConstant::Str("D")
},
PyStaticExpr::Constant {
value: PyStaticConstant::Str("W")
},
PyStaticExpr::Constant {
value: PyStaticConstant::Str("M")
},
PyStaticExpr::Constant {
value: PyStaticConstant::Str("Q")
},
PyStaticExpr::Constant {
value: PyStaticConstant::Str("S")
},
PyStaticExpr::Constant {
value: PyStaticConstant::Str("Y")
}
);
}This looks to my like a common pattern and other users may benefit from this. |
Contributor
|
@bschoenmaeckers Make sense! Thank you! |
Tpt
reviewed
Sep 9, 2026
| #[test] | ||
| fn test_type_hint_literal_macro() { | ||
| const COLOR: PyStaticExpr = type_hint_identifier!("mypkg", "Color"); | ||
| const T: PyStaticExpr = type_hint_literal!( |
Contributor
There was a problem hiding this comment.
It hits the compiler recursion limit in macros
Member
Author
There was a problem hiding this comment.
Interesting, I do not see this failure locally...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a macro to make
Literaltype hints less verbose.