pyright fixes for ext/io/phonon/symmetry/transformations/util/vis/dev_scripts and improve io.lobster#3757
Conversation
- Hopefully all tests have been written in such a way that they will be skipped if the underlying required package is not found
Signed-off-by: Matthew Evans <7916000+ml-evs@users.noreply.github.com>
WalkthroughThe recent updates across the pymatgen library focus on improving code quality through enhanced readability, efficiency, and robustness. Changes include variable renaming for clarity, refactoring for better logic flow, and the integration of modern Python features like the walrus operator. Additionally, stricter type checking with Changes
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
pymatgen/io/pwscf.py
Outdated
| Lattice(lattice), | ||
| species, | ||
| coords, | ||
| # DEBUG (@DanielYang59): need input on coords_are_cartesian |
There was a problem hiding this comment.
Need help on io.pwscf please @janosh (I never use pwscf): coords_are_cartesian is only defined in one of the many branches, and is unbound all elsewhere:
Lines 240 to 355 in 37d1066
I currently set the default coords_are_cartesian to False (the default of Structure), but I'm not sure if it's the default for pwscf too. Would need confirmation on this.
There was a problem hiding this comment.
wow, so many code branches and only one of them was actually tested?
pymatgen/tests/io/test_pwscf.py
Lines 360 to 367 in 37d1066
this seems in urgent need of more tests. i think defaulting coords_are_cartesian=False seems right. i also never used this code so it's hard to know for sure. apparently no one does or this would have been reported long ago
There was a problem hiding this comment.
so many code branches and only one of them was actually tested?
Yes, coords_are_cartesian is defined in only one of the many branches and is unbound all elsewhere 😓, meaning the other branches were likely never entered.
No problem, this is great work! It looks this was fairly comprehensive; I'll try reopening my PR that adds the CI runs that triggered this. |
This is only for some modules. More work is needed |
Yes! It's only like 1/3 of the all work needed. I would ping you once it's all ready. @ml-evs |
Understood, I've opened #3759 with the same changes as before (except enabling pyright), hopefully the CI is now stable enough after the |
@DanielYang59 thanks a lot for donating your time to work on this! 👍 |
Glad I can help :) |
|
@janosh I'm still experimenting on an issue I met with @JaGeo in #3757 (comment). I would appreciate it if you could give me some advice 😄 I just created a minimal code to recreate this issue: from collections import UserDict
class TestClass(UserDict):
def __init__(self, settingsdict: dict):
super().__init__()
self.update(settingsdict)
def __setitem__(self, key, val):
"""Remove case sensitivity."""
new_key = next((key_here for key_here in self if key.strip().lower() == key_here.lower()), key)
super().__setitem__(new_key, val)
def __getitem__(self, item):
"""Implements getitem from dict to avoid problems with cases."""
new_item = next((key_here for key_here in self if item.strip().lower() == key_here.lower()), item)
return super().__getitem__(new_item)
if __name__ == "__main__":
test_dict = {"ConTent": "hello", }
test_instance = TestClass(test_dict)
print(f"{test_instance=}")
print(test_instance.get("content"), test_instance["content"])On this same machine (MacOS Sonoma 14.4.1, Apple Silicon and Python managed by miniconda3). With Python 3.10 and 3.11, I got: (base) yang@Yang-MacBook test % python3 -V
Python 3.10.14
(base) yang@Yang-MacBook test % python3 recreate.py
test_instance={'ConTent': 'hello'}
hello hello(base) yang@Yang-MacBook test % python3 -V
Python 3.11.8
(base) yang@Yang-MacBook test % python3 recreate.py
test_instance={'ConTent': 'hello'}
hello helloWhile with Python 3.12, the two methods get different results: (base) yang@Yang-MacBook test % python3 -V
Python 3.12.2
(base) yang@Yang-MacBook test % python3 recreate.py
test_instance={'ConTent': 'hello'}
None helloIs it some known issue/change from Python 3.12? |
|
Ah, interesting, Python version 😅. |
|
@DanielYang59 I think I found it: python/cpython#105524 |
👍 So speedy. Yes looks like exactly what we're experiencing. I guess it's not intended? The def get(self, key, default=None):
if key in self:
return self[key]
return defaultWhich indeed does not use But at least confirmed my machine is not the "outlier" 😅 |
No idea if intended. |
|
there's only 1 use of |
|
I think we recently switched to UserDict for other issues. |
I would keep it in mind. I'm now secretly working on simplifying |
Summary
Started by @ml-evs in #3646:
pyrightto CI (delayed until all chunks are finished)pyrightfixesio.lobsterby @JaGeosourcery), I should not have done this (make it hard to view difference)Need confirmation on the following:
pyrightfixes forext/io/phonon/symmetry/transformations/util/vis/dev_scriptsand improveio.lobster#3757 (comment)pyrightis commented out until all chunks are finished, otherwise everyone's CI would fail.There are too many (713)
pyrighterrors to fix in a single PR, I'm planning to chop up into chunks. First chunk would fix the following modules:Side note
io.lobster.inputsonly failed on my MacOS machine:pyrightfixes forext/io/phonon/symmetry/transformations/util/vis/dev_scriptsand improveio.lobster#3757 (comment)io.lobsterdiffmethod could potentially be simplified:pyrightfixes forext/io/phonon/symmetry/transformations/util/vis/dev_scriptsand improveio.lobster#3757 (comment)Summary by CodeRabbit
Summary by CodeRabbit
New Features
Bug Fixes
Refactor
Documentation
Style
Tests