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

Added --name argument #131

Merged
merged 6 commits into from
Sep 21, 2020
Merged

Added --name argument #131

merged 6 commits into from
Sep 21, 2020

Conversation

acharles7
Copy link
Contributor

@acharles7 acharles7 commented Sep 17, 2020

Summary

Refer #119

  • Added --name argument to fixit's add_new_rule CLI
    python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py --name abcd

--file = fixit/rules/my_rule.py
--name = abcd

The file will be created at fixit/rules/my_rule.py with class name abcd as AbcdRule

class AbcdRule(CstLintRule):
    """
    docstring or new_rule description
    """

    MESSAGE = "Enter rule description message"

    VALID = [Valid("'example'")]

    INVALID = [Invalid("'example'")]
  • Added snake_to_camelcase function to fixit/cli/utils.py
  • Added documentation to docs/source/build_a_lint_rule.ipynb

Test Plan

@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 Sep 17, 2020
@acharles7
Copy link
Contributor Author

Please have a look at the docs builds error.
We have to avoid using In[2] to write in examples.py

@acharles7 acharles7 changed the title Added --name argument to Added --name argument Sep 17, 2020
@acharles7
Copy link
Contributor Author

@jimmylai Can you please look into it?

Copy link
Contributor

@isidentical isidentical left a comment

Choose a reason for hiding this comment

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

Just suggestion, but how would it look using the path (like chained_isinstance_check.py) when there is no name supplied (convert the path's stem into ChainedIsinstanceCheck)

def create_rule_file(file_path: Path) -> None:
def is_valid_name(name: str) -> str:
"""Check for valid rule name """
if name.endswith(("Rule", "Rules", "rule", "rules")):
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
if name.endswith(("Rule", "Rules", "rule", "rules")):
if name.casefold().endswith(("rule", "rules")):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

def is_valid_name(name: str) -> str:
"""Check for valid rule name """
if name.endswith(("Rule", "Rules", "rule", "rules")):
raise NameError("Please enter rule name without the suffix, `rule` or `Rule`")
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be more consistent if you use single quotes instead of backticks (just like KeyError etc.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, NameError doesn't suit well, maybe use ValueError?

"""Check for valid rule name """
if name.endswith(("Rule", "Rules", "rule", "rules")):
raise NameError("Please enter rule name without the suffix, `rule` or `Rule`")
return snake_to_camelcase(name) if name else ""
Copy link
Contributor

Choose a reason for hiding this comment

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

You can omit if name else "" since snake_to_camelcase with empty string would result with an empty string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@acharles7
Copy link
Contributor Author

Just suggestion, but how would it look using the path (like chained_isinstance_check.py) when there is no name supplied (convert the path's stem into ChainedIsinstanceCheck)

Done. Nice idea
Thanks

}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing newline at the end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@jimmylai
Copy link
Contributor

Please have a look at the docs builds error.
We have to avoid using In[2] to write in examples.py

For some reason, this error just show up recently.
In[2] is a way to access the content of cell number 2 in the notebook to reuse the code example.
tox -e docs run successfully in my local dev. It seems specific to the CI build.
Are you able to reproduce this error locally?

@acharles7
Copy link
Contributor Author

Please have a look at the docs builds error.
We have to avoid using In[2] to write in examples.py

For some reason, this error just show up recently.
In[2] is a way to access the content of cell number 2 in the notebook to reuse the code example.
tox -e docs run successfully in my local dev. It seems specific to the CI build.
Are you able to reproduce this error locally?

I am also getting this error in local dev.

@acharles7
Copy link
Contributor Author

We can use something like this.

file_content = """\
class C:
...content of In[2]
"""

then write file_content to examples.py

Comment on lines 67 to 92
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbsphinx": "hidden"
},
"outputs": [],
"source": [
"file_content = \"\"\"\\\n",
"from typing import Dict\n",
"\n",
"\n",
"class C(object):\n",
" attr = \"ab\" \"cd\" \"ef\" \"gh\"\n",
"\n",
" def method(self) -> Dict[int, str]:\n",
" filtered_char = []\n",
" for char in self.attr:\n",
" if char is not \"a\":\n",
" filtered_char.append(char)\n",
"\n",
" index_to_char = dict([(idx, char) for idx, char in enumerate(filtered_char)])\n",
" return index_to_char\n",
"\"\"\""
]
},
Copy link
Contributor

@jimmylai jimmylai Sep 20, 2020

Choose a reason for hiding this comment

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

Copy the example code is not ideal. It'll make the future updated less easy.

I've found a workaround in #133

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll revert it. Thanks

Comment on lines 142 to 143
"- ``--path`` is used to create rule file at path given in ``--path``, Default is ``fixit/rules/new.py``\n",
"- ``--name`` is used to assign the name of the rule and should be in snake case, Default is name of the rule file if ``path`` provided else `new`. Otherwise, considers the value specified in ``--name``\n",
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
"- ``--path`` is used to create rule file at path given in ``--path``, Default is ``fixit/rules/new.py``\n",
"- ``--name`` is used to assign the name of the rule and should be in snake case, Default is name of the rule file if ``path`` provided else `new`. Otherwise, considers the value specified in ``--name``\n",
"- ``--path`` is used to create rule file at path given in ``--path``, defaults to ``fixit/rules/new.py``\n",
"- ``--name`` is used to assign the name of the rule and should be in snake case, defaults to the rule file name if ``path`` provided else `new`. Otherwise, considers the value specified in ``--name``\n",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

"--name",
type=str,
default="",
help="Name of the rule, Default is New",
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
help="Name of the rule, Default is New",
help="Name of the rule, defaults to `New`",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@jimmylai jimmylai left a comment

Choose a reason for hiding this comment

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

Other than comments, LGTM.
Thanks for contributing!

@jimmylai jimmylai merged commit aaa3446 into Instagram:master Sep 21, 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.

4 participants