Skip to content

Commit

Permalink
model_fields not settable directly (#943)
Browse files Browse the repository at this point in the history
* Update ubuntu-latest dependencies for mp-api

* Update ubuntu-latest dependencies for mp-api

* linting

* try model_copy

* try setting included fields in create_model directly

* wrong attr

* try putting __base__ back in

* include __config__ and transfer annotations

* switch back to __base__

* exclude null fields from repr/str

---------

Co-authored-by: github-actions <[email protected]>
  • Loading branch information
tschaume and github-actions authored Nov 25, 2024
1 parent ff8f84d commit f7de70e
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 191 deletions.
28 changes: 12 additions & 16 deletions mp_api/client/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ def _create_session(api_key, include_user_agent, headers):
mp_api_info = "mp-api/" + __version__ if __version__ else None
python_info = f"Python/{sys.version.split()[0]}"
platform_info = f"{platform.system()}/{platform.release()}"
session.headers[
"user-agent"
] = f"{mp_api_info} ({python_info} {platform_info})"
user_agent = f"{mp_api_info} ({python_info} {platform_info})"
session.headers["user-agent"] = user_agent

settings = MAPIClientSettings() # type: ignore
max_retry_num = settings.MAX_RETRIES
Expand Down Expand Up @@ -1062,29 +1061,26 @@ def _convert_to_model(self, data: list[dict]):
return data

def _generate_returned_model(self, doc):
model_fields = self.document_model.model_fields
set_fields = doc.model_fields_set

unset_fields = [field for field in doc.model_fields if field not in set_fields]
unset_fields = [field for field in model_fields if field not in set_fields]
include_fields = {
name: (model_fields[name].annotation, model_fields[name])
for name in set_fields
}

data_model = create_model( # type: ignore
"MPDataDoc",
**include_fields,
fields_not_requested=(list[str], unset_fields),
__base__=self.document_model, # type: ignore
__base__=self.document_model,
)

data_model.model_fields = {
**{
name: description
for name, description in data_model.model_fields.items()
if name in set_fields
},
"fields_not_requested": data_model.model_fields["fields_not_requested"],
}

def new_repr(self) -> str:
extra = ",\n".join(
f"\033[1m{n}\033[0;0m={getattr(self, n)!r}"
for n in data_model.model_fields
if n == "fields_not_requested" or n in set_fields
)

s = f"\033[4m\033[1m{self.__class__.__name__}<{self.__class__.__base__.__name__}>\033[0;0m\033[0;0m(\n{extra}\n)" # noqa: E501
Expand All @@ -1094,7 +1090,7 @@ def new_str(self) -> str:
extra = ",\n".join(
f"\033[1m{n}\033[0;0m={getattr(self, n)!r}"
for n in data_model.model_fields
if n != "fields_not_requested"
if n in set_fields
)

s = f"\033[4m\033[1m{self.__class__.__name__}<{self.__class__.__base__.__name__}>\033[0;0m\033[0;0m\n{extra}\n\n\033[1mFields not requested:\033[0;0m\n{unset_fields}" # noqa: E501
Expand Down
60 changes: 31 additions & 29 deletions requirements/requirements-ubuntu-latest_py3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ attrs==24.2.0
# jsonlines
# jsonschema
# referencing
bcrypt==4.2.0
bcrypt==4.2.1
# via paramiko
boto3==1.35.33
boto3==1.35.68
# via maggma
botocore==1.35.33
botocore==1.35.68
# via
# boto3
# s3transfer
Expand All @@ -27,21 +27,21 @@ cffi==1.17.1
# via
# cryptography
# pynacl
charset-normalizer==3.3.2
charset-normalizer==3.4.0
# via requests
contourpy==1.3.0
contourpy==1.3.1
# via matplotlib
cryptography==43.0.1
cryptography==43.0.3
# via paramiko
cycler==0.12.1
# via matplotlib
dnspython==2.6.1
dnspython==2.7.0
# via
# maggma
# pymongo
emmet-core==0.84.3rc0
emmet-core==0.84.3rc4
# via mp-api (pyproject.toml)
fonttools==4.54.1
fonttools==4.55.0
# via matplotlib
idna==3.10
# via requests
Expand All @@ -55,19 +55,19 @@ jsonlines==4.0.0
# via maggma
jsonschema==4.23.0
# via maggma
jsonschema-specifications==2023.12.1
jsonschema-specifications==2024.10.1
# via jsonschema
kiwisolver==1.4.7
# via matplotlib
latexcodec==3.0.0
# via pybtex
maggma==0.69.4
maggma==0.70.0
# via mp-api (pyproject.toml)
matplotlib==3.9.2
# via pymatgen
mongomock==4.2.0.post1
mongomock==4.3.0
# via maggma
monty==2024.7.30
monty==2024.10.21
# via
# emmet-core
# maggma
Expand All @@ -79,21 +79,22 @@ msgpack==1.1.0
# via
# maggma
# mp-api (pyproject.toml)
networkx==3.3
networkx==3.4.2
# via pymatgen
numpy==1.26.4
# via
# contourpy
# emmet-core
# maggma
# matplotlib
# monty
# pandas
# pymatgen
# scipy
# spglib
orjson==3.10.7
orjson==3.10.12
# via maggma
packaging==24.1
packaging==24.2
# via
# matplotlib
# mongomock
Expand All @@ -106,7 +107,7 @@ pandas==2.2.3
# pymatgen
paramiko==3.5.0
# via sshtunnel
pillow==10.4.0
pillow==11.0.0
# via matplotlib
plotly==5.24.1
# via pymatgen
Expand All @@ -116,28 +117,28 @@ pybtex==0.24.0
# pymatgen
pycparser==2.22
# via cffi
pydantic==2.9.2
pydantic==2.10.1
# via
# emmet-core
# maggma
# pydantic-settings
pydantic-core==2.23.4
pydantic-core==2.27.1
# via pydantic
pydantic-settings==2.5.2
pydantic-settings==2.6.1
# via
# emmet-core
# maggma
pydash==8.0.3
pydash==8.0.4
# via maggma
pymatgen==2024.10.3
pymatgen==2024.11.13
# via
# emmet-core
# mp-api (pyproject.toml)
pymongo==4.10.1
# via maggma
pynacl==1.5.0
# via paramiko
pyparsing==3.1.4
pyparsing==3.2.0
# via matplotlib
python-dateutil==2.9.0.post0
# via
Expand All @@ -163,17 +164,18 @@ requests==2.32.3
# via
# mp-api (pyproject.toml)
# pymatgen
rpds-py==0.20.0
rpds-py==0.21.0
# via
# jsonschema
# referencing
ruamel-yaml==0.18.6
# via
# maggma
# monty
# pymatgen
ruamel-yaml-clib==0.2.8
ruamel-yaml-clib==0.2.12
# via ruamel-yaml
s3transfer==0.10.2
s3transfer==0.10.4
# via boto3
scipy==1.14.1
# via pymatgen
Expand All @@ -183,7 +185,7 @@ six==1.16.0
# via
# pybtex
# python-dateutil
smart-open==7.0.4
smart-open==7.0.5
# via mp-api (pyproject.toml)
spglib==2.5.0
# via pymatgen
Expand All @@ -195,7 +197,7 @@ tabulate==0.9.0
# via pymatgen
tenacity==9.0.0
# via plotly
tqdm==4.66.5
tqdm==4.67.1
# via
# maggma
# pymatgen
Expand All @@ -214,7 +216,7 @@ urllib3==2.2.3
# via
# botocore
# requests
wrapt==1.16.0
wrapt==1.17.0
# via smart-open

# The following packages are considered to be unsafe in a requirements file:
Expand Down
Loading

0 comments on commit f7de70e

Please sign in to comment.