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

Make typing-extensions and sympy required dependencies to fix PyBaMM import #3848

Merged
merged 13 commits into from
Mar 5, 2024

Conversation

arjxn-py
Copy link
Member

Description

Fix broken import of PyBaMM raised by type hinting.

Fixes #3836

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All tests pass: $ python run-tests.py --all (or $ nox -s tests)
  • The documentation builds: $ python run-tests.py --doctest (or $ nox -s doctests)

You can run integration tests, unit tests, and doctests together at once, using $ python run-tests.py --quick (or $ nox -s quick).

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@arjxn-py arjxn-py changed the title Make typing-extensions required dependency and import sympy as optional to fix import Make typing-extensions required dependency & import sympy as optional to fix import Feb 28, 2024
Copy link
Contributor

@kratman kratman left a comment

Choose a reason for hiding this comment

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

I would say that we need SymPy. It is a requirement for expression tree if it uses the types from the module. We should probably just make SymPy required instead.

If expression tree is not needed by everything else, then we could make expression tree optional, but I think SymPy is clearly required by expression tree

Copy link

codecov bot commented Feb 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.60%. Comparing base (ed6df7e) to head (ad958a2).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3848      +/-   ##
===========================================
- Coverage    99.60%   99.60%   -0.01%     
===========================================
  Files          259      259              
  Lines        21284    21261      -23     
===========================================
- Hits         21200    21177      -23     
  Misses          84       84              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@agriyakhetarpal agriyakhetarpal linked an issue Mar 1, 2024 that may be closed by this pull request
Copy link
Member

@agriyakhetarpal agriyakhetarpal left a comment

Choose a reason for hiding this comment

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

Thanks, @arjxn-py – this looks better now. To ensure that this doesn't happen again, could you please add some more tests for the have_optional_dependency method?

In particular, to do this we can either open and read the pyproject.toml file to extract the list of dependencies we have, or use importlib.metadata (the latter is better):

import importlib.metadata

importlib.metadata.metadata("pybamm").json['requires_dist']

returns this list of strings:

['numpy >=1.23.5',
 'scipy >=1.9.3',
 'casadi >=3.6.3',
 'xarray >=2022.6.0',
 'anytree >=2.8.0',
 "autograd >=1.6.2 ; extra == 'all'",
 "scikit-fem >=8.1.0 ; extra == 'all'",
 "pybamm[bpx,cite,examples,latexify,pandas,plot,tqdm] ; extra == 'all'",
 "bpx >=0.4.0 ; extra == 'bpx'",
 "bibtexparser >=2.0.0b5 ; extra == 'cite'",
 "pre-commit ; extra == 'dev'",
 "ruff ; extra == 'dev'",
 "nox ; extra == 'dev'",
 "coverage[toml] ; extra == 'dev'",
 "pytest >=6 ; extra == 'dev'",
 "pytest-xdist ; extra == 'dev'",
 "nbmake ; extra == 'dev'",
 "sphinx >=6 ; extra == 'docs'",
 "sphinx-rtd-theme >=0.5 ; extra == 'docs'",
 "pydata-sphinx-theme ; extra == 'docs'",
 "sphinx-design ; extra == 'docs'",
 "sphinx-copybutton ; extra == 'docs'",
 "myst-parser ; extra == 'docs'",
 "sphinx-inline-tabs ; extra == 'docs'",
 "sphinxcontrib-bibtex ; extra == 'docs'",
 "sphinx-autobuild ; extra == 'docs'",
 "sphinx-last-updated-by-git ; extra == 'docs'",
 "nbsphinx ; extra == 'docs'",
 "ipykernel ; extra == 'docs'",
 "ipywidgets ; extra == 'docs'",
 "sphinx-gallery ; extra == 'docs'",
 "sphinx-hoverxref ; extra == 'docs'",
 "sphinx-docsearch ; extra == 'docs'",
 "jupyter ; extra == 'examples'",
 'jax ==0.4.20 ; (python_version >= "3.9") and extra == \'jax\'',
 'jaxlib ==0.4.20 ; (python_version >= "3.9") and extra == \'jax\'',
 "sympy >=1.12 ; extra == 'latexify'",
 "scikits.odes ; extra == 'odes'",
 "pandas >=1.5.0 ; extra == 'pandas'",
 "imageio >=2.3.0 ; extra == 'plot'",
 "matplotlib >=3.6.0 ; extra == 'plot'",
 "tqdm ; extra == 'tqdm'"]

from which we should be able to clean and parse all of the optional dependencies. The json attribute was added in Python 3.10, so for versions prior to that we can perhaps read importlib.metadata.metadata("pybamm")["Provides-Extra"] (note that there are multiple keys of the same name "Provides-Extra"]).

P.S. The best resource for the job would be the PyPA packaging package (https://packaging.pypa.io/en/stable/metadata.html), but that is an external dependency and not part of the stdlib

CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
@arjxn-py
Copy link
Member Author

arjxn-py commented Mar 4, 2024

To ensure that this doesn't happen again, could you please add some more tests for the have_optional_dependency method?

Thanks for the review @agriyakhetarpal, but just mocking all optional dependencies to tests won't be enough as we currently we run tests on pybamm[all] variant. I should need to create a new job where i just install pybamm base variant and then run necessary tests. But considering the priority of this issue i feel it'd be right to open a separate PR for that.

@agriyakhetarpal
Copy link
Member

Yes, a follow-up PR should be okay – we should target that soon after this PR so that we are properly testing all dependencies. It should be nothing more than another entry to the GitHub Actions matrix that can be accessed in the context of a step

@agriyakhetarpal
Copy link
Member

Could you fix the example notebooks failure here? It does look like a small fix – not sure how it was passing earlier

@arjxn-py
Copy link
Member Author

arjxn-py commented Mar 4, 2024

Could you fix the example notebooks failure here? It does look like a small fix – not sure how it was passing earlier

Sure

@arjxn-py
Copy link
Member Author

arjxn-py commented Mar 4, 2024

Could you fix the example notebooks failure here? It does look like a small fix – not sure how it was passing earlier

The failure looks unrelated and inconsistent, please check the runs here. It is passing currently but if it remains an issue we shall look what causing it in the first place.

@kratman
Copy link
Contributor

kratman commented Mar 4, 2024

Just a question: Is have_optional_dependency() used anywhere else? It looks like it was used pretty heavily for sympy. If it is was not used anywhere else then can it be removed as well?

@agriyakhetarpal
Copy link
Member

agriyakhetarpal commented Mar 4, 2024

It is currently used for matplotlib and scikit-fem, which seem to be more optional in their use than sympy. matplotlib was originally the only dependency that was implemented to be optional before PyBaMM v23.5 (but it was not marked as such).

At some point we should consider renaming the function to import_optional_dependency to align it with the fact that it is just a wrapper over importlib.import_module() to raise a helpful error message

@kratman
Copy link
Contributor

kratman commented Mar 5, 2024

At some point we should consider renaming the function to import_optional_dependency to align it with the fact that it is just a wrapper over importlib.import_module() to raise a helpful error message

Yeah that would be a way better name. We should just start fixing that sort of thing on the fly as we see it

@kratman kratman self-requested a review March 5, 2024 00:07
@kratman kratman dismissed their stale review March 5, 2024 00:08

My complaints were addressed

@agriyakhetarpal agriyakhetarpal changed the title Make typing-extensions required dependency & import sympy as optional to fix import Make typing-extensions and sympy required dependencies to fix PyBaMM import Mar 5, 2024
Copy link
Member

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

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

Thanks, @arjxn-py!

CHANGELOG.md Outdated Show resolved Hide resolved
@kratman kratman merged commit e29d30e into pybamm-team:develop Mar 5, 2024
47 checks passed
@agriyakhetarpal agriyakhetarpal deleted the #3836-broken-import branch March 5, 2024 15:29
@arjxn-py arjxn-py mentioned this pull request Mar 5, 2024
6 tasks
lorenzofavaro pushed a commit to lorenzofavaro/PyBaMM that referenced this pull request Mar 13, 2024
…aMM import (pybamm-team#3848)

* import `typing_extensions` as optional_dependency

* Make `typing-extensions` a required dependency

* Try forward referencing for `sympy` in `IndependentVariable`

* Make `sympy` a required dependency

* Update docs for SymPy

* Import `sympy` without `have_optional_dependency`

* Import `sympy` without `TYPE_CHECKING`

* Changelog

* Update CHANGELOG.md

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>

* Rearrange position in changelog

---------

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>
agriyakhetarpal added a commit that referenced this pull request Mar 13, 2024
* Enable flake8-bugbear

Fix unused-loop-control-variable (B007)

Fix function-uses-loop-variable (B023)

Fix raise-without-from-inside-except (B904)

Fix mutable-argument-default (B006)

Fix no-explicit-stacklevel (B028)

Fix get-attr-with-constant (B009)

Fix loop-variable-overrides-iterator (B020)

Fix unary-prefix-increment-decrement (B002)

Fix function-call-in-default-argument (B008)

Fix cached-instance-method (B019)

* Show exception chain (B904)

* Disable single lint check (B019)

* delay xarray.DataArray initialization

* revert example

* Bump the actions group with 1 update (#3861)

Bumps the actions group with 1 update: [awalsh128/cache-apt-pkgs-action](https://github.com/awalsh128/cache-apt-pkgs-action).


Updates `awalsh128/cache-apt-pkgs-action` from 1.4.1 to 1.4.2
- [Release notes](https://github.com/awalsh128/cache-apt-pkgs-action/releases)
- [Commits](awalsh128/cache-apt-pkgs-action@v1.4.1...v1.4.2)

---
updated-dependencies:
- dependency-name: awalsh128/cache-apt-pkgs-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Make `typing-extensions` and `sympy` required dependencies to fix PyBaMM import (#3848)

* import `typing_extensions` as optional_dependency

* Make `typing-extensions` a required dependency

* Try forward referencing for `sympy` in `IndependentVariable`

* Make `sympy` a required dependency

* Update docs for SymPy

* Import `sympy` without `have_optional_dependency`

* Import `sympy` without `TYPE_CHECKING`

* Changelog

* Update CHANGELOG.md

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>

* Rearrange position in changelog

---------

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>

* Rename have_optional_dependency (#3866)

* Rename have_optional_dependency

* Change log

* Fix import

* style: pre-commit fixes

* Update pybamm/util.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Arjun Verma <arjunverma.oc@gmail.com>

* update changelog for #3862 (#3869)

* update changelog for #3862

* reword

* fix deprecation warning and check for electrode/particle diffusivity

* update test

* Edit naming for unused loop control variable (B007)

* #3883 updates to install from source (#3884)

* Disable benchmarks on PRs (#3876)

* Remove`[ latexify` extra (#3888)

* Improve Getting Started notebooks (#3750)

* improve tutorials 1 to 3

* use new syntax for plot_voltage_components

* style: pre-commit fixes

* simplify notebook by removing drive cycle

* add additional comment on dynamic_plot

* improve tutorial 5

* remove the use of solution in notebook 5, this is introduced in notebook 6

* fix minor typo

* minor text updates

* style: pre-commit fixes

* minor text updates plus extra links

* minor text updates!

* minor text updates

* remove tutorial 10 as very similar to tutorial 3 in creating models

* move and rename tutorial 11 to creating models section

* style: pre-commit fixes

* fix minor typo in current_with_time

* implement Rob & Eric's comments

* update docs links

* Valentin's comments

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>

* chore: update pre-commit hooks (#3890)

* chore: update pre-commit hooks

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](astral-sh/ruff-pre-commit@v0.2.2...v0.3.2)

* style: pre-commit fixes

* Update .git-blame-ignore-revs

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>

* style: pre-commit fixes

* Resolve conflicts (B904, B028, B020)

* Added a schedule on `needs-reply remove` workflow  (#3891)

* added schedule to need-reply workflow

* added schedule to need-reply workflow

---------

Co-authored-by: Arjun Verma <arjunverma.oc@gmail.com>

* style: pre-commit fixes

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Valentin Sulzer <valentinsulzer@hotmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Arjun Verma <arjunverma.oc@gmail.com>
Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Martin Robinson <martinjrobins@gmail.com>
Co-authored-by: Ferran Brosa Planella <Ferran.Brosa-Planella@warwick.ac.uk>
Co-authored-by: Santhosh <52504160+santacodes@users.noreply.github.com>
js1tr3 pushed a commit to js1tr3/PyBaMM that referenced this pull request Aug 12, 2024
…aMM import (pybamm-team#3848)

* import `typing_extensions` as optional_dependency

* Make `typing-extensions` a required dependency

* Try forward referencing for `sympy` in `IndependentVariable`

* Make `sympy` a required dependency

* Update docs for SymPy

* Import `sympy` without `have_optional_dependency`

* Import `sympy` without `TYPE_CHECKING`

* Changelog

* Update CHANGELOG.md

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>

* Rearrange position in changelog

---------

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>
js1tr3 pushed a commit to js1tr3/PyBaMM that referenced this pull request Aug 12, 2024
* Enable flake8-bugbear

Fix unused-loop-control-variable (B007)

Fix function-uses-loop-variable (B023)

Fix raise-without-from-inside-except (B904)

Fix mutable-argument-default (B006)

Fix no-explicit-stacklevel (B028)

Fix get-attr-with-constant (B009)

Fix loop-variable-overrides-iterator (B020)

Fix unary-prefix-increment-decrement (B002)

Fix function-call-in-default-argument (B008)

Fix cached-instance-method (B019)

* Show exception chain (B904)

* Disable single lint check (B019)

* delay xarray.DataArray initialization

* revert example

* Bump the actions group with 1 update (pybamm-team#3861)

Bumps the actions group with 1 update: [awalsh128/cache-apt-pkgs-action](https://github.com/awalsh128/cache-apt-pkgs-action).


Updates `awalsh128/cache-apt-pkgs-action` from 1.4.1 to 1.4.2
- [Release notes](https://github.com/awalsh128/cache-apt-pkgs-action/releases)
- [Commits](awalsh128/cache-apt-pkgs-action@v1.4.1...v1.4.2)

---
updated-dependencies:
- dependency-name: awalsh128/cache-apt-pkgs-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Make `typing-extensions` and `sympy` required dependencies to fix PyBaMM import (pybamm-team#3848)

* import `typing_extensions` as optional_dependency

* Make `typing-extensions` a required dependency

* Try forward referencing for `sympy` in `IndependentVariable`

* Make `sympy` a required dependency

* Update docs for SymPy

* Import `sympy` without `have_optional_dependency`

* Import `sympy` without `TYPE_CHECKING`

* Changelog

* Update CHANGELOG.md

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>

* Rearrange position in changelog

---------

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>

* Rename have_optional_dependency (pybamm-team#3866)

* Rename have_optional_dependency

* Change log

* Fix import

* style: pre-commit fixes

* Update pybamm/util.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Arjun Verma <arjunverma.oc@gmail.com>

* update changelog for pybamm-team#3862 (pybamm-team#3869)

* update changelog for pybamm-team#3862

* reword

* fix deprecation warning and check for electrode/particle diffusivity

* update test

* Edit naming for unused loop control variable (B007)

* pybamm-team#3883 updates to install from source (pybamm-team#3884)

* Disable benchmarks on PRs (pybamm-team#3876)

* Remove`[ latexify` extra (pybamm-team#3888)

* Improve Getting Started notebooks (pybamm-team#3750)

* improve tutorials 1 to 3

* use new syntax for plot_voltage_components

* style: pre-commit fixes

* simplify notebook by removing drive cycle

* add additional comment on dynamic_plot

* improve tutorial 5

* remove the use of solution in notebook 5, this is introduced in notebook 6

* fix minor typo

* minor text updates

* style: pre-commit fixes

* minor text updates plus extra links

* minor text updates!

* minor text updates

* remove tutorial 10 as very similar to tutorial 3 in creating models

* move and rename tutorial 11 to creating models section

* style: pre-commit fixes

* fix minor typo in current_with_time

* implement Rob & Eric's comments

* update docs links

* Valentin's comments

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>

* chore: update pre-commit hooks (pybamm-team#3890)

* chore: update pre-commit hooks

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](astral-sh/ruff-pre-commit@v0.2.2...v0.3.2)

* style: pre-commit fixes

* Update .git-blame-ignore-revs

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>

* style: pre-commit fixes

* Resolve conflicts (B904, B028, B020)

* Added a schedule on `needs-reply remove` workflow  (pybamm-team#3891)

* added schedule to need-reply workflow

* added schedule to need-reply workflow

---------

Co-authored-by: Arjun Verma <arjunverma.oc@gmail.com>

* style: pre-commit fixes

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Valentin Sulzer <valentinsulzer@hotmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Arjun Verma <arjunverma.oc@gmail.com>
Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <kratman@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Martin Robinson <martinjrobins@gmail.com>
Co-authored-by: Ferran Brosa Planella <Ferran.Brosa-Planella@warwick.ac.uk>
Co-authored-by: Santhosh <52504160+santacodes@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fortnightly build for wheels failed [Bug]: non-required dependency typing_extensions is imported by default
4 participants