Sat, 22 Feb 2025 18:04:02 +0100
Added a checker for `pydantic` and `dataclass` related topics based on `flake8-pydantic` v0.4.0.
<!DOCTYPE html> <html><head> <title>eric7.Plugins.CheckerPlugins.CodeStyleChecker.Pydantic.PydanticUtils</title> <meta charset="UTF-8"> <link rel="stylesheet" href="styles.css"> </head> <body> <a NAME="top" ID="top"></a> <h1>eric7.Plugins.CheckerPlugins.CodeStyleChecker.Pydantic.PydanticUtils</h1> <p> Module implementing utility functions for the PydanticVisitor class. </p> <h3>Global Attributes</h3> <table> <tr><td>PYDANTIC_DECORATORS</td></tr> <tr><td>PYDANTIC_FIELD_ARGUMENTS</td></tr> <tr><td>PYDANTIC_METHODS</td></tr> </table> <h3>Classes</h3> <table> <tr><td>None</td></tr> </table> <h3>Functions</h3> <table> <tr> <td><a href="#_hasAnnotatedField">_hasAnnotatedField</a></td> <td>Function to check if the class has a field making use of `Annotated`.</td> </tr> <tr> <td><a href="#_hasFieldFunction">_hasFieldFunction</a></td> <td>Function to check, if the class has a field defined with the `Field` function.</td> </tr> <tr> <td><a href="#_hasModelConfig">_hasModelConfig</a></td> <td>Function to check, if the class has a `model_config` attribute set.</td> </tr> <tr> <td><a href="#_hasPydanticDecorator">_hasPydanticDecorator</a></td> <td>Function to check, if the class makes use of Pydantic decorators, such as `computed_field` or `model_validator`.</td> </tr> <tr> <td><a href="#_hasPydanticMethod">_hasPydanticMethod</a></td> <td>Function to check, if the class overrides any of the Pydantic methods, such as `model_dump`.</td> </tr> <tr> <td><a href="#_hasPydanticModelBase">_hasPydanticModelBase</a></td> <td>Function to check, if a class definition inherits from Pydantic model classes.</td> </tr> <tr> <td><a href="#extractAnnotations">extractAnnotations</a></td> <td>Function to extract the annotations of an expression.</td> </tr> <tr> <td><a href="#getDecoratorNames">getDecoratorNames</a></td> <td>Function to extract the set of decorator names.</td> </tr> <tr> <td><a href="#isDataclass">isDataclass</a></td> <td>Function to check, if a class is a dataclass.</td> </tr> <tr> <td><a href="#isFunction">isFunction</a></td> <td>Function to check, if a function call is referencing a given function name.</td> </tr> <tr> <td><a href="#isName">isName</a></td> <td>Function to check, if an expression is referencing a given name.</td> </tr> <tr> <td><a href="#isPydanticModel">isPydanticModel</a></td> <td>Function to determine if a class definition is a Pydantic model.</td> </tr> </table> <hr /> <hr /> <a NAME="_hasAnnotatedField" ID="_hasAnnotatedField"></a> <h2>_hasAnnotatedField</h2> <b>_hasAnnotatedField</b>(<i>node</i>) <p> Function to check if the class has a field making use of `Annotated`. </p> <dl> <dt><i>node</i> (ast.ClassDef)</dt> <dd> reference to the node to be be analyzed </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that the class has a field making use of `Annotated` </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="_hasFieldFunction" ID="_hasFieldFunction"></a> <h2>_hasFieldFunction</h2> <b>_hasFieldFunction</b>(<i>node</i>) <p> Function to check, if the class has a field defined with the `Field` function. </p> <dl> <dt><i>node</i> (ast.ClassDef)</dt> <dd> reference to the node to be be analyzed </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that the class has a field defined with the `Field` function </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="_hasModelConfig" ID="_hasModelConfig"></a> <h2>_hasModelConfig</h2> <b>_hasModelConfig</b>(<i>node</i>) <p> Function to check, if the class has a `model_config` attribute set. </p> <dl> <dt><i>node</i> (ast.ClassDef)</dt> <dd> reference to the node to be be analyzed </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that the class has a `model_config` attribute set </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="_hasPydanticDecorator" ID="_hasPydanticDecorator"></a> <h2>_hasPydanticDecorator</h2> <b>_hasPydanticDecorator</b>(<i>node</i>) <p> Function to check, if the class makes use of Pydantic decorators, such as `computed_field` or `model_validator`. </p> <dl> <dt><i>node</i> (ast.ClassDef)</dt> <dd> reference to the node to be be analyzed </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that the class makes use of Pydantic decorators, such as `computed_field` or `model_validator`. </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="_hasPydanticMethod" ID="_hasPydanticMethod"></a> <h2>_hasPydanticMethod</h2> <b>_hasPydanticMethod</b>(<i>node: ast.ClassDef</i>) <p> Function to check, if the class overrides any of the Pydantic methods, such as `model_dump`. </p> <dl> <dt><i>node</i> (ast.ClassDef)</dt> <dd> reference to the node to be be analyzed </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that class overrides any of the Pydantic methods, such as `model_dump` </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="_hasPydanticModelBase" ID="_hasPydanticModelBase"></a> <h2>_hasPydanticModelBase</h2> <b>_hasPydanticModelBase</b>(<i>node, *, includeRootModel</i>) <p> Function to check, if a class definition inherits from Pydantic model classes. </p> <dl> <dt><i>node</i> (ast.ClassDef)</dt> <dd> reference to the node to be be analyzed </dd> <dt><i>includeRootModel=</i> (bool)</dt> <dd> flag indicating to include the root model </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that the class definition inherits from a Pydantic model class </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="extractAnnotations" ID="extractAnnotations"></a> <h2>extractAnnotations</h2> <b>extractAnnotations</b>(<i>node</i>) <p> Function to extract the annotations of an expression. </p> <dl> <dt><i>node</i> (ast.expr)</dt> <dd> reference to the node to be be processed </dd> </dl> <dl> <dt>Return:</dt> <dd> set containing the annotation names </dd> </dl> <dl> <dt>Return Type:</dt> <dd> set[str] </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="getDecoratorNames" ID="getDecoratorNames"></a> <h2>getDecoratorNames</h2> <b>getDecoratorNames</b>(<i>decoratorList</i>) <p> Function to extract the set of decorator names. </p> <dl> <dt><i>decoratorList</i> (list of ast.expr)</dt> <dd> list of decorators to be processed </dd> </dl> <dl> <dt>Return:</dt> <dd> set containing the decorator names </dd> </dl> <dl> <dt>Return Type:</dt> <dd> set of str </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="isDataclass" ID="isDataclass"></a> <h2>isDataclass</h2> <b>isDataclass</b>(<i>node</i>) <p> Function to check, if a class is a dataclass. </p> <dl> <dt><i>node</i> (ast.ClassDef)</dt> <dd> reference to the node to be be analyzed </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that the class is a dataclass. </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="isFunction" ID="isFunction"></a> <h2>isFunction</h2> <b>isFunction</b>(<i>node, functionName</i>) <p> Function to check, if a function call is referencing a given function name. </p> <dl> <dt><i>node</i> (ast.Call)</dt> <dd> reference to the node to be be analyzed </dd> <dt><i>functionName</i> (str)</dt> <dd> name of the function to check for </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that the function call is referencing the given function name </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="isName" ID="isName"></a> <h2>isName</h2> <b>isName</b>(<i>node, name</i>) <p> Function to check, if an expression is referencing a given name. </p> <dl> <dt><i>node</i> (ast.expr)</dt> <dd> reference to the node to be be analyzed </dd> <dt><i>name</i> (str)</dt> <dd> name to check for </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating that the expression is referencing teh given name </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> <hr /> <a NAME="isPydanticModel" ID="isPydanticModel"></a> <h2>isPydanticModel</h2> <b>isPydanticModel</b>(<i>node, *, includeRootModel=True</i>) <p> Function to determine if a class definition is a Pydantic model. </p> <p> Multiple heuristics are use to determine if this is the case: - The class inherits from `BaseModel` (or `RootModel` if `includeRootModel` is `True`). - The class has a `model_config` attribute set. - The class has a field defined with the `Field` function. - The class has a field making use of `Annotated`. - The class makes use of Pydantic decorators, such as `computed_field` or `model_validator`. - The class overrides any of the Pydantic methods, such as `model_dump`. </p> <dl> <dt><i>node</i> (ast.ClassDef)</dt> <dd> reference to the node to be be analyzed </dd> <dt><i>includeRootModel=</i> (bool (optional))</dt> <dd> flag indicating to include the root model (defaults to True) </dd> </dl> <dl> <dt>Return:</dt> <dd> flag indicating a Pydantic model class </dd> </dl> <dl> <dt>Return Type:</dt> <dd> bool </dd> </dl> <div align="right"><a href="#top">Up</a></div> <hr /> </body></html>