-
Notifications
You must be signed in to change notification settings - Fork 878
Introspection: Introduce TypeHint struct #5438
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Introspection: introduce `TypeHint` and make use of it to encode type hint annotations. | ||
Rename `PyType{Info,Check}::TYPE_INFO` into `PyType{Info,Check}::TYPE_HINT` |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code to extract modules was in the |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ pub struct Function { | |
pub decorators: Vec<String>, | ||
pub arguments: Arguments, | ||
/// return type | ||
pub returns: Option<String>, | ||
pub returns: Option<TypeHint>, | ||
} | ||
|
||
#[derive(Debug, Eq, PartialEq, Clone, Hash)] | ||
|
@@ -31,7 +31,7 @@ pub struct Attribute { | |
/// Value as a Python expression if easily expressible | ||
pub value: Option<String>, | ||
/// Type annotation as a Python expression | ||
pub annotation: Option<String>, | ||
pub annotation: Option<TypeHint>, | ||
} | ||
|
||
#[derive(Debug, Eq, PartialEq, Clone, Hash)] | ||
|
@@ -54,13 +54,38 @@ pub struct Argument { | |
/// Default value as a Python expression | ||
pub default_value: Option<String>, | ||
/// Type annotation as a Python expression | ||
pub annotation: Option<String>, | ||
pub annotation: Option<TypeHint>, | ||
} | ||
|
||
/// A variable length argument ie. *vararg or **kwarg | ||
#[derive(Debug, Eq, PartialEq, Clone, Hash)] | ||
pub struct VariableLengthArgument { | ||
pub name: String, | ||
/// Type annotation as a Python expression | ||
pub annotation: Option<String>, | ||
pub annotation: Option<TypeHint>, | ||
} | ||
|
||
/// A type hint annotation | ||
/// | ||
/// Might be a plain string or an AST fragment | ||
#[derive(Debug, Eq, PartialEq, Clone, Hash)] | ||
pub enum TypeHint { | ||
Ast(TypeHintExpr), | ||
Plain(String), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for custom type hint witten in the |
||
} | ||
|
||
/// A type hint annotation as an AST fragment | ||
#[derive(Debug, Eq, PartialEq, Clone, Hash)] | ||
pub enum TypeHintExpr { | ||
/// A Python builtin like `int` | ||
Builtin { id: String }, | ||
/// The attribute of a python object like `{value}.{attr}` | ||
Attribute { module: String, attr: String }, | ||
/// A union `{left} | {right}` | ||
Union { elts: Vec<TypeHintExpr> }, | ||
/// A subscript `{value}[*slice]` | ||
Subscript { | ||
value: Box<TypeHintExpr>, | ||
slice: Vec<TypeHintExpr>, | ||
}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now added it dynamically in the
_get_feature_sets
function of the noxfile. Not sure if it's the best way to do it