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

Fix typing_extension ImportError in downstream packages #3752

Merged
merged 5 commits into from
Apr 12, 2024

Conversation

janosh
Copy link
Member

@janosh janosh commented Apr 12, 2024

closes #3751

Summary by CodeRabbit

  • Refactor

    • Improved code readability by consolidating imports in core modules.
    • Enhanced type hinting and conditional type checking across various modules.
  • Tests

    • Updated test functions with specific error messages for better clarity.
  • Chores

    • Updated revision of pre-commit hooks to ensure code quality.

@janosh janosh added fix Bug fix PRs types Type all the things labels Apr 12, 2024
@janosh janosh enabled auto-merge (squash) April 12, 2024 17:37
Copy link

coderabbitai bot commented Apr 12, 2024

Walkthrough

The recent updates to the pymatgen codebase involve enhancing type handling and code readability. Changes include updating the ruff-pre-commit repository version, consolidating imports in core modules, and adding conditional imports for improved type checking.

Changes

File Path Change Summary
.pre-commit-config.yaml Updated ruff-pre-commit repository to v0.3.7
pymatgen/core/... Grouped imports for better readability
pymatgen/analysis/...
pymatgen/ext/...
pymatgen/io/abinit/...
Added TYPE_CHECKING and conditional Self imports
tests/io/test_packmol.py Added import re, error message constant, and test modifications
tests/transformations/... Removed unnecessary comment directive

Assessment against linked issues

Objective Addressed Explanation
Import typing_extension at runtime causing ImportError [#3751]

Poem

🐇 Hop, hop, hooray! Let's celebrate today,
Code has been groomed, with imports well-tuned.
From Self imports sly, to a version bump high,
Every line and dot, makes our code a top-notch spot!
🚀 Let's code away, in a much smoother way!


Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 666db91 and 16af9e4.
Files selected for processing (1)
  • tests/io/test_packmol.py (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/io/test_packmol.py

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.

@janosh janosh force-pushed the fix-gh-3751-typ-ext-import branch from 32c320e to 16af9e4 Compare April 12, 2024 19:25
@janosh janosh merged commit f81535f into master Apr 12, 2024
22 checks passed
@janosh janosh deleted the fix-gh-3751-typ-ext-import branch April 12, 2024 19:36
@kavanase
Copy link
Contributor

Hi @janosh @DanielYang59 !
Sorry to be the bearer of bad news, but this issue has cropped up again in the recent release of pymatgen, which again is causing failures in our CI for doped/ShakeNBreak (and I imagine other downstream packages of pymatgen).
(https://github.com/SMTG-Bham/ShakeNBreak/actions/runs/9323684325/job/25667402879, https://github.com/SMTG-Bham/doped/actions/runs/9323686497).

The issue is that typing_extensions is now imported before the TYPE_CHECKING block in pymatgen.core.structure, added in this commit: d0ad785
As far as I can tell, it's correctly imported in the TYPE_CHECKING block in all other usages, but might be worth a quick check.

@janosh
Copy link
Member Author

janosh commented May 31, 2024

sorry for the trouble!

we'll get a fix in asap ofc but really we need to setup CI that only installs non-optional deps so we catch these mistakes in the future.

@kavanase
Copy link
Contributor

Also minor issue that doesn't really matter but might be useful to know about, pymatgen.core.periodic_table uses the monty.dev @deprecated() decorator with the deadline keyword argument, but this was only added by @DanielYang59 in v2024.3.31 of monty, but this isn't a version requirement of pymatgen, so when I updated to the new pymatgen it was throwing top_level_collect error messages about this until I upgrade monty.
So probably worth adding monty>=2024.3.31 to the dependencies!

@janosh
Copy link
Member Author

janosh commented May 31, 2024

that should be easy to prevent in future since we use uv which offers --resolution=lowest to install the oldest allowed versions of all packages

@DanielYang59
Copy link
Contributor

DanielYang59 commented Jun 1, 2024

Sorry for the trouble, again. And I really want some automated import guard against TYPE_CHECKING only imports #3827, which would prevent such issues.

Also minor issue that doesn't really matter but might be useful to know about, pymatgen.core.periodic_table uses the monty.dev @deprecated() decorator with the deadline keyword argument, but this was only added by @DanielYang59 in v2024.3.31 of monty, but this isn't a version requirement of pymatgen, so when I updated to the new pymatgen it was throwing top_level_collect error messages about this until I upgrade monty. So probably worth adding monty>=2024.3.31 to the dependencies!

Looks like I forgot about the monty version in setup.py:

pymatgen/setup.py

Lines 30 to 32 in 2982968

install_requires=[
"matplotlib>=1.5",
"monty>=2024.2.2",

And updated the monty version in requirements.txt alone:

monty==2024.5.24

Perhaps we should reuse requirements.txt in setup.py instead of having duplicates? Maybe there are reasons that I'm not aware of?

@janosh
Copy link
Member Author

janosh commented Jun 1, 2024

Perhaps we should reuse requirements.txt in setup.py instead of having duplicates? Maybe there are reasons that I'm not aware of?

better yet, get rid of requirements.txt as i've been saying for years. no good reasons for keeping it imo

@JaGeo
Copy link
Member

JaGeo commented Jun 1, 2024

I would also be pro removing. Would it actually make sense to have a pymatgen[strict] similar to atomate2[strict] to have exact version numbers of all packages as well?

janosh added a commit that referenced this pull request Jun 3, 2024
…lowest-direct` (#3852)

* fix typing_extensions ImportError

#3752 (comment)

* bump min ase to 3.23.0

* test both lowest-direct and highest uv package resolutions in CI

* establish some loose lower bounds on all deps

needed for deps install with --resolution=lowest-direct to work

* netcdf4>=1.6

* pin chgnet==0.3.6

* try chgnet==0.3.5

* bump pre-commit hooks

* imperative doc strings

* doc str white space

* test_string->test_str

* ruff auto fixes

* matgl>=1.1.1 numba>=0.55

* try fix netcdf4 install

* h5py>=3.9.0

* monty>=2024.5.24

fixes TypeError: deprecated() got an unexpected keyword argument 'deadline'

* revert TestWavecar to again inherit from PymatgenTest

* h5py>=3.11.0

* phonopy>=2.23

* scipy>=1.13.0

* matplotlib>=3.8

* requests>=2.32

* don't run oldest/newest supported python twice on linux AND windows

* fix bad refactor in get_atom_map

fixes KeyError: 'Co' ipot = self.pot_dict[el.symbol]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix PRs types Type all the things
Projects
None yet
Development

Successfully merging this pull request may close these issues.

typing_extension imported at run time causing ImportError
4 participants