opencobra / cobrapy

COBRApy is a package for constraint-based modeling of metabolic networks.
http://opencobra.github.io/cobrapy/
GNU General Public License v2.0
461 stars 216 forks source link

Incompatibility with pydantic version 2 #1345

Closed teddygroves closed 1 year ago

teddygroves commented 1 year ago

Problem description

I would like my project to have both the latest versions of cobrapy and pydantic as dependencies.

However this isn't currently possible as cobrapy pins pydantic version 1.6.

This is a problem because pydantic version 2 introduced some breaking changes, so would take a lot of work to make my project compatible with version 1.6.

Code Sample

Here is a minimal pyproject.toml that demonstrates the problem

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "myproject"
version = "0.0.1"

dependencies = [
    "cobra >= 0.26.3",
    "pydantic >= 2.0.0",
]

and here is the error message when I run pip install -e . in an empty folder containing this pyproject.toml:

ERROR: Cannot install cobra==0.26.3 and myproject==0.0.1 because these package versions have conflicting dependencies.

The conflict is caused by:
    myproject 0.0.1 depends on pydantic>=2.0.0
    cobra 0.26.3 depends on pydantic~=1.6

To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip attempt to solve the dependency conflict

ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts

From a quick look I don't think cobrapy uses pydantic except for in this file, which I think is compatible with pydantic version 2 as is. It therefore doesn't seem like a very hard bug to fix: just remove the pin or change it to a version greater than 2.

Context

Not relevant as the bug happens before cobra is installed

``` ```
cdiener commented 1 year ago

Seems reasonable to me. Feel free to send a PR with a newer requirement. That way we will see if the CI passes.

Midnighter commented 1 year ago

Maybe just >=1.6 would allow both users of <2 and >2 to happily use cobrapy.

teddygroves commented 1 year ago

The PR above just changes the ~ here to a > as @Midnighter suggests.

I think the CI is just waiting for a maintainer to approve before it sets off.

cdiener commented 1 year ago

Now fixed in 0.27.0.

chrismgowen commented 9 months ago

Problem description

From a quick look I don't think cobrapy uses pydantic except for in this file, which I think is compatible with pydantic version 2 as is. It therefore doesn't seem like a very hard bug to fix: just remove the pin or change it to a version greater than 2.

@teddygroves or @cdiener, did you test whether the file referenced is compatible with pydantic v2? I ask because when setting up a new environment with conda, I get the following import error when importing cobra (cobra=0.29.0, pydantic=2.5.6):

Traceback (most recent call last): File "", line 1, in File "/home/a/.conda/envs/cobra/lib/python3.12/site-packages/cobra/init.py", line 17, in from cobra import io File "/home/a/.conda/envs/cobra/lib/python3.12/site-packages/cobra/io/init.py", line 9, in from cobra.io.web import AbstractModelRepository, BiGGModels, BioModels, load_model File "/home/a/.conda/envs/cobra/lib/python3.12/site-packages/cobra/io/web/init.py", line 6, in from .biomodels_repository import BioModels File "/home/a/.conda/envs/cobra/lib/python3.12/site-packages/cobra/io/web/biomodels_repository.py", line 14, in class BioModelsFile(pydantic.BaseModel): ^^^^^^^^^^^^^^^^^^ File "/home/a/.conda/envs/cobra/lib/python3.12/site-packages/pydantic/init.py", line 372, in getattr module = import_module(module_name, package=package) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/a/.conda/envs/cobra/lib/python3.12/importlib/init.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/a/.conda/envs/cobra/lib/python3.12/site-packages/pydantic/main.py", line 13, in from pydantic_core import PydanticUndefined ImportError: cannot import name 'PydanticUndefined' from 'pydantic_core' (unknown location)

I forced conda to install pydantic-1.10.0 and import cobra works fine. I do not have experience with pydantic, so I'm not clear if this is an issue with the requirements or not, but it appears that something does break when upgrading to pydantic v2.

cdiener commented 9 months ago

Hi this error seems to be raised in pydantic itself and there seems to have been a bug in the conda package. Can you update your pedantic in the environment to a newer build and try again?