krrood.utils#

Attributes#

T

Functions#

recursive_subclasses(cls) → list[type[T]]

get_full_class_name(cls)

Return the full name of a class, including the module name.

module_and_class_name(type_) → str

get_module_of_type(type_) → str

resolve_class_from_full_name(fully_qualified_class_name) → type

Import and return the class named by a fully qualified name of the form

get_default_value(dataclass_type, field_name)

Return the default value for a given field in a dataclass.

get_default_values_for_dataclass(dataclass_type)

Return a dict mapping field names to their default values.

own_dataclass_fields(cls) → list[dataclasses.Field]

is_typing_type(type_object)

is_builtin_type(type_object)

get_import_root_from_path(path) → pathlib.Path

Find the directory an import of a path is resolved against.

get_import_path_from_path(path) → str | None

Convert a file system path to a Python import path.

make_path_importable(path) → None

Put the directory an import of a path is resolved against on the search path.

get_function_import_data(func) → tuple[str, str]

Get the import path of a function.

get_method_name(method) → str

Get the name of a method.

get_class_and_attribute_name(class_name, attribute_name) → str

Return the dot-qualified name "{class_name}.{attribute_name}".

get_method_class_name_if_exists(method) → str | None

Get the class name of a method if it has one.

get_method_file_name(method) → str

Get the file name of a method.

get_relative_import(target_file_path[, ...]) → str

Get a relative import path from the target file to the imported module.

get_path_starting_from_latest_encounter_of(path, ...) → str

Get the path starting from the package name.

get_generic_type_parameters(cls, generic_base[, ...]) → list[type[T]]

Given a subclass and its generic base, return the concrete type parameter(s).

get_existing_field_by_name(cls, name) → dataclasses.Field | None

Find the existing field in the MRO if it exists.

is_hashable(obj) → bool

Checks if an object is hashable by attempting to compute its hash.

ensure_hashable(obj) → Hashable

get_scope_from_imports([file_path, tree, ...]) → dict[str, Any]

Create a scope dictionary from imports in a Python file or an AST tree.

module_level_imports(node) → Iterator[ast.Import | ast.ImportFrom]

Yield the import statements that bind names in the module namespace.

get_and_import_module(module_name, package_name) → types.ModuleType

Attempt to import a module with an optional package context and return the module or

get_module_object(module_name[, package_name]) → types.ModuleType | None

is_dynamic_class(cls) → bool

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.module_and_class_name(type_: type | _SpecialForm) → str#
Parameters:

type – A class or special form.

Returns:

Its fully qualified "{module}.{name}" identifier.

krrood.utils.get_module_of_type(type_: type | _SpecialForm) → str#
Parameters:

type – The type of which the module is obtained.

Returns:

The module name of the given type_.

krrood.utils.resolve_class_from_full_name(fully_qualified_class_name: str) → type#

Import and return the class named by a fully qualified name of the form "module.submodule.ClassName", as written by get_full_class_name() or module_and_class_name().

Parameters:

fully_qualified_class_name – The fully qualified class name.

Returns:

The resolved class.

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.own_dataclass_fields(cls) → list[dataclasses.Field]#
Returns:

The fields of the dataclass that are not inherited from a base class.

krrood.utils.is_typing_type(type_object: Any)#
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_root_from_path(path: pathlib.Path) → pathlib.Path#

Find the directory an import of a path is resolved against.

Parameters:

path – The file system path to find the import root of.

Returns:

The nearest ancestor of the path that is not itself a package.

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, or None if the path is not inside a package.

krrood.utils.make_path_importable(path: pathlib.Path) → None#

Put the directory an import of a path is resolved against on the search path.

..note:: Generated code is written wherever its caller chose, which need not be a

directory Python already searches, so importing it back requires saying where to look for it.

Parameters:

path – The file system path that is about to be imported.

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_class_and_attribute_name(class_name: str, attribute_name: str) → str#

Return the dot-qualified name "{class_name}.{attribute_name}".

Parameters:
  • class_name – The owner class name, typically SomeClass.__name__.

  • attribute_name – The attribute or variable name to qualify.

Returns:

The qualified name string.

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_generic_type_parameters(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_parameters(Employee, Role) -> [<class ‘__main__.Person’>]

Direct parameterizations (e.g. class C(B, Generic[U])) take priority over an inherited binding discovered by recursing into an unparameterized base.

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.module_level_imports(node: ast.AST) → Iterator[ast.Import | ast.ImportFrom]#

Yield the import statements that bind names in the module namespace.

Imports inside functions, lambdas and class bodies are skipped, since the names they bind are local to that body.

Parameters:

node – The node whose statements are searched.

Returns:

The module-level import statements, including those nested in compound statements such as if TYPE_CHECKING: or try.

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