Skip to content

temporary_visited_module requires manually running patch_ast beforehand #171

@machow

Description

@machow

Describe the bug

It looks like outside of using the pytest plugin and creating a loader--which both run patch_ast--running temporary_visited_module() will fail with a message like below.

AttributeError: 'Module' object has no attribute 'kind'

It looks like patch_ast mutates attributes etc.. onto ast classes that the visitor needs.

I wonder if turning the AstNode methods into function might help avoid some of the trickiness of chasing down the patching / making sure it's applied before anything that needs it? E.g. something like...

import ast

# note that lru_cache(maxsize=None) works for python < 3.9 compat
from functools import cache

@cache
def kind(self: ast.AST) -> str:    return self.__class__.__name__.lower()

kind(ast.parse("1 + 1"))

Otherwise, maybe patch_ast() could be run inside things like temporary_visited_module()?

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