Skip to content
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

[feature][metadata] add SuperClassProvider #273

Open
jimmylai opened this issue Mar 25, 2020 · 0 comments
Open

[feature][metadata] add SuperClassProvider #273

jimmylai opened this issue Mar 25, 2020 · 0 comments
Labels
codemod Bundled codemods, visitors, metadata providers

Comments

@jimmylai
Copy link
Contributor

In some lint or codemod use cases, we want to know the super classes of a class to see if it's inherited from a specific class in order to enforce convention or codemods on all subclasses.
The superclass information requires full repository analysis due to multiple inheritance. We can leverage Pyre for that which is similar to the existing TypeInferenceProvider .
https://libcst.readthedocs.io/en/latest/metadata.html#type-inference-metadata

Prerequisite: pyre query support superclasses query given a list of paths.
The current pyre query "superclasses(...)" only supports fully qualified class name but that requires caller to pass the name. To make it more efficient, we want to add the path support to pyer query, so FullRepoManager can pass a list of path to pyre query to read all classes in those paths and their superclasses. To map superclasses data to LibCST syntax tree, we also need the location info.
The pyre query interface for TypeInferenceProvider is: pyre query "types(path=path1, path=path2, ...)" and the output format is

{
"types": [
{
"location": {
"start": {
"line": 8,
"column": 19
},
"stop": {
"line": 8,
"column": 27
}
},
"annotation": "typing.Type[typing.Sequence]"
},

We want to have a similar interface for SuperClassProvider: pyre query "superclasses(path=path1, path=path2, ...)" and the output format is

List[  
{  
class_name: fully qualified string  
location: location fields  
superclasses: list of superclasses (fully qualified string)  
}  
]

CC @shannonzhu

Implementing SuperClassProvider
We want to implement a SuperClassProvider which returns a list of superclasses on ClassDef node or ClassDef.name node.
TypeInferenceProvider can be used as an example.

class TypeInferenceProvider(BatchableMetadataProvider[str]):

We also need some mocked integration tests by extending this script:
https://github.com/Instagram/LibCST/blob/2fb0db33d1b393228a6b45f7749e6df659f186b2/libcst/tests/test_pyre_integration.py

The script generates pyre json output by running pyre query and store as test artifacts in dir
https://github.com/Instagram/LibCST/tree/2fb0db33d1b393228a6b45f7749e6df659f186b2/libcst/tests/pyre
The test cases in the file checks whether each node has expected output.
We also run the artifact generation in CI to make sure we address needed changes when we upgrade Pyre or update the test examples. https://github.com/Instagram/LibCST/blob/master/.circleci/config.yml#L78

For unit tests, we can reuse the generated artifacts and verify the correctness of superclass like

def _test_simple_class_helper(test: UnitTest, wrapper: MetadataWrapper) -> None:

@zsol zsol added the codemod Bundled codemods, visitors, metadata providers label Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codemod Bundled codemods, visitors, metadata providers
Projects
None yet
Development

No branches or pull requests

2 participants