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

Re-enable some useful ruff rules #3813

Merged
merged 11 commits into from
May 7, 2024

Conversation

DanielYang59
Copy link
Contributor

@DanielYang59 DanielYang59 commented May 7, 2024

Re-enable some useful ruff rules

REVERTED

Copy link

coderabbitai bot commented May 7, 2024

Note

Reviews Paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The recent updates across various files in the pymatgen library primarily involve the introduction of ClassVar type annotations to class attributes, enhancing clarity on which variables are class-level constants. This change streamlines the handling of configurations and settings across the library, ensuring that these attributes are correctly recognized as static properties of their respective classes.

Changes

Files Change Summary
.../chemenv/..., .../eos.py, .../composition.py, .../xcfunc.py, .../molecule_structure_comparator.py Added ClassVar to class dictionary attributes to clarify their usage as static class properties.
.../io/abinit/..., .../io/adf.py, .../io/icet.py, .../io/nwchem.py, .../io/pwscf.py, .../io/vasp/inputs.py Introduction of ClassVar annotations for various class attributes to enforce their static nature.
.../symmetry/groups.py Type annotations (ClassVar) added to dictionaries to explicitly define them as class-level attributes.
pyproject.toml Modifications in linting rules, including the removal of specific checks and adjustments in documentation and exception handling practices.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@DanielYang59
Copy link
Contributor Author

@coderabbitai ignore

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Out of diff range and nitpick comments (5)
pymatgen/io/vasp/inputs.py (5)

Line range hint 1747-1765: Ensure consistent use of ClassVar for class-level constants.

-    functional_dir: ClassVar = {
+    functional_dir: ClassVar[dict[str, str]] = {

Line range hint 1766-1780: Ensure consistent use of ClassVar for class-level constants.

-    functional_tags: ClassVar = {
+    functional_tags: ClassVar[dict[str, dict[str, str]]] = {

Line range hint 1780-1804: Ensure consistent use of ClassVar for class-level constants.

-    parse_functions: ClassVar = {
+    parse_functions: ClassVar[dict[str, Callable[[str], Any]]] = {

Line range hint 1804-1823: Consider adding type hints to the constructor parameters for clarity.

-    def __init__(self, data: str, symbol: str | None = None) -> None:
+    def __init__(self, data: str, symbol: Optional[str] = None) -> None:

Line range hint 1823-1842: Consider adding type hints to the constructor parameters for clarity.

-    def __init__(self, incar: dict | Incar, kpoints: Kpoints | None, poscar: Poscar, potcar: Potcar | None, optional_files: dict[PathLike, object] | None = None, **kwargs) -> None:
+    def __init__(self, incar: Union[dict, Incar], kpoints: Optional[Kpoints], poscar: Poscar, potcar: Optional[Potcar], optional_files: Optional[dict[PathLike, object]] = None, **kwargs) -> None:

Copy link

coderabbitai bot commented May 7, 2024

@coderabbitai ignore

Actions Performed

Reviews paused.

@DanielYang59 DanielYang59 changed the title Make ruff stricter by enabling some rules Re-enable some useful disabled ruff rules May 7, 2024
@DanielYang59 DanielYang59 changed the title Re-enable some useful disabled ruff rules Re-enable some useful ruff rules May 7, 2024
@DanielYang59 DanielYang59 marked this pull request as draft May 7, 2024 09:18
Copy link
Member

@janosh janosh left a comment

Choose a reason for hiding this comment

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

i actually often prefer the dict(foo=bar) version over {"foo": bar} since it's more compact due to not needing so many quotes. this sometimes avoids a line break which imo helps readability. i'd vote to keep C408 in the ignore list

@DanielYang59
Copy link
Contributor Author

DanielYang59 commented May 7, 2024

i actually often prefer the dict(foo=bar) version over {"foo": bar} since it's more compact due to not needing so many quotes. this sometimes avoids a line break which imo helps readability.

dict(foo=bar) has one more character than {"foo": bar} including the whitespace. So I assume there is no reason dict() is more compact than {}?

And it seems {} should be faster than dict():

It's unnecessary to call, e.g., dict() as opposed to using an empty literal ({}). The former is slower because the name dict must be looked up in the global scope in case it has been rebound.

@DanielYang59 DanielYang59 marked this pull request as ready for review May 7, 2024 11:12
@DanielYang59
Copy link
Contributor Author

DanielYang59 commented May 7, 2024

It should be ready for review now @janosh. Thanks. Let me know if want to enable any more/less ruff rules.

I tried to enable:

"PERF401", # manual-list-comprehension (TODO fix these or wait for autofix)

But it seems to force all kind of list builder into list comprehensions (especially some nested conditions list), which would greatly reduce readability.

@janosh
Copy link
Member

janosh commented May 7, 2024

dict(foo=bar) is 13 characters and {"foo": bar} is 12 (including the whitespace).

that inverts once you have 2 or more keys in your dict. at that point the collection call is shorter

@DanielYang59
Copy link
Contributor Author

DanielYang59 commented May 7, 2024

dict(foo=bar) is 13 characters and {"foo": bar} is 12 (including the whitespace).

that inverts once you have 2 or more keys in your dict. at that point the collection call is shorter

That's true. But if you look into the commit 38a5aa7, it appears among all changes, this case you mentioned (where a line is broken by extra characters) only happened once for:

updates = dict(LCHARG=False, NELM=60, PREC="Normal", EDIFFG=-0.02, ICHARG=0, NSW=200, EDIFF=0.0001)

And all other changes didn't introduce any additional line.

But anyway, feel free to revert that commit if you really prefer not to change that (it's all automatically changed by ruff so wasn't any effort from me). But at least we should search and replace some list() or dict().

Copy link
Member

@janosh janosh left a comment

Choose a reason for hiding this comment

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

thanks @DanielYang59, lgtm! 👍

@janosh janosh enabled auto-merge (squash) May 7, 2024 11:45
@janosh janosh added the linting Linting and quality assurance label May 7, 2024
@DanielYang59
Copy link
Contributor Author

Beautiful. Thanks for reviewing @janosh Appreciate it.

@janosh janosh merged commit a9da16e into materialsproject:master May 7, 2024
23 checks passed
@DanielYang59 DanielYang59 deleted the stricter-ruff branch May 7, 2024 11:55
"D105", # Missing docstring in magic method
"D205", # 1 blank line required between summary line and description
"D212", # Multi-line docstring summary should start at the first line
"ISC001",
"NPY002", # replace legacy numpy.random with numpy.random.Generator
"PD901", # pandas-df-variable-name
"PERF203", # try-except-in-loop
"PERF401", # manual-list-comprehension (TODO fix these or wait for autofix)
Copy link
Contributor Author

@DanielYang59 DanielYang59 May 7, 2024

Choose a reason for hiding this comment

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

I forgot to remove this TODO tag for PERF401, would do it in another PR.

I'm pretty sure we don't really want to enable this rule:

it seems to force all kind of list builder into list comprehensions (especially some nested conditions list), which would greatly reduce readability.

@DanielYang59
Copy link
Contributor Author

DanielYang59 commented May 8, 2024

I just look into this speed comparison between dict() and {}, and it surprised me as the speed improvement seems quite significant (could be more than 2x the time for dict()). I thought it was just minor improvement like ~10%.

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

Successfully merging this pull request may close these issues.

2 participants