pylint-dev / astroid

A common base representation of python source code for pylint and other projects
https://pylint.readthedocs.io/projects/astroid/en/latest/
GNU Lesser General Public License v2.1
528 stars 273 forks source link

Ability to overwrite transforms or improved support for unregistering transforms #836

Open ltcmelo opened 4 years ago

ltcmelo commented 4 years ago

I need to write a (module) transform for a a module that is already part of astroid's brain — think of my transform as an extension/enhancement of the transform existing one.

Problems:

The way I'm working around this situation so far is like this:

    class Mock:
        def __init__(self, name):
            self.name = name

    mock = Mock('hashlib')

    for node_type, data in manager._transform.transforms.items():
        if node_type == nodes.Module:
            for transform, pred in data:
                if pred(mock):
                    manager.unregister_transform(node_type, transform, pred)

In this example ☝️ I'm unregistered the transform for hashlib by iterating over (intendedly) private field that contains the transforms container and testing whether or not the predicate matches. Not ideal…

Am I missing anything or there's indeed a need for an API here?

eugene57 commented 4 years ago

This is similar to https://github.com/PyCQA/astroid/issues/816.

I also have not found a public API for that.

PCManticore commented 3 years ago

Thanks, this was a good report. We definitely need to look over the API for this.