Skip to content

Commit 3706ef6

Browse files
authored
Revert "gh-132947: Apply changes from importlib_metadata 8.7 (#137885)" (#137924)
This reverts commit 5292fc0.
1 parent 8750e5e commit 3706ef6

File tree

12 files changed

+118
-290
lines changed

12 files changed

+118
-290
lines changed

Lib/importlib/metadata/__init__.py

Lines changed: 62 additions & 143 deletions
Large diffs are not rendered by default.

Lib/importlib/metadata/_adapters.py

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
1-
import email.message
2-
import email.policy
31
import re
42
import textwrap
3+
import email.message
54

65
from ._text import FoldedCase
76

87

9-
class RawPolicy(email.policy.EmailPolicy):
10-
def fold(self, name, value):
11-
folded = self.linesep.join(
12-
textwrap.indent(value, prefix=' ' * 8, predicate=lambda line: True)
13-
.lstrip()
14-
.splitlines()
15-
)
16-
return f'{name}: {folded}{self.linesep}'
17-
18-
198
class Message(email.message.Message):
20-
r"""
21-
Specialized Message subclass to handle metadata naturally.
22-
23-
Reads values that may have newlines in them and converts the
24-
payload to the Description.
25-
26-
>>> msg_text = textwrap.dedent('''
27-
... Name: Foo
28-
... Version: 3.0
29-
... License: blah
30-
... de-blah
31-
... <BLANKLINE>
32-
... First line of description.
33-
... Second line of description.
34-
... <BLANKLINE>
35-
... Fourth line!
36-
... ''').lstrip().replace('<BLANKLINE>', '')
37-
>>> msg = Message(email.message_from_string(msg_text))
38-
>>> msg['Description']
39-
'First line of description.\nSecond line of description.\n\nFourth line!\n'
40-
41-
Message should render even if values contain newlines.
42-
43-
>>> print(msg)
44-
Name: Foo
45-
Version: 3.0
46-
License: blah
47-
de-blah
48-
Description: First line of description.
49-
Second line of description.
50-
<BLANKLINE>
51-
Fourth line!
52-
<BLANKLINE>
53-
<BLANKLINE>
54-
"""
55-
569
multiple_use_keys = set(
5710
map(
5811
FoldedCase,
@@ -104,20 +57,15 @@ def __getitem__(self, item):
10457
def _repair_headers(self):
10558
def redent(value):
10659
"Correct for RFC822 indentation"
107-
indent = ' ' * 8
108-
if not value or '\n' + indent not in value:
60+
if not value or '\n' not in value:
10961
return value
110-
return textwrap.dedent(indent + value)
62+
return textwrap.dedent(' ' * 8 + value)
11163

11264
headers = [(key, redent(value)) for key, value in vars(self)['_headers']]
11365
if self._payload:
11466
headers.append(('Description', self.get_payload()))
115-
self.set_payload('')
11667
return headers
11768

118-
def as_string(self):
119-
return super().as_string(policy=RawPolicy())
120-
12169
@property
12270
def json(self):
12371
"""

Lib/importlib/metadata/_collections.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import collections
2-
import typing
32

43

54
# from jaraco.collections 3.3
@@ -25,10 +24,7 @@ def freeze(self):
2524
self._frozen = lambda key: self.default_factory()
2625

2726

28-
class Pair(typing.NamedTuple):
29-
name: str
30-
value: str
31-
27+
class Pair(collections.namedtuple('Pair', 'name value')):
3228
@classmethod
3329
def parse(cls, text):
3430
return cls(*map(str.strip, text.split("=", 1)))

Lib/importlib/metadata/_functools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import functools
21
import types
2+
import functools
33

44

55
# from jaraco.functools 3.3

Lib/importlib/metadata/_meta.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
from __future__ import annotations
22

33
import os
4-
from collections.abc import Iterator
5-
from typing import (
6-
Any,
7-
Protocol,
8-
TypeVar,
9-
overload,
10-
)
4+
from typing import Protocol
5+
from typing import Any, Dict, Iterator, List, Optional, TypeVar, Union, overload
6+
117

128
_T = TypeVar("_T")
139

@@ -24,25 +20,25 @@ def __iter__(self) -> Iterator[str]: ... # pragma: no cover
2420
@overload
2521
def get(
2622
self, name: str, failobj: None = None
27-
) -> str | None: ... # pragma: no cover
23+
) -> Optional[str]: ... # pragma: no cover
2824

2925
@overload
30-
def get(self, name: str, failobj: _T) -> str | _T: ... # pragma: no cover
26+
def get(self, name: str, failobj: _T) -> Union[str, _T]: ... # pragma: no cover
3127

3228
# overload per python/importlib_metadata#435
3329
@overload
3430
def get_all(
3531
self, name: str, failobj: None = None
36-
) -> list[Any] | None: ... # pragma: no cover
32+
) -> Optional[List[Any]]: ... # pragma: no cover
3733

3834
@overload
39-
def get_all(self, name: str, failobj: _T) -> list[Any] | _T:
35+
def get_all(self, name: str, failobj: _T) -> Union[List[Any], _T]:
4036
"""
4137
Return all values associated with a possibly multi-valued key.
4238
"""
4339

4440
@property
45-
def json(self) -> dict[str, str | list[str]]:
41+
def json(self) -> Dict[str, Union[str, List[str]]]:
4642
"""
4743
A JSON-compatible form of the metadata.
4844
"""
@@ -54,11 +50,11 @@ class SimplePath(Protocol):
5450
"""
5551

5652
def joinpath(
57-
self, other: str | os.PathLike[str]
53+
self, other: Union[str, os.PathLike[str]]
5854
) -> SimplePath: ... # pragma: no cover
5955

6056
def __truediv__(
61-
self, other: str | os.PathLike[str]
57+
self, other: Union[str, os.PathLike[str]]
6258
) -> SimplePath: ... # pragma: no cover
6359

6460
@property

Lib/importlib/metadata/_typing.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

Lib/test/test_importlib/metadata/_path.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
# from jaraco.path 3.7.2
2-
3-
from __future__ import annotations
1+
# from jaraco.path 3.7
42

53
import functools
64
import pathlib
7-
from collections.abc import Mapping
8-
from typing import TYPE_CHECKING, Protocol, Union, runtime_checkable
9-
10-
if TYPE_CHECKING:
11-
from typing_extensions import Self
5+
from typing import Dict, Protocol, Union
6+
from typing import runtime_checkable
127

138

149
class Symlink(str):
@@ -17,25 +12,29 @@ class Symlink(str):
1712
"""
1813

1914

20-
FilesSpec = Mapping[str, Union[str, bytes, Symlink, 'FilesSpec']]
15+
FilesSpec = Dict[str, Union[str, bytes, Symlink, 'FilesSpec']] # type: ignore
2116

2217

2318
@runtime_checkable
2419
class TreeMaker(Protocol):
25-
def __truediv__(self, other, /) -> Self: ...
26-
def mkdir(self, *, exist_ok) -> object: ...
27-
def write_text(self, content, /, *, encoding) -> object: ...
28-
def write_bytes(self, content, /) -> object: ...
29-
def symlink_to(self, target, /) -> object: ...
20+
def __truediv__(self, *args, **kwargs): ... # pragma: no cover
21+
22+
def mkdir(self, **kwargs): ... # pragma: no cover
23+
24+
def write_text(self, content, **kwargs): ... # pragma: no cover
25+
26+
def write_bytes(self, content): ... # pragma: no cover
27+
28+
def symlink_to(self, target): ... # pragma: no cover
3029

3130

32-
def _ensure_tree_maker(obj: str | TreeMaker) -> TreeMaker:
33-
return obj if isinstance(obj, TreeMaker) else pathlib.Path(obj)
31+
def _ensure_tree_maker(obj: Union[str, TreeMaker]) -> TreeMaker:
32+
return obj if isinstance(obj, TreeMaker) else pathlib.Path(obj) # type: ignore
3433

3534

3635
def build(
3736
spec: FilesSpec,
38-
prefix: str | TreeMaker = pathlib.Path(),
37+
prefix: Union[str, TreeMaker] = pathlib.Path(), # type: ignore
3938
):
4039
"""
4140
Build a set of files/directories, as described by the spec.
@@ -67,24 +66,23 @@ def build(
6766

6867

6968
@functools.singledispatch
70-
def create(content: str | bytes | FilesSpec, path: TreeMaker) -> None:
69+
def create(content: Union[str, bytes, FilesSpec], path):
7170
path.mkdir(exist_ok=True)
72-
# Mypy only looks at the signature of the main singledispatch method. So it must contain the complete Union
73-
build(content, prefix=path) # type: ignore[arg-type] # python/mypy#11727
71+
build(content, prefix=path) # type: ignore
7472

7573

7674
@create.register
77-
def _(content: bytes, path: TreeMaker) -> None:
75+
def _(content: bytes, path):
7876
path.write_bytes(content)
7977

8078

8179
@create.register
82-
def _(content: str, path: TreeMaker) -> None:
80+
def _(content: str, path):
8381
path.write_text(content, encoding='utf-8')
8482

8583

8684
@create.register
87-
def _(content: Symlink, path: TreeMaker) -> None:
85+
def _(content: Symlink, path):
8886
path.symlink_to(content)
8987

9088

Lib/test/test_importlib/metadata/fixtures.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import contextlib
1+
import sys
22
import copy
3-
import functools
43
import json
5-
import pathlib
64
import shutil
7-
import sys
5+
import pathlib
86
import textwrap
7+
import functools
8+
import contextlib
99

1010
from test.support import import_helper
1111
from test.support import os_helper
@@ -14,10 +14,14 @@
1414
from . import _path
1515
from ._path import FilesSpec
1616

17-
if sys.version_info >= (3, 9):
18-
from importlib import resources
19-
else:
20-
import importlib_resources as resources
17+
18+
try:
19+
from importlib import resources # type: ignore
20+
21+
getattr(resources, 'files')
22+
getattr(resources, 'as_file')
23+
except (ImportError, AttributeError):
24+
import importlib_resources as resources # type: ignore
2125

2226

2327
@contextlib.contextmanager

Lib/test/test_importlib/metadata/test_api.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import importlib
21
import re
32
import textwrap
43
import unittest
4+
import importlib
55

6+
from . import fixtures
67
from importlib.metadata import (
78
Distribution,
89
PackageNotFoundError,
@@ -14,8 +15,6 @@
1415
version,
1516
)
1617

17-
from . import fixtures
18-
1918

2019
class APITests(
2120
fixtures.EggInfoPkg,

Lib/test/test_importlib/metadata/test_main.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import importlib
2-
import pickle
31
import re
2+
import pickle
43
import unittest
4+
import importlib
5+
import importlib.metadata
56
from test.support import os_helper
67

78
try:
89
import pyfakefs.fake_filesystem_unittest as ffs
910
except ImportError:
1011
from .stubs import fake_filesystem_unittest as ffs
1112

13+
from . import fixtures
14+
from ._path import Symlink
1215
from importlib.metadata import (
1316
Distribution,
1417
EntryPoint,
@@ -21,9 +24,6 @@
2124
version,
2225
)
2326

24-
from . import fixtures
25-
from ._path import Symlink
26-
2727

2828
class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
2929
version_pattern = r'\d+\.\d+(\.\d)?'
@@ -157,16 +157,6 @@ def test_valid_dists_preferred(self):
157157
dist = Distribution.from_name('foo')
158158
assert dist.version == "1.0"
159159

160-
def test_missing_metadata(self):
161-
"""
162-
Dists with a missing metadata file should return None.
163-
164-
Ref python/importlib_metadata#493.
165-
"""
166-
fixtures.build_files(self.make_pkg('foo-4.3', files={}), self.site_dir)
167-
assert Distribution.from_name('foo').metadata is None
168-
assert metadata('foo') is None
169-
170160

171161
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
172162
@staticmethod

0 commit comments

Comments
 (0)