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

transition to pydantic ConfigDict #958

Open
DanielYang59 opened this issue Dec 17, 2024 · 4 comments
Open

transition to pydantic ConfigDict #958

DanielYang59 opened this issue Dec 17, 2024 · 4 comments

Comments

@DanielYang59
Copy link
Contributor

Getting the following warning:

DeprecationWarning: Accessing thermo data through MPRester.thermo is deprecated. Please use MPRester.materials.thermo instead.

For:

docs = self.thermo.search(
**input_params, # type: ignore
all_fields=False,
fields=fields,
)

@DanielYang59
Copy link
Contributor Author

DanielYang59 commented Dec 17, 2024

I'm seeing another deprecation from pydantic for:

class MAPIClientSettings(BaseSettings):

For code:

import warnings
from mp_api.client import MPRester

warnings.filterwarnings("error", category=DeprecationWarning)

mpr = MPRester()
s = mpr.get_structure_by_material_id("mp-1143")

I got:

Traceback (most recent call last):
  File "/Users/yang/developer/pymatgen/debug/test_pydantic_warning.py", line 6, in <module>
    from mp_api.client import MPRester
  File "/Users/yang/developer/pymatgen/venv312/lib/python3.12/site-packages/mp_api/client/__init__.py", line 7, in <module>
    from .core import MPRestError
  File "/Users/yang/developer/pymatgen/venv312/lib/python3.12/site-packages/mp_api/client/core/__init__.py", line 3, in <module>
    from .client import BaseRester, MPRestError
  File "/Users/yang/developer/pymatgen/venv312/lib/python3.12/site-packages/mp_api/client/core/client.py", line 35, in <module>
    from mp_api.client.core.settings import MAPIClientSettings
  File "/Users/yang/developer/pymatgen/venv312/lib/python3.12/site-packages/mp_api/client/core/settings.py", line 24, in <module>
    class MAPIClientSettings(BaseSettings):
  File "/Users/yang/developer/pymatgen/venv312/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 111, in __new__
    config_wrapper = ConfigWrapper.for_model(bases, namespace, kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yang/developer/pymatgen/venv312/lib/python3.12/site-packages/pydantic/_internal/_config.py", line 135, in for_model
    config_from_namespace = config_dict_from_namespace or prepare_config(config_class_from_namespace)
                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yang/developer/pymatgen/venv312/lib/python3.12/site-packages/pydantic/_internal/_config.py", line 295, in prepare_config
    warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
pydantic.warnings.PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.10/migration/

@tschaume
Copy link
Member

Thanks @DanielYang59! I fixed the first issue in c176278. Looking into switching to ConfigDict.

@tschaume tschaume changed the title DeprecationWarning: Accessing thermo data through MPRester.thermo is deprecated. Please use MPRester.materials.thermo instead. transition to pydantic ConfigDict Dec 17, 2024
@DanielYang59
Copy link
Contributor Author

Thanks a lot for the quick fix :)

@DanielYang59
Copy link
Contributor Author

DanielYang59 commented Dec 18, 2024

@tschaume I noticed another issue, mp_api doesn't seem to explicitly list pydantic as dependency. I'm not sure if this is intended, but IMO using an transitive dependency would be pretty dangerous (i.e. depend on another package which declares pydantic as its dependency).

api/pyproject.toml

Lines 22 to 32 in 87c072e

dependencies = [
"setuptools",
"msgpack",
"maggma>=0.57.1",
"pymatgen>=2022.3.7,!=2024.2.20",
"typing-extensions>=3.7.4.1",
"requests>=2.23.0",
"monty>=2024.12.10",
"emmet-core>=0.84.3rc6",
"smart_open",
]


Also it might be advisable to declare an lower version bound for direct dependencies to avoid accidentally installing outdated versions (under certain resolution strategy), e.g.

-  "setuptools",
+  "setuptools >= lowest_working_version",

Lower bounds are particularly critical when writing a library. It's important to declare the lowest version for each dependency that your library works with, and to validate that the bounds are correct — testing with --resolution lowest or --resolution lowest-direct. Otherwise, a user may receive an old, incompatible version of one of your library's dependencies and the library will fail with an unexpected error.


One last off-topic question, and I could be wrong as I don't really look into the code base of api, but does it really need setuptools as runtime dependency (instead of build dependency)?

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

No branches or pull requests

2 participants