Skip to content

Commit a8a9391

Browse files
authored
Update DynamicFieldsEnum to include all deprecated fields (#17409)
* Update DynamicFieldsEnum to include all deprecated fields * Update test to check actually invalid Dynamic value * Add a test for edge case where our enum is not updated
1 parent ef8cc62 commit a8a9391

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

tests/unit/forklift/test_metadata.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pytest
1515

1616
from packaging.version import Version
17+
from sqlalchemy.dialects.postgresql import ENUM
1718
from webob.multidict import MultiDict
1819

1920
from warehouse.forklift import metadata
@@ -283,7 +284,20 @@ def test_valid_dynamic(self):
283284

284285
def test_invalid_dynamic(self):
285286
data = MultiDict(metadata_version="2.2", name="spam", version="2.0")
286-
data.add("dynamic", "Requires")
287+
data.add("dynamic", "Invalid")
288+
with pytest.raises(ExceptionGroup) as excinfo:
289+
metadata.parse(None, form_data=data)
290+
_assert_invalid_metadata(excinfo.value, "dynamic")
291+
292+
def test_valid_dynamic_but_missing_from_our_enum(self, monkeypatch):
293+
"""
294+
Handles the case where there are new metadata fields that pypa/packaging
295+
considers to be valid, but don't exist in our enum and would otherwise fail
296+
when inserting them into the database
297+
"""
298+
monkeypatch.setattr(metadata, "DynamicFieldsEnum", ENUM())
299+
data = MultiDict(metadata_version="2.2", name="spam", version="2.0")
300+
data.add("dynamic", "author")
287301
with pytest.raises(ExceptionGroup) as excinfo:
288302
metadata.parse(None, form_data=data)
289303
_assert_invalid_metadata(excinfo.value, "dynamic")
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
"""
13+
Update DynamicFieldsEnum to include all deprecated fields
14+
15+
Revision ID: 24aa37164e72
16+
Revises: ed4cc2ef6b0f
17+
Create Date: 2025-01-14 15:19:26.813044
18+
"""
19+
20+
from alembic import op
21+
from alembic_postgresql_enum import ColumnType, TableReference
22+
23+
revision = "24aa37164e72"
24+
down_revision = "ed4cc2ef6b0f"
25+
26+
27+
def upgrade():
28+
op.sync_enum_values(
29+
enum_schema="public",
30+
enum_name="release_dynamic_fields",
31+
new_values=[
32+
"Platform",
33+
"Supported-Platform",
34+
"Summary",
35+
"Description",
36+
"Description-Content-Type",
37+
"Keywords",
38+
"Author",
39+
"Author-Email",
40+
"Maintainer",
41+
"Maintainer-Email",
42+
"License",
43+
"License-Expression",
44+
"License-File",
45+
"Classifier",
46+
"Requires-Dist",
47+
"Requires-Python",
48+
"Requires-External",
49+
"Project-Url",
50+
"Provides-Extra",
51+
"Provides-Dist",
52+
"Obsoletes-Dist",
53+
"Home-Page",
54+
"Download-Url",
55+
"Requires",
56+
"Provides",
57+
"Obsoletes",
58+
],
59+
affected_columns=[
60+
TableReference(
61+
table_schema="public",
62+
table_name="releases",
63+
column_name="dynamic",
64+
column_type=ColumnType.ARRAY,
65+
)
66+
],
67+
enum_values_to_rename=[],
68+
)
69+
70+
71+
def downgrade():
72+
op.sync_enum_values(
73+
enum_schema="public",
74+
enum_name="release_dynamic_fields",
75+
new_values=[
76+
"Platform",
77+
"Supported-Platform",
78+
"Summary",
79+
"Description",
80+
"Description-Content-Type",
81+
"Keywords",
82+
"Home-Page",
83+
"Download-Url",
84+
"Author",
85+
"Author-Email",
86+
"Maintainer",
87+
"Maintainer-Email",
88+
"License",
89+
"License-Expression",
90+
"License-File",
91+
"Classifier",
92+
"Requires-Dist",
93+
"Requires-Python",
94+
"Requires-External",
95+
"Project-Url",
96+
"Provides-Extra",
97+
"Provides-Dist",
98+
"Obsoletes-Dist",
99+
],
100+
affected_columns=[
101+
TableReference(
102+
table_schema="public",
103+
table_name="releases",
104+
column_name="dynamic",
105+
column_type=ColumnType.ARRAY,
106+
)
107+
],
108+
enum_values_to_rename=[],
109+
)

warehouse/packaging/models.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,6 @@ class ReleaseURL(db.Model):
547547
"Description",
548548
"Description-Content-Type",
549549
"Keywords",
550-
"Home-Page",
551-
"Download-Url",
552550
"Author",
553551
"Author-Email",
554552
"Maintainer",
@@ -564,6 +562,14 @@ class ReleaseURL(db.Model):
564562
"Provides-Extra",
565563
"Provides-Dist",
566564
"Obsoletes-Dist",
565+
# Although the following are deprecated fields, they are technically
566+
# permitted as dynamic by PEP 643
567+
# https://github.com/pypa/setuptools/issues/4797#issuecomment-2589514950
568+
"Home-Page",
569+
"Download-Url",
570+
"Requires",
571+
"Provides",
572+
"Obsoletes",
567573
name="release_dynamic_fields",
568574
)
569575

0 commit comments

Comments
 (0)