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

Add NamedTuple to dataclass conversion codemod #299

Merged
merged 6 commits into from
May 27, 2020

Conversation

josieesh
Copy link
Contributor

Summary

Conversion of a NamedTuple class declaration to Python 3.7 dataclasses.dataclass type as an annotation. Perform removal of unused NamedTuple import and add the import of dataclasses.dataclass if not already imported.

Test Plan

Updated test suite:

python -m unittest libcst.codemod.commands.tests.test_convert_namedtuple_to_dataclass

Run entire test suite:

tox test

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 25, 2020
new_bases = []
namedtuple_base = None

for base_class in original_node.bases:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want original_node here? If so, worth a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that the metadata dict is directly tied to the original node's bases, so updated_node.bases does not work with the QualifiedNameProvider here. Added a comment to acknowledge this 👍

Copy link
Contributor

@thatch thatch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@jimmylai jimmylai requested review from carljm and mvismonte May 26, 2020 15:46
@josieesh josieesh requested a review from jimmylai May 26, 2020 15:52
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!! Awesome work. All my comments are just nits, nothing important to change.

Do we want documentation for this, or will we get autogenerated reference docs from the docstring?

NamedTuple-specific attributes and methods.
"""

DESCRIPTION: str = "Convert legacy NamedTuple class declarations to Python 3.7 dataclasses."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit, but probably best to leave out the word "legacy" here and avoid provoking any pedantry or misleading anyone, since in upstream Python NamedTuple is not (as far as I know) deprecated or discouraged in any way. In our codebases we prefer to convert NamedTuple to dataclass, but that doesn't make NamedTuple "legacy" in general.


# Need to examine the original node's bases since they are directly tied to import metadata
for base_class in original_node.bases:
# Compare the base class's qualified named against the expected typing.NamedTuple
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Compare the base class's qualified named against the expected typing.NamedTuple
# Compare the base class' qualified name against the expected typing.NamedTuple

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carljm class's is legal for singular, which this is one at a time. did you mean classes' to talk about the whole operation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thatch Oops, yeah I was thinking of the plural. Made the comment due to the extraneous d and the other part was an afterthought that clearly needed a bit more thought :)

Comment on lines 40 to 42
qualified_namedtuple: QualifiedName = QualifiedName(
name=f"{self.MODULE}.{self.OBJECT}", source=QualifiedNameSource.IMPORT
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is immutable and creating it has no side effects; any reason not to just make the entire QualifiedName object a class attribute, and not have to construct it anew each time we leave a class definition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point

Comment on lines +71 to +72
lpar=cst.MaybeSentinel.DEFAULT,
rpar=cst.MaybeSentinel.DEFAULT,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these two lines necessary? What fails without them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They account for the scenario where all of the base classes have been removed, so that we default to class Foo: instead of class Foo():

… changed immutable variable to class attribute
@jimmylai
Copy link
Contributor

jimmylai commented May 27, 2020

Do we want documentation for this, or will we get autogenerated reference docs from the docstring?

We only have the document for transform of libraries (https://libcst.readthedocs.io/en/latest/codemods.html#library-of-transforms) and we can automatically generate the list of available CodemodCommands to keep the document update easier. We'll cover documentation later in Milestone 3 and @josieesh can work on it at that time (It requires writing some code to generate the rst for Sphinx and we already have something similar for our lint rules.)

@josieesh josieesh merged commit 7462265 into Instagram:master May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants