Skip to content

Commit f7de70e

Browse files
tschaumegithub-actions
andauthored
model_fields not settable directly (#943)
* 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]>
1 parent ff8f84d commit f7de70e

File tree

5 files changed

+231
-191
lines changed

5 files changed

+231
-191
lines changed

mp_api/client/core/client.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,8 @@ def _create_session(api_key, include_user_agent, headers):
178178
mp_api_info = "mp-api/" + __version__ if __version__ else None
179179
python_info = f"Python/{sys.version.split()[0]}"
180180
platform_info = f"{platform.system()}/{platform.release()}"
181-
session.headers[
182-
"user-agent"
183-
] = f"{mp_api_info} ({python_info} {platform_info})"
181+
user_agent = f"{mp_api_info} ({python_info} {platform_info})"
182+
session.headers["user-agent"] = user_agent
184183

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

10641063
def _generate_returned_model(self, doc):
1064+
model_fields = self.document_model.model_fields
10651065
set_fields = doc.model_fields_set
1066-
1067-
unset_fields = [field for field in doc.model_fields if field not in set_fields]
1066+
unset_fields = [field for field in model_fields if field not in set_fields]
1067+
include_fields = {
1068+
name: (model_fields[name].annotation, model_fields[name])
1069+
for name in set_fields
1070+
}
10681071

10691072
data_model = create_model( # type: ignore
10701073
"MPDataDoc",
1074+
**include_fields,
10711075
fields_not_requested=(list[str], unset_fields),
1072-
__base__=self.document_model, # type: ignore
1076+
__base__=self.document_model,
10731077
)
10741078

1075-
data_model.model_fields = {
1076-
**{
1077-
name: description
1078-
for name, description in data_model.model_fields.items()
1079-
if name in set_fields
1080-
},
1081-
"fields_not_requested": data_model.model_fields["fields_not_requested"],
1082-
}
1083-
10841079
def new_repr(self) -> str:
10851080
extra = ",\n".join(
10861081
f"\033[1m{n}\033[0;0m={getattr(self, n)!r}"
10871082
for n in data_model.model_fields
1083+
if n == "fields_not_requested" or n in set_fields
10881084
)
10891085

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

11001096
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

requirements/requirements-ubuntu-latest_py3.10.txt

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ attrs==24.2.0
1313
# jsonlines
1414
# jsonschema
1515
# referencing
16-
bcrypt==4.2.0
16+
bcrypt==4.2.1
1717
# via paramiko
18-
boto3==1.35.33
18+
boto3==1.35.68
1919
# via maggma
20-
botocore==1.35.33
20+
botocore==1.35.68
2121
# via
2222
# boto3
2323
# s3transfer
@@ -27,21 +27,21 @@ cffi==1.17.1
2727
# via
2828
# cryptography
2929
# pynacl
30-
charset-normalizer==3.3.2
30+
charset-normalizer==3.4.0
3131
# via requests
32-
contourpy==1.3.0
32+
contourpy==1.3.1
3333
# via matplotlib
34-
cryptography==43.0.1
34+
cryptography==43.0.3
3535
# via paramiko
3636
cycler==0.12.1
3737
# via matplotlib
38-
dnspython==2.6.1
38+
dnspython==2.7.0
3939
# via
4040
# maggma
4141
# pymongo
42-
emmet-core==0.84.3rc0
42+
emmet-core==0.84.3rc4
4343
# via mp-api (pyproject.toml)
44-
fonttools==4.54.1
44+
fonttools==4.55.0
4545
# via matplotlib
4646
idna==3.10
4747
# via requests
@@ -55,19 +55,19 @@ jsonlines==4.0.0
5555
# via maggma
5656
jsonschema==4.23.0
5757
# via maggma
58-
jsonschema-specifications==2023.12.1
58+
jsonschema-specifications==2024.10.1
5959
# via jsonschema
6060
kiwisolver==1.4.7
6161
# via matplotlib
6262
latexcodec==3.0.0
6363
# via pybtex
64-
maggma==0.69.4
64+
maggma==0.70.0
6565
# via mp-api (pyproject.toml)
6666
matplotlib==3.9.2
6767
# via pymatgen
68-
mongomock==4.2.0.post1
68+
mongomock==4.3.0
6969
# via maggma
70-
monty==2024.7.30
70+
monty==2024.10.21
7171
# via
7272
# emmet-core
7373
# maggma
@@ -79,21 +79,22 @@ msgpack==1.1.0
7979
# via
8080
# maggma
8181
# mp-api (pyproject.toml)
82-
networkx==3.3
82+
networkx==3.4.2
8383
# via pymatgen
8484
numpy==1.26.4
8585
# via
8686
# contourpy
8787
# emmet-core
8888
# maggma
8989
# matplotlib
90+
# monty
9091
# pandas
9192
# pymatgen
9293
# scipy
9394
# spglib
94-
orjson==3.10.7
95+
orjson==3.10.12
9596
# via maggma
96-
packaging==24.1
97+
packaging==24.2
9798
# via
9899
# matplotlib
99100
# mongomock
@@ -106,7 +107,7 @@ pandas==2.2.3
106107
# pymatgen
107108
paramiko==3.5.0
108109
# via sshtunnel
109-
pillow==10.4.0
110+
pillow==11.0.0
110111
# via matplotlib
111112
plotly==5.24.1
112113
# via pymatgen
@@ -116,28 +117,28 @@ pybtex==0.24.0
116117
# pymatgen
117118
pycparser==2.22
118119
# via cffi
119-
pydantic==2.9.2
120+
pydantic==2.10.1
120121
# via
121122
# emmet-core
122123
# maggma
123124
# pydantic-settings
124-
pydantic-core==2.23.4
125+
pydantic-core==2.27.1
125126
# via pydantic
126-
pydantic-settings==2.5.2
127+
pydantic-settings==2.6.1
127128
# via
128129
# emmet-core
129130
# maggma
130-
pydash==8.0.3
131+
pydash==8.0.4
131132
# via maggma
132-
pymatgen==2024.10.3
133+
pymatgen==2024.11.13
133134
# via
134135
# emmet-core
135136
# mp-api (pyproject.toml)
136137
pymongo==4.10.1
137138
# via maggma
138139
pynacl==1.5.0
139140
# via paramiko
140-
pyparsing==3.1.4
141+
pyparsing==3.2.0
141142
# via matplotlib
142143
python-dateutil==2.9.0.post0
143144
# via
@@ -163,17 +164,18 @@ requests==2.32.3
163164
# via
164165
# mp-api (pyproject.toml)
165166
# pymatgen
166-
rpds-py==0.20.0
167+
rpds-py==0.21.0
167168
# via
168169
# jsonschema
169170
# referencing
170171
ruamel-yaml==0.18.6
171172
# via
172173
# maggma
174+
# monty
173175
# pymatgen
174-
ruamel-yaml-clib==0.2.8
176+
ruamel-yaml-clib==0.2.12
175177
# via ruamel-yaml
176-
s3transfer==0.10.2
178+
s3transfer==0.10.4
177179
# via boto3
178180
scipy==1.14.1
179181
# via pymatgen
@@ -183,7 +185,7 @@ six==1.16.0
183185
# via
184186
# pybtex
185187
# python-dateutil
186-
smart-open==7.0.4
188+
smart-open==7.0.5
187189
# via mp-api (pyproject.toml)
188190
spglib==2.5.0
189191
# via pymatgen
@@ -195,7 +197,7 @@ tabulate==0.9.0
195197
# via pymatgen
196198
tenacity==9.0.0
197199
# via plotly
198-
tqdm==4.66.5
200+
tqdm==4.67.1
199201
# via
200202
# maggma
201203
# pymatgen
@@ -214,7 +216,7 @@ urllib3==2.2.3
214216
# via
215217
# botocore
216218
# requests
217-
wrapt==1.16.0
219+
wrapt==1.17.0
218220
# via smart-open
219221

220222
# The following packages are considered to be unsafe in a requirements file:

0 commit comments

Comments
 (0)