krrood.utils#
Attributes#
Functions#
|
|
|
Return the full name of a class, including the module name. |
|
Calculate the inheritance path length between two classes. |
|
|
|
Return the default value for a given field in a dataclass. |
|
Return a dict mapping field names to their default values. |
|
Extract imports from a module or source code or a file path or an ast and returns them as a list of strings. |
|
Generate a relative import statement using Python's own resolver. |
|
|
|
Get a dictionary of type names grouped by module. |
|
|
|
|
|
Convert a file system path to a Python import path. |
|
Get the import path of a function. |
|
Get the name of a method. |
|
Get the class name of a method if it has one. |
|
Get the file name of a method. |
|
Get a relative import path from the target file to the imported module. |
Get the path starting from the package name. |
|
|
Format import lines from type objects. |
|
Format the file with black |
|
Format the file with ruff |
|
Run a subprocess command and handle errors. |
|
Given a subclass and its generic base, return the concrete type parameter(s). |
|
Find the existing field in the MRO if it exists. |
|
Checks if an object is hashable by attempting to compute its hash. |
|
|
|
Create a scope dictionary from imports in a Python file or an AST tree. |
|
Attempt to import a module with an optional package context and return the module or raise. |
|
|
|
Caches the return value of a function call at the instance level. |
|
Caches the return value of a function call at the instance level but returns a deepcopy of the value. |
|
Clears the memoization cache of an instance. |
|
Check if a class is dynamically created. |
Module Contents#
- krrood.utils.T#
- krrood.utils.recursive_subclasses(cls: Type[T]) List[Type[T]]#
- Parameters:
cls – The class.
- Returns:
A list of the classes subclasses without the class itself.
- krrood.utils.get_full_class_name(cls)#
Return the full name of a class, including the module name.
- Parameters:
cls – The class.
- Returns:
The full name of the class
- krrood.utils.inheritance_path_length(child_class: Type, parent_class: Type) int | None#
Calculate the inheritance path length between two classes. Every inheritance level that lies between child_class and parent_class increases the length by one. In case of multiple inheritance, the path length is calculated for each branch and the minimum is returned.
- Parameters:
child_class – The child class.
parent_class – The parent class.
- Returns:
The minimum path length between child_class and parent_class or None if no path exists.
- krrood.utils.module_and_class_name(t: Type | _SpecialForm) str#
- krrood.utils.get_default_value(dataclass_type, field_name)#
Return the default value for a given field in a dataclass.
- Parameters:
dataclass_type – The dataclass type to get the default value for.
field_name – The name of the field to get the default value for.
- Returns:
The default value for the field.
- krrood.utils.get_default_values_for_dataclass(dataclass_type)#
Return a dict mapping field names to their default values. Only includes fields that actually define a default.
- Parameters:
dataclass_type – The dataclass type to get the default values for.
- Returns:
A dict mapping field names to their default values.
- krrood.utils.extract_imports_from(module: types.ModuleType | None = None, file_path: str | None = None, source: str | None = None, ast_tree: ast.AST | None = None, exclude_libraries: List[str] | None = None, convert_relative_to_absolute: bool = False) List[str]#
Extract imports from a module or source code or a file path or an ast and returns them as a list of strings.
- Parameters:
module – The module to extract imports from.
file_path – The file path to extract imports from.
source – The source code to extract imports from.
ast_tree – The ast tree to extract imports from.
exclude_libraries – A list of libraries to exclude from the imports.
convert_relative_to_absolute – Whether to convert relative imports to absolute imports.
- krrood.utils.generate_relative_import(from_module: str, target_module: str, symbol: str | None = None) str#
Generate a relative import statement using Python’s own resolver.
- Parameters:
from_module – The module where the import is being made.
target_module – The module to import.
symbol – The symbol (e.g., a class, a method, …, etc.) to import (optional).
- krrood.utils.own_dataclass_fields(cls) List[dataclasses.Field]#
- Returns:
The fields of the dataclass that are not inherited from a base class.
- krrood.utils.get_type_names_per_module_from_types(type_objects: Iterable[Type], excluded_names: List[str] | None = None, excluded_modules: List[str] | None = None) Dict[str, List[str]]#
Get a dictionary of type names grouped by module.
- Parameters:
type_objects – A list of type objects to format.
excluded_names – A list of names to exclude from the imports.
excluded_modules – A list of modules to exclude from the imports.
- Returns:
A dictionary of type names grouped by module.
- krrood.utils.is_typing_type(type_object: Type)#
- Parameters:
type_object – A type object to check.
- Returns:
True if the type is a type from the typing module, False otherwise.
- krrood.utils.is_builtin_type(type_object: Any)#
- Parameters:
type_object – A type object to check.
- Returns:
True if the type is a built-in type, False otherwise.
- krrood.utils.get_import_path_from_path(path: str) str | None#
Convert a file system path to a Python import path.
- Parameters:
path – The file system path to convert.
- Returns:
The Python import path.
- krrood.utils.get_function_import_data(func: Callable) Tuple[str, str]#
Get the import path of a function.
- Parameters:
func – The function to get the import path for.
- Returns:
The import path of the function.
- krrood.utils.get_method_name(method: Callable) str#
Get the name of a method.
- Parameters:
method – The method to get the name of.
- Returns:
The name of the method.
- krrood.utils.get_method_class_name_if_exists(method: Callable) str | None#
Get the class name of a method if it has one.
- Parameters:
method – The method to get the class name of.
- Returns:
The class name of the method.
- krrood.utils.get_method_file_name(method: Callable) str#
Get the file name of a method.
- Parameters:
method – The method to get the file name of.
- Returns:
The file name of the method.
- krrood.utils.get_relative_import(target_file_path: str | os.PathLike[str], imported_module_path: str | None = None, module_name: str | None = None, package_name: str | None = None) str#
Get a relative import path from the target file to the imported module.
- Parameters:
target_file_path – The file path of the target file.
imported_module_path – The file path of the module being imported.
module_name – The module name, if available.
package_name – The name of the root package where the module is located.
- Returns:
A relative import path as a string.
- krrood.utils.get_path_starting_from_latest_encounter_of(path: str, package_name: str, should_contain: List[str]) str#
Get the path starting from the package name.
- Parameters:
path – The full path to the file.
package_name – The name of the package to start from.
should_contain – The names of the files or directories to look for.
- Returns:
The path starting from the package name that contains all the names in should_contain, otherwise raise an error.
- Raises:
PackageNameNotFoundError – If the package name could not be found in the path.
PathMissingRequiredComponentsError – If the path does not contain all the names in should_contain.
- krrood.utils.get_imports_from_types(type_objects: Iterable[Type], target_file_path: str | None = None, package_name: str | None = None, excluded_names: List[str] | None = None, excluded_modules: List[str] | None = None) List[str]#
Format import lines from type objects.
- Parameters:
type_objects – A list of type objects to format.
target_file_path – The file path to which the imports should be relative.
package_name – The name of the package to use for relative imports.
excluded_names – A list of names to exclude from the imports.
excluded_modules – A list of modules to exclude from the imports.
- Returns:
A list of formatted import lines.
- krrood.utils.run_black_on_file(filename: str)#
Format the file with black
- Parameters:
filename – The name of the file to format.
- krrood.utils.run_ruff_on_file(filename: str)#
Format the file with ruff
- Parameters:
filename – The name of the file to format.
- krrood.utils.run_subprocess_on_file(command: List[str])#
Run a subprocess command and handle errors.
- Parameters:
command – The command to run as a list of arguments.
- Raises:
SubprocessExecutionError – If the subprocess command fails.
- krrood.utils.get_generic_type_params(cls, generic_base: Type, include_root_generic_base: bool = True, include_specialized_generic_base: bool = True) List[Type[T]]#
Given a subclass and its generic base, return the concrete type parameter(s).
- Example:
get_generic_type_params(Employee, Role) -> (<class ‘__main__.Person’>,)
- Parameters:
cls – The subclass to check.
generic_base – The generic base class to check against.
include_root_generic_base – Whether to include type parameters the class gets from its own typing.Generic directly.
include_specialized_generic_base – Whether to include type parameters from superclasses that are generic, which are not typing.Generic.
- Returns:
A list of concrete type parameters
- krrood.utils.get_existing_field_by_name(cls, name: str) dataclasses.Field | None#
Find the existing field in the MRO if it exists.
- Parameters:
name – The name of the field.
- Returns:
The existing field if found, otherwise None.
- krrood.utils.is_hashable(obj) bool#
Checks if an object is hashable by attempting to compute its hash.
- Parameters:
obj – The object to check.
- Returns:
True if the object is hashable, False otherwise.
- krrood.utils.ensure_hashable(obj) Hashable#
- Returns:
The object itself if it is hashable, otherwise its id.
- krrood.utils.get_scope_from_imports(file_path: str | None = None, tree: ast.AST | None = None, package_name: str | None = None, source: str | None = None) Dict[str, Any]#
Create a scope dictionary from imports in a Python file or an AST tree.
- Parameters:
file_path – The path to the Python file to extract imports from.
tree – An AST tree to extract imports from. If provided, file_path is ignored.
package_name – The name of the package to use for relative imports.
source – The source code to extract imports from. If provided, file_path and tree are ignored.
- Returns:
A dictionary representing the scope with imported modules and their attributes.
- krrood.utils.get_and_import_module(module_name: str, package_name: str | None) types.ModuleType#
Attempt to import a module with an optional package context and return the module or raise.
- Parameters:
module_name – The name of the module to import.
package_name – The package name to use for relative imports, or None for absolute imports.
- Returns:
The imported module.
- Raises:
ModuleNotFoundError – If the module cannot be found.
- krrood.utils.get_module_object(module_name: str, package_name: str | None = None) types.ModuleType | None#
- Parameters:
module_name – The name of the module to import.
package_name – The name of the package containing the module.
- Returns:
The imported module object.
- krrood.utils.TCallable#
- krrood.utils.memoize(function: TCallable) TCallable#
Caches the return value of a function call at the instance level.
- krrood.utils.copy_memoize(function: TCallable) TCallable#
Caches the return value of a function call at the instance level but returns a deepcopy of the value.
- krrood.utils.clear_memoization_cache(instance)#
Clears the memoization cache of an instance.
- krrood.utils.is_dynamic_class(cls: Type) bool#
Check if a class is dynamically created.
This is done by checking if the class is actually registered in that module under its own name Normal classes will be found; classes created with for instance make_dataclass usually won’t be unless manually assigned. :param cls: The class to check. :return: True if the class is dynamically created, False otherwise.