eric7.Plugins.CheckerPlugins.CodeStyleChecker.Pydantic.PydanticUtils

Module implementing utility functions for the PydanticVisitor class.

Global Attributes

PYDANTIC_DECORATORS
PYDANTIC_FIELD_ARGUMENTS
PYDANTIC_METHODS

Classes

None

Functions

_hasAnnotatedField Function to check if the class has a field making use of `Annotated`.
_hasFieldFunction Function to check, if the class has a field defined with the `Field` function.
_hasModelConfig Function to check, if the class has a `model_config` attribute set.
_hasPydanticDecorator Function to check, if the class makes use of Pydantic decorators, such as `computed_field` or `model_validator`.
_hasPydanticMethod Function to check, if the class overrides any of the Pydantic methods, such as `model_dump`.
_hasPydanticModelBase Function to check, if a class definition inherits from Pydantic model classes.
extractAnnotations Function to extract the annotations of an expression.
getDecoratorNames Function to extract the set of decorator names.
isDataclass Function to check, if a class is a dataclass.
isFunction Function to check, if a function call is referencing a given function name.
isName Function to check, if an expression is referencing a given name.
isPydanticModel Function to determine if a class definition is a Pydantic model.


_hasAnnotatedField

_hasAnnotatedField(node)

Function to check if the class has a field making use of `Annotated`.

node (ast.ClassDef)
reference to the node to be be analyzed
Return:
flag indicating that the class has a field making use of `Annotated`
Return Type:
bool
Up


_hasFieldFunction

_hasFieldFunction(node)

Function to check, if the class has a field defined with the `Field` function.

node (ast.ClassDef)
reference to the node to be be analyzed
Return:
flag indicating that the class has a field defined with the `Field` function
Return Type:
bool
Up


_hasModelConfig

_hasModelConfig(node)

Function to check, if the class has a `model_config` attribute set.

node (ast.ClassDef)
reference to the node to be be analyzed
Return:
flag indicating that the class has a `model_config` attribute set
Return Type:
bool
Up


_hasPydanticDecorator

_hasPydanticDecorator(node)

Function to check, if the class makes use of Pydantic decorators, such as `computed_field` or `model_validator`.

node (ast.ClassDef)
reference to the node to be be analyzed
Return:
flag indicating that the class makes use of Pydantic decorators, such as `computed_field` or `model_validator`.
Return Type:
bool
Up


_hasPydanticMethod

_hasPydanticMethod(node: ast.ClassDef)

Function to check, if the class overrides any of the Pydantic methods, such as `model_dump`.

node (ast.ClassDef)
reference to the node to be be analyzed
Return:
flag indicating that class overrides any of the Pydantic methods, such as `model_dump`
Return Type:
bool
Up


_hasPydanticModelBase

_hasPydanticModelBase(node, *, includeRootModel)

Function to check, if a class definition inherits from Pydantic model classes.

node (ast.ClassDef)
reference to the node to be be analyzed
includeRootModel= (bool)
flag indicating to include the root model
Return:
flag indicating that the class definition inherits from a Pydantic model class
Return Type:
bool
Up


extractAnnotations

extractAnnotations(node)

Function to extract the annotations of an expression.

node (ast.expr)
reference to the node to be be processed
Return:
set containing the annotation names
Return Type:
set[str]
Up


getDecoratorNames

getDecoratorNames(decoratorList)

Function to extract the set of decorator names.

decoratorList (list of ast.expr)
list of decorators to be processed
Return:
set containing the decorator names
Return Type:
set of str
Up


isDataclass

isDataclass(node)

Function to check, if a class is a dataclass.

node (ast.ClassDef)
reference to the node to be be analyzed
Return:
flag indicating that the class is a dataclass.
Return Type:
bool
Up


isFunction

isFunction(node, functionName)

Function to check, if a function call is referencing a given function name.

node (ast.Call)
reference to the node to be be analyzed
functionName (str)
name of the function to check for
Return:
flag indicating that the function call is referencing the given function name
Return Type:
bool
Up


isName

isName(node, name)

Function to check, if an expression is referencing a given name.

node (ast.expr)
reference to the node to be be analyzed
name (str)
name to check for
Return:
flag indicating that the expression is referencing teh given name
Return Type:
bool
Up


isPydanticModel

isPydanticModel(node, *, includeRootModel=True)

Function to determine if a class definition is a Pydantic model.

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`.

node (ast.ClassDef)
reference to the node to be be analyzed
includeRootModel= (bool (optional))
flag indicating to include the root model (defaults to True)
Return:
flag indicating a Pydantic model class
Return Type:
bool
Up