Add type_hint_literal macro - #6398
bschoenmaeckers wants to merge 1 commit into
Conversation
275b627 to
490545d
Compare
Tpt
left a comment
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?
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. |
|
@bschoenmaeckers Make sense! Thank you! |
| #[test] | ||
| fn test_type_hint_literal_macro() { | ||
| const COLOR: PyStaticExpr = type_hint_identifier!("mypkg", "Color"); | ||
| const T: PyStaticExpr = type_hint_literal!( |
There was a problem hiding this comment.
It hits the compiler recursion limit in macros
There was a problem hiding this comment.
Interesting, I do not see this failure locally...
| const T: PyStaticExpr = type_hint_literal!( | ||
| "USD", | ||
| int(1), | ||
| float(-2.5e2), |
There was a problem hiding this comment.
float isn't valid as Literal value: https://typing.python.org/en/latest/spec/literal.html#legal-and-illegal-parameterizations
This adds a macro to make
Literaltype hints less verbose.