Skip to content

Provide __mypyc_attrs__ analogue for non-compiled mypy #12935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
devmessias opened this issue Jun 3, 2022 · 3 comments
Closed

Provide __mypyc_attrs__ analogue for non-compiled mypy #12935

devmessias opened this issue Jun 3, 2022 · 3 comments
Labels

Comments

@devmessias
Copy link
Contributor

devmessias commented Jun 3, 2022

Feature

mpyc_attrs provides a simple way to list all the attributes that can be related to SymbolTableNode, Nodes, etc.
If I understood correctly __mypyc_attrs__ is similar to _fields attribute on the python ast module.

In mypy, this attribute exists only in the compiled version and it's provided by allocate_class

create_mypyc_attrs_tuple(builder, builder.mapper.type_to_ir[cdef.info], cdef.line)],

Pitch

I'm trying to create something that can also be used to address the following issues

#4868
#12513

One example of use is this

import mypy.main as MAIN
import mypy.build as BUILD

from mypy.nodes import SymbolTableNode, Node, NodeVisitor
py_file = "src/script_name"

mod = py_file.replace("/", ".")

files, opt = MAIN.process_options([f"{py_file}.py"])
opt.preserve_asts = True
opt.fine_grained_incremental = True
result = BUILD.build(files, options=opt)
tree = result.graph[mod].tree
names = tree.names.keys()

def is_node(value):
    return isinstance(value, Node) or isinstance(value, SymbolTableNode)

class MyVisitor(NodeVisitor):
    def generic_visit(self, node):
        print(f'entering {node.__class__.__name__}')
        if hasattr(node, "line"):
            print(f"\t{node.line}")
        if hasattr(node, "column"):
            print(f"\t{node.column}")
            if node.column == -1:
                return
        if hasattr(node, "name"):
            print(f"\t{node.name}")
        if not hasattr(node, "__mypyc_attrs__"):
            return
        node_fields = zip(
            node.__mypyc_attrs__,
            [
                getattr(node, attr)
                for attr in node.__mypyc_attrs__
            ]
        )
        for attr, value in node_fields:
            if isinstance(value, list):
                for v in value:
                    if not is_node(v):
                        continue
                    self.generic_visit(v)
            if is_node(value):
                #self.visit(value)
                self.generic_visit(value)

for name in names:
    symbol_table_node = tree.names[name]
    if symbol_table_node is None:
        continue
    visitor = MyVisitor()
    visitor.generic_visit(symbol_table_node.node)
@devmessias
Copy link
Contributor Author

To be more clear. I'm trying to extract the type information with the position like pyre does

image

@devmessias
Copy link
Contributor Author

Also, I want to point out that we still can't use NodeVisitor as a subclass in a 3rd party code. Is this should be solved by this committ? effd970

Running the code described in the first message with the compiled version of mypy still returns the error

TypeError: interpreted classes cannot inherit from compiled traits

@devmessias
Copy link
Contributor Author

devmessias commented Jun 11, 2022

I'll close this because after digging in the mypy codebase I believe this will create too much friction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant