Skip to content

Improve the AutoGraph interface for external projects #1

@dime10

Description

@dime10

Projects who wish to depend on AutoGraph to implement their own operator overloading often need to duplicate code from AutoGraph and/or patch existing objects from the library. A clean interface for other projects to customize the behaviour of AutoGraph or provide their own implementations of operators would be beneficial.

A few examples where such issues arise:

  • the _TRANSPILER global in autograph/impl/api:
    The global instance of the AST transformer is useful in order to benefit from caching. However, projects may supply their own instance or subclass the existing implementation to customize behaviour (e.g. to supply custom operators to the ag__. namespace).
    The problem arises whenever this variable is directly referenced in AG code, for example _convert_actual which is invoked from the the converted_call function. Patching _TRANSPILER or re-implementing converted_call is thus required.
  • the CONVERSION_RULES global in autograph/core/config:
    This variable defines rules to avoid converting functions from certain packages, which is accessed by the is_allowlisted function (also invoked in converted_call).
    Projects may wish to extend the existing list, requiring to patch CONVERSION_RULES or re-implementing is_allowlisted and converted_call.
  • the BUILTIN_FUNCTIONS_MAP global in autograph/operators/py_builtins:
    This map is used to overload Python builtins such as range, len, enumerate, print, etc. Unfortunately, this overloading is not done via the AST transformer, which normally transforms AST nodes into operators of the ag__. namespace (for which custom implementations can be provided). Instead, the operators are overloaded directly in the converted_call function, by referencing this map.
    Providing custom implementations for builtins thus requires patching BUILTIN_FUNCTIONS_MAP or re-implementing converted_call.

Note that the converted_call function is itself marked with a TODO to implement it as a Call operator, in the same manner as all other overloaded operators. Implementing the operator in a way that components such as conversion rules, caching, builtins, etc. could be reused would be advantageous.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions